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 4 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)
Sandhya said:
9 years ago
I did not understand this, tell me clarity.
Santhosh said:
9 years ago
Thanks Mahendra.
Praju said:
9 years ago
Well done @Uttam.
Emanuel said:
9 years ago
I think it will be BIX.
(1)
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.
'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)
SHIVANI said:
8 years ago
Thank you all.
(1)
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.
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)
Rohan said:
7 years ago
You are absolutely right @Sowmya.
str1 value must be "India" as output.
str1 value must be "India" as output.
Manish said:
7 years ago
I think while need a condition but there is '='assignment operator that can only assign the value so. It couldn't run.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers