Machine-translated by Google. Questions are authored in English.
Top

Discussion

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: A

Correct answer: Hello

The condition *t = *s is an assignment, not a comparison: it copies the character and yields it, so the loop continues while that character is non-zero. Each character is therefore written twice - once by the condition and once by the body - but to the same place, and the pointers advance only in the body. When the terminating null is reached the condition copies it and evaluates to 0, ending the loop with str2 properly terminated: Hello.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.