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;
}
Discussion:
43 comments Page 3 of 5.
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.
I agree to @avi please read the stm of avi I hope you understanding this program.
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
Assigns value at present location of s1 to present location of s2. Thereby substituting b at first position. Same process is continued
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".
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".
Balasaheb Mule said:
1 decade ago
s1[0]=s2[0] i.e. I replaces with B;
In 1st Loop string prints "Bndia"
Again, s1 and s2 incremented by 1.
So,that becomes,
s1[1]=s2[1] i.e. n replaces with I;
In 2nd loop string prints "BIdia"
Again, s1 and s2 incremented by 1.
So,that becomes,
s1[2]=s2[2] i.e. d replaces with X;
In 3nd loop string prints "BIXia"
And Loops terminates.
Finally output will be,
BndiaBIdiaBIXia
In 1st Loop string prints "Bndia"
Again, s1 and s2 incremented by 1.
So,that becomes,
s1[1]=s2[1] i.e. n replaces with I;
In 2nd loop string prints "BIdia"
Again, s1 and s2 incremented by 1.
So,that becomes,
s1[2]=s2[2] i.e. d replaces with X;
In 3nd loop string prints "BIXia"
And Loops terminates.
Finally output will be,
BndiaBIdiaBIXia
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.
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....
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....
Shabana said:
1 decade ago
Here the str1 = "India".
str2 = "BIX".
s1=*str1,*s2=*str2 //means s1,s2 contains the the address of str1 and str2;
While conditions use the loop repetitions take place
First str1++ means s1 points to I.
srt2++ means s2 points to B.
So str1++ gets copy the value of B and replaced I in place B and then incremented it will print Bndia,
Because of loop it will points str1 to n and str2 points to I so str1 copy the I in and replace I in place of n.
And then incremented it will print --BIdia same for BIXia.
str2 did not contain any character so loop is terminated and it will print: Bndia BIdia BIXia.
Hope understand clearly.
str2 = "BIX".
s1=*str1,*s2=*str2 //means s1,s2 contains the the address of str1 and str2;
While conditions use the loop repetitions take place
First str1++ means s1 points to I.
srt2++ means s2 points to B.
So str1++ gets copy the value of B and replaced I in place B and then incremented it will print Bndia,
Because of loop it will points str1 to n and str2 points to I so str1 copy the I in and replace I in place of n.
And then incremented it will print --BIdia same for BIXia.
str2 did not contain any character so loop is terminated and it will print: Bndia BIdia BIXia.
Hope understand clearly.
(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?
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?
So first post increment or dereferencing should be done, later assignment should take place?
Manjunath K L said:
10 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 %.
'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 %.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers