What will be the following C code print ?
int a[] = {1,2,3,4};
int *p = a ;
printf("%d", *(++p) + 2);
Tripura Public Service Commission - 2026,
Answer: B
The program prints 4.
int *p = a;makes p point at a[0], which holds 1.++pis a pre-increment, so p moves to a[1] before it is used.*(++p)therefore reads a[1], which is 2.2 + 2gives 4, printed with %d.
3 is what you get from *p + 2 without the increment (1 + 2). 5 would need p to move two places, to a[2], which holds 3. There is no compilation error: incrementing a pointer variable is legal, and the parentheses make the increment apply to p before the dereference.
Please log in or register to report this question.