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;
}
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.
Please log in or register to report this question.