C Programming - Strings - Discussion

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;
}

[A]. Hello
[B]. HelloHello
[C]. No output
[D]. ello

Answer: Option A

Explanation:

No answer description available for this question.

Kavyashree said: (Oct 11, 2010)  
Here s will points to the beginig of string str1.

And t will points to the begining address of string str2.

In while loop we are copying the elements from str1 to str2 using pointers s and t untill end of string is reached.

Nikhil said: (Nov 9, 2010)  
In the loop, it is always a true condition hence it will never come out of the loop so no output

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

Newbie said: (May 24, 2011)  
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: (Aug 5, 2011)  
Actually it is not == it is just = in while loop so it will assign the str1 to str2 till end of string is reached.

Mahalaxmibyahatti said: (Aug 5, 2011)  
Actually the condition in the while loop is having assignment operator i.e = and not == so str1 will be copied to str2 till end of string is reached.

Anish said: (Aug 30, 2011)  
== is used for comparison ex: a=2; b=2 a==b returns true otherwise false
but = is used for assignment ex: a=1; b=a; then b have 1
in if() any value except 0 or null values is true

Napster said: (Nov 2, 2011)  
Simply look at the condition in the while,it is assignment condition which is always true hence the code will copy the hello into str2. If condition is like as '==' then nothing will be copied into it.

Harish said: (Sep 5, 2012)  
There is assignment operator in while whenthe str2 will end it can't assign any value or 0 so the loop will be false and the loop will be exit

Saurabh said: (Sep 17, 2012)  
@wikilok is current ...

Logu said: (Aug 12, 2013)  
'\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.

Puju said: (Aug 13, 2013)  
*t++ = *s++;

Can anyone help ,what does this statement means?

Ankita said: (Nov 22, 2013)  
It simply increase the address by one. But here it can not enter into loop. So answer is "Hello".

Neeraj Singla said: (Apr 23, 2014)  
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: (Jul 2, 2014)  
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: (Sep 15, 2014)  
Why this statement *t++ = *s++; is not executing?

Manoj Goyal said: (Sep 29, 2014)  
Because *t++ = *s++ statement become (*t++ = '\0')==0, So condition become false and loop will be terminated.

Skm said: (Jul 5, 2015)  
But here value of str is printed not value of t. So answer should be "no output".

Deepak Chauhan said: (Feb 2, 2016)  
*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.

Ashu said: (Aug 19, 2016)  
When the loop is terminated and no value assigned to str2 so how can it print hello?

Pawan said: (Jan 26, 2017)  
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.

Varsha said: (Aug 15, 2017)  
You are right, Thank you @Pawan.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.