C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 20)
20.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    char str1[] = "India";
    char str2[] = "BIX";
    char *s1 = str1, *s2=str2;
    while(*s1++ = *s2++)
        printf("%s", str1);

    printf("\n");
    return 0;
}
IndiaBIX
BndiaBIdiaBIXia
India
(null)
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
43 comments Page 2 of 5.

Vasavi said:   1 decade ago
Hai Friends. I have a small doubt.
PLZ Explain any one.
The output of while loop is either true or false.
here in first checking loop statement *s1++ not Equal to *s2++. so printf is not executed. Then how we get the result.

Sonam said:   1 decade ago
@vasavi:
in while... it is assingning the value rather then comparing.. so when assignment is done then while gets the value true i.e while(true) ....thus while loop is executing...also check out uttam comment it is really good....

Jarvis said:   1 decade ago
The output is FINE but why the loop is executing only THRICE?

I know the thing of ending the second string BIX\0 but why its not assigning \0 to I?

What's the terminating condition?

Siddu said:   1 decade ago
But assignment operator is have less precedence than ++ and *.

So first post increment or dereferencing should be done, later assignment should take place?

Manjunath K L said:   9 years ago
The 1st letter of *s2++ is BIX = B.

'B' is assigned to the 1st letter of S1 so it becomes Bndia....in the same way rest will be carried out likes BIdia, BIXia... until full S2 values finished.

All the best who referred IndiaBix it's %.

Sandhya said:   9 years ago
I did not understand this, tell me clarity.

Santhosh said:   9 years ago
Thanks Mahendra.

Praju said:   9 years ago
Well done @Uttam.

Rohan said:   7 years ago
You are absolutely right @Sowmya.

str1 value must be "India" as output.

Manish said:   7 years ago
I think while need a condition but there is '='assignment operator that can only assign the value so. It couldn't run.


Post your comments here:

Your comments will be displayed after verification.