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.
Raja said:
7 years ago
Thanks @Avi.
Riswan said:
6 years ago
Thanks to you all.
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.
Shrinidhi said:
2 decades ago
Someone please explain.
Raju said:
2 decades ago
The 1st letter of *s2++ is BIX = 'B'.
'B' is assigned to the 1st letter of S1 so it becomes Bndia.
Rest are follows the same way.
'B' is assigned to the 1st letter of S1 so it becomes Bndia.
Rest are follows the same way.
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
Ramu kaka said:
1 decade ago
You all wrong see in while statement operator '=' is assignment operator not a equality operator, so s2 is assigned to s1.
Nishu said:
1 decade ago
Agree wth ramu.
Nitin said:
1 decade ago
@(ramu and nishu)::it works.raju has explained it well ,read his explanation once more you will get the idea.each assignment comes to TRUE and that leads to the PRINT.
Aditi said:
1 decade ago
None of the explanations are clear. Please give some more description. Anyone?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers