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

Pradeep said:   1 decade ago
We have str1="India" and str2="BIX".
And also *s1=str1, *s2=str2.

As the given while condition is like while (*s1++==*s2++).

Here *s1 is pointing to "India" as it increment *s1++ then it.
Will point to "ndia" and similarly *s2++ will point to "IX".

While (*s1++=*s2++).

Note that it applying assignment operator not equality operator so the first letter of str1 will b replace by the first letter of str2. It will replaces till the strings completes.

So in the first iteration the output will be "Bndia".

And similarly second iteration fist two letter are replace ("In" of str1 replaces with "BI" of str2) and output generated will be BIdia. And the final Iteration output will be "BIXia".

So the actual output is "BndiaBIdiaBIXia".

Abc said:   1 decade ago
The statement in while i.e. while(*s1++=*s2++)
Assigns value at present location of s1 to present location of s2. Thereby substituting b at first position. Same process is continued

Vikas said:   1 decade ago
Read the statement of @avi. He give the clear way to define the step wise and clear understand.

I agree to @avi please read the stm of avi I hope you understanding this program.

Shailesh said:   1 decade ago
What raju said on 17th sep was right.

Beyond that I want to tell something is that, while loop is longlasting upto string sizes of both strings are matched i.e => bix=ind.

Now upto this condition there will be 2 increments of both s1, s2.

As at 2nd itteration BI{size)is still less than Ind(size), it will not violate while loop & print BIdia same as raju already told.

Next itteration is last one b'z BIX (srng size) matches with dia(size)& now s2 get assigned to s1 i.e =>BIXia lastly get print.

Joe said:   1 decade ago
Thanks raju.

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

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

Ravitheja j said:   1 decade ago
Thanks raju.

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.

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.


Post your comments here:

Your comments will be displayed after verification.