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
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2017 Set 2 · Q14
Time
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) isSelect one answerSingle correct
Solution
Official answer: C) 1,0
Programming in CLet's trace the execution of printxy(1, 1) step by step:
Initial values of parameters: , .
x = 0; becomes .
ptr = &x; points to the memory location of .
y = *ptr; is assigned the value stored at ptr (which is ), so .
*ptr = 1; the value at the address ptr is updated to . Since ptr points to , becomes .
printf("%d,%d", x, y); prints the current values of and , which are and respectively.
Thus, the output printed is 1,0, corresponding to option (C).
Try the question first, then check your work.
Examiner trap
Trace the pointer dereferencing carefully: modifying *ptr directly modifies the variable x whose address ptr holds.
This Programming in C previous year question is Q14 of the GATE CSE 2017 Set 2 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.
Free diagnostic
Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 20 of them are 1-mark MCQs like this one, and GATE CSE 2017 Set 2 has 6.
How GATE tests Programming in CIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.