C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 19)
19.
What will be the output of the program ?
#include<stdio.h>
int main()
{
char str1[] = "Hello";
char str2[10];
char *t, *s;
s = str1;
t = str2;
while(*t=*s)
*t++ = *s++;
printf("%s\n", str2);
return 0;
}
Discussion:
22 comments Page 2 of 3.
Logu said:
1 decade ago
'\0' equal to zero(0).
i.e '\0' ascii value is 0.
So here, *t=*s is doing the copy the value of *s to *t .
So finally it copy the '\0' to *t. now *t has '\0' i.e zero.
So loop exit. So it prints hello.
i.e '\0' ascii value is 0.
So here, *t=*s is doing the copy the value of *s to *t .
So finally it copy the '\0' to *t. now *t has '\0' i.e zero.
So loop exit. So it prints hello.
Puju said:
1 decade ago
*t++ = *s++;
Can anyone help ,what does this statement means?
Can anyone help ,what does this statement means?
Ankita said:
1 decade ago
It simply increase the address by one. But here it can not enter into loop. So answer is "Hello".
Neeraj singla said:
1 decade ago
In while statement the operator is equal to(=) instead of (==) comparing operator so in while condition. It will start copying the elements of *s to *t and condition becomes true and output will be hello.
Madhu said:
1 decade ago
But in printf statement str2 is given so it has to be printed which was not assigned any value. How could Hello be printed? Any one explain it please.
Vishakha said:
1 decade ago
Why this statement *t++ = *s++; is not executing?
Manoj goyal said:
1 decade ago
Because *t++ = *s++ statement become (*t++ = '\0')==0, So condition become false and loop will be terminated.
SKM said:
1 decade ago
But here value of str is printed not value of t. So answer should be "no output".
Deepak Chauhan said:
10 years ago
*t++ means (*t)++ here the address is not being incremented but the value at the address is being incremented to increment the address it should be written as t++ or *++t;
Ex:
while(*t=*s)
{t++;
s++;}
As per above the progranm will keep incrementing the character h then i, j, k. And will store it in str2 base address.
Ex:
while(*t=*s)
{t++;
s++;}
As per above the progranm will keep incrementing the character h then i, j, k. And will store it in str2 base address.
Ashu said:
9 years ago
When the loop is terminated and no value assigned to str2 so how can it print hello?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers