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 1 of 5.
Mahendra said:
9 years ago
-------------------------------------------------
char str1[] = "India";
char str2[] = "BIX";
char *s1 = str1, *s2=str2;
-------------------------------------------------
Here str1 is array holding values as - >>
str1 ['I','n','d','i','a']
0 1 2 3 4
^
|
s1 // s1 is pointing to base address or
first element of array str1
And str2 is array holding values as - >>
str1 ['B','I','X']
0 1 2
^
|
s2 // s2 is pointing to base address or
first element of array str2
-------------------------------------------------
while(*s1++ = *s2++)
printf("%s", str1);
-------------------------------------------------
This while loop will be executed till there is some values in the array pointed by s1 and s2. or simply till '\0' is not encountered.
*s1++ = *s2++
The above statement means assign the value pointed by s2 into value pointed by s1;
Increment will be done after assignment because it is postfix operator.
As above statement is executed value pointed by s2(i.e. B ) is assigned to the location pointed by s1(i.e. atr1[0]).
str1 is having values as [B,n,d,i,a].
printf will print ---------->> Bndia.
On the second iteration.
As s1 and s2 are incremented they are now pointing to'n' and 'I' respectively.
Now in a similar way, and will be replaced by I.
And,
str1 is having values as [B,I,d,i,a]
printf will print ---------->> BIdia
Similarly on 3rd iteration,
str1 is having values as [B,I,X,i,a]
printf will print ---------->> BIXia
Therefore, the output is BndiaBIdiaBIXia.
char str1[] = "India";
char str2[] = "BIX";
char *s1 = str1, *s2=str2;
-------------------------------------------------
Here str1 is array holding values as - >>
str1 ['I','n','d','i','a']
0 1 2 3 4
^
|
s1 // s1 is pointing to base address or
first element of array str1
And str2 is array holding values as - >>
str1 ['B','I','X']
0 1 2
^
|
s2 // s2 is pointing to base address or
first element of array str2
-------------------------------------------------
while(*s1++ = *s2++)
printf("%s", str1);
-------------------------------------------------
This while loop will be executed till there is some values in the array pointed by s1 and s2. or simply till '\0' is not encountered.
*s1++ = *s2++
The above statement means assign the value pointed by s2 into value pointed by s1;
Increment will be done after assignment because it is postfix operator.
As above statement is executed value pointed by s2(i.e. B ) is assigned to the location pointed by s1(i.e. atr1[0]).
str1 is having values as [B,n,d,i,a].
printf will print ---------->> Bndia.
On the second iteration.
As s1 and s2 are incremented they are now pointing to'n' and 'I' respectively.
Now in a similar way, and will be replaced by I.
And,
str1 is having values as [B,I,d,i,a]
printf will print ---------->> BIdia
Similarly on 3rd iteration,
str1 is having values as [B,I,X,i,a]
printf will print ---------->> BIXia
Therefore, the output is BndiaBIdiaBIXia.
(11)
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".
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)
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.
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.
Fatma mofreh said:
2 years ago
First step:
*s1++ point to I and *s2++ point to B
the first while loop I is replaced by B
so the first output will be Bndia
Second step:
*s1++ point to n and *s2++ point to I
The second while loop n is replaced by I
So the second output will be BIdia.
Third step:
*s1++ point to d and *s2++ point to X
The third while loop d is replaced by X.
So the third output will be BIXia.
Then;
*s2++ point to null so while is ended.
then print \n
*s1++ point to I and *s2++ point to B
the first while loop I is replaced by B
so the first output will be Bndia
Second step:
*s1++ point to n and *s2++ point to I
The second while loop n is replaced by I
So the second output will be BIdia.
Third step:
*s1++ point to d and *s2++ point to X
The third while loop d is replaced by X.
So the third output will be BIXia.
Then;
*s2++ point to null so while is ended.
then print \n
(7)
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.
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
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.
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.
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
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
Wikiok said:
1 decade ago
#include<stdio.h>
int main()
{
char aa[]="1stString";
char bb[]="2nd";
char *sa,*sb;
sa=&aa;sb=&bb;
while (*sa=*sb)
{
printf("Value for while condition: %c,\n",*sa=*sb);
*sa++;
*sb++;
}
}
Try this, and you will see, that "=" operator gives back other values, than true.
int main()
{
char aa[]="1stString";
char bb[]="2nd";
char *sa,*sb;
sa=&aa;sb=&bb;
while (*sa=*sb)
{
printf("Value for while condition: %c,\n",*sa=*sb);
*sa++;
*sb++;
}
}
Try this, and you will see, that "=" operator gives back other values, than true.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers