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 str[] = "peace";
    char *s = str;
    printf("%s\n", s++ +3);
    return 0;
}

 

  • A.peace
  • B.eace
  • C.ace
  • D.ce

Answer: D

Correct answer: ce

The expression s++ +3 uses the original value of s, since ++ here is postfix, and adds 3 to it. s points at the start of "peace", so the argument to printf addresses position 3.

Counting from zero - p, e, a, c - position 3 is the c, so the output is ce. The increment of s itself happens too, but s is never used again. The spacing in s++ +3 is necessary: s+++3 would tokenise differently.

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

Post your comment

Please log in or register to post a comment.