Multiple choice

What does the following fragment of C-program print? char c[ ] "GATE2011"; char *p =c; printf "%s", p+p [3] - p[1];

  1. GATE2011

  2. E2011

  3. 2011

  4. 011

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The pointer p points to 'G' (index 0). p[3] = 'E' (ASCII 69) and p[1] = 'A' (ASCII 65). So p[3] - p[1] = 69 - 65 = 4. Therefore p + 4 points to index 4, which is '2'. printf(%s, p+4) prints from '2' onward: '2011'. The key is pointer arithmetic with character ASCII values.