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.
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.
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.
Bharathi said:
1 decade ago
Thanks raju.
It was good explanation.
It was good explanation.
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.
Anand said:
1 decade ago
Actually in this program it working like this.
Case 1: Post increment is happening.
Case 2: STR1's character replacing with str2's fist character then printing Bndia then incrementing one so second will BIdia then once again increment so it will print BIXia.
Case 1: Post increment is happening.
Case 2: STR1's character replacing with str2's fist character then printing Bndia then incrementing one so second will BIdia then once again increment so it will print BIXia.
Ravitheja j said:
1 decade ago
Thanks raju.
Vanaja said:
1 decade ago
Thanks raju and anand, subbu.
Lijina said:
1 decade ago
str1="india" str2="bix"
so
first->bndia
second->bidia
third->bixia
so
first->bndia
second->bidia
third->bixia
Joe said:
1 decade ago
Thanks raju.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers