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.

Kavyashree said:   1 decade ago
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:   1 decade ago
In the loop, it is always a true condition hence it will never come out of the loop so no output

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

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.

Mahalaxmibyahatti said:   1 decade ago
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:   1 decade ago
== 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:   1 decade ago
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:   1 decade ago
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:   1 decade ago
@wikilok is current ...


Post your comments here:

Your comments will be displayed after verification.