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;
}
Hello
HelloHello
No output
ello
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
22 comments Page 2 of 3.

Newbie said:   1 decade ago
How it is always a true condition? it will be false condition when *t not equal to *s. rite? here *t and *s are not equal. rite?

Mahalaxmi said:   1 decade ago
Actually it is not == it is just = in while loop so it will assign the str1 to str2 till end of string is reached.

Manoj goyal said:   1 decade ago
Because *t++ = *s++ statement become (*t++ = '\0')==0, So condition become false and loop will be terminated.

Ankita said:   1 decade ago
It simply increase the address by one. But here it can not enter into loop. So answer is "Hello".

Nikhil said:   1 decade ago
In the loop, it is always a true condition hence it will never come out of the loop so no output

Ashu said:   9 years ago
When the loop is terminated and no value assigned to str2 so how can it print hello?

SKM said:   1 decade ago
But here value of str is printed not value of t. So answer should be "no output".

Wikiok said:   1 decade ago
If s[n] == '\0' then ( *t=*s ) == zero, so it is false, so the loop will exit.

Puju said:   1 decade ago
*t++ = *s++;

Can anyone help ,what does this statement means?

Vishakha said:   1 decade ago
Why this statement *t++ = *s++; is not executing?


Post your comments here:

Your comments will be displayed after verification.