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 1 of 5.

Aravind said:   2 decades ago
How it comes please explain anyone pa.

Shrinidhi said:   2 decades ago
Someone please explain.

Raju said:   1 decade ago
The 1st letter of *s2++ is BIX = 'B'.

'B' is assigned to the 1st letter of S1 so it becomes Bndia.

Rest are follows the same way.

Subbu said:   1 decade ago
Here we have to understand operator precedence.
let us understand:
ite1:*s1++=*s2++ (here s2 points to B and s1 points to I) by this end of the statement first s1 gets value of s2(B) then increment takes place India->Bndia
Ite2: now s1="Bndia" *s1 points to n
here *s2 points to I
same operation repeated this time it is BIdia ang goes on until end of the loop

Ramu kaka said:   1 decade ago
You all wrong see in while statement operator '=' is assignment operator not a equality operator, so s2 is assigned to s1.

Nishu said:   1 decade ago
Agree wth ramu.

Nitin said:   1 decade ago
@(ramu and nishu)::it works.raju has explained it well ,read his explanation once more you will get the idea.each assignment comes to TRUE and that leads to the PRINT.

Aditi said:   1 decade ago
None of the explanations are clear. Please give some more description. Anyone?

Manoj said:   1 decade ago
while(*s1++ = *s2++)

What is condition does?

Atul said:   1 decade ago
@Manoj: It is a assignment operator not a comparison. so it will be true in all cases. Point not clear to me is what will be the result at end of "BIX". it will assign '\0' to *s1++. what while loop will do that time? Rest explanation is given by Ramu.


Post your comments here:

Your comments will be displayed after verification.