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

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.

Avi said:   1 decade ago
Here str1 points to I of India first. str2 points B of BIX... in while loop B of bix is assigned to I of india now string value changes to Bndia since assignment statement is executed successfully Bndia is printed. now str1 points to n of Bndia and str2 points to I of BIX again n is replaced by I ... this condition is continued till '\0' is encountered where assignment statement fails and the program comes out of the loop.

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

Bharathi said:   1 decade ago
Thanks raju.

It was good explanation.

Uttam said:   1 decade ago
1st step:

I is replaced by B. So str1=Bndia.

Now on op: Bndia

2nd step:

s1 points to 'i' of str1 and s2 points to 'I'of str2.
so str1 becomes BIdia.
now on op: BndiaBIdia

3rd step:
output is appended by BIXia.
so now op: BndiaBIdiaBIXia.

4th step:
s2 points to '\0' i.e null character.
so s1 becomes null.
so in the while loop condition,

while('\0')=while(false).

So compiler comes out of loop.

Anand said:   1 decade ago
Actually in this program it working like this.

Case 1: Post increment is happening.

Case 2: STR1's character replacing with str2's fist character then printing Bndia then incrementing one so second will BIdia then once again increment so it will print BIXia.

Ravitheja j said:   1 decade ago
Thanks raju.

Vanaja said:   1 decade ago
Thanks raju and anand, subbu.

Lijina said:   1 decade ago
str1="india" str2="bix"
so
first->bndia
second->bidia
third->bixia


Post your comments here:

Your comments will be displayed after verification.