Skip to content
Official PYQ · 2022

GATE CS 2022 Q11 — Programming in C

Explain with AI

What is printed by the following ANSI C\text{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;

}
Choose one option
View worked solution

Answer: D

Let's trace the execution of the program step-by-step:

  1. Initialization:
    int x = 1, z[2] = {10, 11};
    • x is initialized to 11.
    • Array z is initialized with z[0] = 10 and z[1] = 11.
  2. Pointer Assignment and Modification:
    p = &x;
    *p = 10;
    • Pointer p is set to the address of x.
    • Dereferencing p and assigning 1010 updates the value of x to 1010.
  3. Pointer Redirection:
    p = &z[1];
    • Pointer p now points to z[1]. Note that p is not used after this statement.
  4. Pointer Arithmetic and Modification:
    *(&z[0] + 1) += 3;
    • &z[0] is the address of the first element of the array z.
    • Adding 11 to this pointer (&z[0] + 1) shifts it to point to the next element, which is &z[1].
    • Dereferencing this expression gives z[1].
    • Thus, the statement is equivalent to: z[1]=z[1]+3  ⟹  z[1]=11+3=14z[1] = z[1] + 3 \implies z[1] = 11 + 3 = 14
  5. Printing the Values:
    printf("%d, %d, %d\n", x, z[0], z[1]);
    • The printed values are: x = 10, z[0] = 10, and z[1] = 14.

Therefore, the output printed is 10, 10, 14, which corresponds to Option D.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions