What is printed by the following ANSI C program?
#include<stdio.h> int main(int argc, char *argv[]) { int x = 1, z[2] = {10, 11}; int *p = NULL; p = &x; *p = 10; p = &z[1]; *(&z[0] + 1) += 3; printf("%d, %d, %d\n", x, z[0], z[1]); return 0; }
(A)
1, 10, 11
(B)
1, 10, 14
(C)
10, 14, 11
(D)
10, 10, 14
Answer: (D)
Explanation:
From the C-programming code, x=1, z[0]=10 and z[1]=11
int *p= NULL; p=&x;
*p=10;
After this is executed, we are modifying the value of the variable ‘x’ and it’s value gets modified as x=10.
*(&z[0]+1)+=3;
The above line means,
*( address of z[0]+1 ) += 3
=>*( address of z[1] ) += 3
=>z[1] += 3 ==> z[1] = z[1] + 3
=>z[1]=14
It is clear that the value of z[0] is not modified. So, option D is the correct answer.
Quiz of this Question
Please comment below if you find anything wrong in the above post