Write the output of the following code fragment : GATE 2011

Write the output of the following code fragment:

char c[] = "GATE2011";
char *p = c;
printf("%s", p + p[3] - p[1]);

a) GATE2011        b) E2011        c) 2011        d) 011

Answer

The output is: c) 2011
Following is the explanation:
Lets say base address of the string c is 200, so pointer variable p also contains 200.
Now, p + p[3] - p[1] = 200 + 'E' - 'A' = 200 + 69 - 65 = 204.
204 is the address of the byte containing '2', as we are printing the string starting from location 204, it will give us "2011".

0Comment