Project ForgeSign up free
Programming in C
Programming Fundamentals
GATE 2017
Medium

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) is

  • A0,0
  • B0,1
  • 1,0
  • D1,1

Explanation

x is set to 0. ptr now points at x. y = *ptr reads x's current value, 0, into y. *ptr = 1 then sets x back to 1 (through the pointer). At the printf, x=1 and y=0, so the output is '1,0'.

Source: GATE CS 2017 (Session 2) (2017)

This question is part of a 15-day revision cycle — sign in and Project Forge will bring it back the day before you'd forget it.

Start revising free