Skip to content
Official PYQ · 2017

GATE CS 2017 Set 2 Q14 — Programming in C

Explain with AI

Consider the following function implemented in C:

void printxy(int x, int y) {
    int *ptr;
    x=0;
    ptr=&x;
    y=*ptr;
    *ptr=1;
    printf(“%d, %d”, x, y);
}

The output of invoking printxy(1,1)printxy(1,1) is:

Choose one option
View worked solution

Answer: C

Let us trace the execution of the function call printxy(1, 1) step-by-step:

  1. Initialization:

    The function is called with arguments x = 1 and y = 1. Inside the function, local variables x and y are initialized to 1.

  2. Step 1: x = 0;

    The value of x is updated to 0.

  3. Step 2: ptr = &x;

    The pointer ptr is assigned the address of the local variable x. Thus, ptr now points to x.

  4. Step 3: y = *ptr;

    The value at the address stored in ptr (which is the current value of x, i.e., 0) is assigned to y. Hence, y becomes 0.

  5. Step 4: *ptr = 1;

    The value at the address pointed to by ptr is set to 1. Since ptr points to x, this direct assignment changes the value of x to 1.

  6. Step 5: printf("%d, %d", x, y);

    At this point, the values of the variables are:

    • x = 1
    • y = 0

    Thus, the output printed is 1, 0.

Therefore, the correct option is C.

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

Published
Report a correction
Keep exploringMore Programming in C questions