What is printed by the following ANSI C program? #include<stdio.h> int…

2022

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;
    }

  1. A.

    1, 10, 11

  2. B.

    1, 10, 14

  3. C.

    10, 14, 11

  4. D.

    10, 10, 14

Attempted by 141 students.

Show answer & explanation

Correct answer: D

Key result: The program prints 10, 10, 14.

  1. Initial values: x = 1; z[0] = 10; z[1] = 11; p = NULL.

  2. p = &x; then *p = 10; sets x to 10 because p points to x.

  3. p = &z[1]; now p points to z[1], but no write happens through p after this assignment.

  4. *(&z[0] + 1) += 3; here &z[0] + 1 is the address of z[1], so this adds 3 to z[1], changing it from 11 to 14.

  5. Final values: x = 10; z[0] = 10; z[1] = 14. The printf prints these in order: x, z[0], z[1].

Common mistakes:

  • Misreading *(&z[0] + 1) as modifying z[0] instead of z[1].

  • Overlooking the effect of *p = 10 when p points to x, which changes x from its initial value.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir