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 1 of 3.

Varsha said:   8 years ago
You are right, Thank you @Pawan.

Pawan said:   9 years ago
According to me here,

while(*t=*s)
*t++ = *s++;

Here first *t=*s will take the value, after entering into the while it is again going to overwrite the value and then do the post-increment of the pointer. Now the pointers got next character location and again the same repeats till the while fails because of the array *t comes with the null character.
(2)

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

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.

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

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

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

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.

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.

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


Post your comments here:

Your comments will be displayed after verification.