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 2 of 5.
Santhosh said:
9 years ago
Thanks Mahendra.
Sandhya said:
9 years ago
I did not understand this, tell me clarity.
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)
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 %.
'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 %.
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?
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?
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)
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....
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.
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers