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.

Suraj said:   8 years ago
The 1st letter of *s2++ is BIX = 'B'.

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

After that *S2++ mens increment by +1 so the result is BI now replace with *S1++ first two letters are BIdia(dia) from S1 we assign S2 in S1 .after that increment *S2++ so the result is BIX and it replaced 3 letter with S1 and then print.

Hope you understand.
(2)

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.

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.

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 %.

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....

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.

Sowmya said:   8 years ago
Hello friends,

str1 values r stored in S1 , we r operating on S1, str1 values remain same kW,in pf statement we should print the values of str1, so str1 values should be "India " kw.
(1)

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?

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.


Post your comments here:

Your comments will be displayed after verification.