C Programming Output Questions in GATE CSE: Patterns and Traps
C output questions are the most predictable items in the GATE CSE paper and among the easiest to lose. The program is short, the concept is familiar, and the mark goes to whoever traces it correctly the first time. This guide covers the recurring patterns, one small worked example for each, and the traps the option sets are built around. Every example below is written for this guide and traced by hand.
In this guide
Key takeaways
- Programming questions use C. The syllabus line is unchanged for 2027.
- Since 2014 the default form is a numerical answer: the program is written so the output is a single integer.
- Two or three C questions appear per shift, and pointer or array tracing and recursion have appeared in essentially every paper.
- Marks are lost to procedure, not to theory: post-increment, precedence, the sign of the modulo result, static locals, off-by-one.
- Trace with a written table of variables. The candidates who trace in their heads are the ones who finish the paper unsure.
How GATE examines C
The official section reads: programming in C, recursion, arrays, stacks, queues, linked lists, trees, binary search trees, binary heaps, graphs. The programming half is examined almost entirely through output tracing, because any program with a print statement has a numeric answer, which suits the numerical-answer format. Multiple-select questions since 2021 add a second form: four statements about what a fragment does, with no partial credit.
The patterns below are ordered by how often they have appeared in past papers.
| Pattern | Usual format | Usual marks |
|---|---|---|
| Pointer and array output tracing | NAT, sometimes MCQ | 1–2 |
| Two-dimensional arrays through a row pointer | NAT | 2 |
| Recursion: return value, call count, print order | NAT | 2 |
| Operator precedence, increment, short-circuit, switch, macros | MCQ or NAT | 1 |
| Integer division and modulo, bitwise one-liners | NAT | 1–2 |
| Storage classes, scope, static | NAT or MCQ | 1–2 |
| sizeof, structs and unions | MCQ or NAT | 1–2 |
| Loop counting and the recurrence behind it | NAT | 1–2 |
For the wider subject, including the data structures half, see Programming and Data Structures important topics.
Pointer and array tracing
The classic one-liner confuses two expressions that look alike. *p++ increments the pointer and yields the value it pointed to before the increment. (*p)++ increments the value and leaves the pointer alone.
int a[4] = {1, 2, 3, 4};
int *p = a;
int x = *p++; /* x gets a[0]; p now points at a[1] */
int y = (*p)++; /* y gets 2; a[1] becomes 3 */
printf("%d %d %d", x, y, a[1]); /* prints 1 2 3 */
The other half of this pattern is scaling. Adding 1 to an int * moves four bytes when an integer is four bytes wide, so p + k names the element k positions later, never the byte k positions later. A question that mixes an int * with a char * is testing exactly this.
Two-dimensional arrays and row pointers
A two-dimensional array is examined by giving two pointers of different types into the same storage and asking for a sum.
int a[2][3] = {{10, 20, 30}, {40, 50, 60}};
int (*q)[3] = a;
printf("%d", q[1][0] + *(*q + 2)); /* prints 70 */
Here q points to a whole row, so q[1][0] is a[1][0], that is 40. *q is the first row, which decays to a pointer to a[0][0], so *(*q + 2) is a[0][2], that is 30.
The discipline that removes the difficulty: translate every expression into a[i][j] form before you compute anything. An element pointer offset k becomes row k divided by the number of columns and column k modulo the number of columns.
Operator precedence and evaluation order
Two rules cause most of the damage. The equality operators bind tighter than the bitwise operators, and logical operators short-circuit.
int x = 2;
printf("%d %d", x & 2 == 2, (x & 2) == 2); /* prints 0 1 */
The first expression parses as x & (2 == 2), which is 2 & 1, that is 0. Only the parenthesised version tests the bit.
int i = 0, j = 0;
if (i++ && j++) { }
printf("%d %d", i, j); /* prints 1 0 */
i++ yields 0, the condition is already false, and the right operand is never evaluated, so j is untouched.
The order of precedence worth memorising, from tightest: postfix, unary, multiplicative, additive, shifts, relational, equality, bitwise AND, bitwise XOR, bitwise OR, logical AND, logical OR, conditional, assignment, comma.
Macros belong to this pattern because they are textual substitution, not function calls.
#define SQ(a) a * a
printf("%d", SQ(2 + 3)); /* expands to 2 + 3 * 2 + 3, prints 11 */
Integer division and modulo
C truncates toward zero, and the remainder takes the sign of the dividend.
printf("%d %d", -7 / 2, -7 % 2); /* prints -3 -1 */
Anyone who imports the mathematical definition of modulo answers 1 for the second value and loses the mark. The same rule governs bitwise one-liners such as clearing the lowest set bit, which are a rising one-mark form.
Recursion output
Three forms recur: the value returned, the number of calls, and the order in which things are printed. The print-order form places one statement before the recursive call and one after it.
void f(int n) {
if (n == 0) return;
printf("%d", n);
f(n - 1);
printf("%d", n);
}
/* f(3) prints 321123 */
The prints before the call come out in descending order on the way down; the prints after the call come out in the reverse of that sequence while the stack unwinds. Once you see this, you never trace the whole call tree again.
For the call-count form, build a small table of values from the base case upward rather than drawing the tree. A function with two recursive calls produces a count that satisfies its own recurrence, and the table reaches the answer in seconds. That is the same machinery as the recurrences in Algorithms.
Static variables and scope
A static local is initialised once and keeps its value between calls.
int g(void) { static int c = 0; c += 2; return c; }
/* in main */
int u = g();
int v = g();
printf("%d %d", u, v); /* prints 2 4 */
The harder variant adds a global with the same name as the static local, plus a block-local of that name inside a loop. Resolve every identifier by the innermost scope that declares it, and write the three variables in three separate columns of your trace.
Structs and unions
Struct assignment copies every member, including arrays, but a pointer member is copied as a pointer, so both structs then refer to the same target.
struct S { int v; int *p; };
int x = 1;
struct S s1 = {5, &x}, s2;
s2 = s1;
*s2.p = 7; /* writes through the shared pointer */
s2.v = 8; /* writes only the copy */
printf("%d %d %d", s1.v, x, s2.v); /* prints 5 7 8 */
Size questions on structs and unions depend on alignment and padding: a union is at least as large as its widest member, and a struct may be larger than the sum of its members. GATE states the size of an integer when the answer turns on it.
The undefined and unspecified traps
Recognise these even though the paper rarely asks you to predict their output.
- Modifying a variable twice in one expression, as in
i = i++ + 1, is undefined. There is no correct printed value to compute. - The order in which function arguments are evaluated is unspecified, so a call whose two arguments both change the same variable has no defined printed order.
sizeofapplied to an array parameter of a function gives the size of a pointer, not of the array, because the parameter is a pointer.- Returning the address of a local variable leaves a pointer to storage that no longer exists.
- Assuming an integer is two bytes wide, as some older textbooks do. GATE states four bytes when it matters.
This article describes the patterns and gives one example of each. The Programming and Data Structures chapter of the GATE CSE 2027 book works through each pattern with full solutions and a bank of original questions built on them, all traced statement by statement.
How to practise
Set a four-minute limit for a fifteen-line program and keep a written variable table for every trace. Do the pointer and recursion patterns first, since they appear in essentially every paper, then the one-mark semantics items, which are cheap marks once the precedence table is memorised. Finish with structs, scope and loop counting. Most people's error log in this subject holds the same three mistakes repeatedly, and clearing those is worth more than new topics. The wider list is in common mistakes that cost marks in GATE CSE, and the attempt rules for numerical and multiple-select items are in the MCQ, MSQ and NAT strategy guide.
Frequently asked questions
Which programming language is used in GATE CSE questions?
C. The official pattern states that programming questions use C, and the syllabus line reads "Programming in C. Recursion." Questions give a short program and ask for the printed value, usually as a numerical answer. You need exact semantics, not idiomatic style, so read every program as a compiler would.
What kind of C questions are asked in GATE CSE?
Mostly output tracing. The recurring forms are pointer and array arithmetic, two-dimensional arrays read through a row pointer, recursion return values and print order, operator precedence and increment semantics, integer division and modulo with negative operands, static and scope, and struct or union sizes and copying.
How many marks do C programming questions carry in GATE CSE?
Programming and Data Structures as a whole has averaged about 9.9 marks per paper in analyst compilations for 2009 to 2026, with a range of 6 to 13. The C part of that usually supplies two or three questions per shift, and at least one of them is a two-mark numerical answer. These figures are unofficial.
Does GATE ask questions with undefined behaviour in C?
GATE generally avoids output questions whose answer depends on the compiler. It states the size of an integer when the size matters and avoids expressions that modify a variable twice between sequence points. You still need to recognise these cases, because a question may ask which statement is undefined or unspecified rather than what it prints.
How do I prepare for C output questions in GATE CSE?
Trace on paper with a written table of variables, never in your head. Memorise the operator precedence table, the rules for post and pre increment, and the sign rule for modulo with negative operands. Then solve past questions under a four-minute limit until a fifteen-line program no longer needs a second reading.
Sources
- GATE 2027 official website (IIT Madras)
- GATE 2027 CS syllabus (official PDF)
- GATE 2027 question paper pattern (official)
- GeeksforGeeks subject-wise weightage for GATE CS
- GATE Overflow previous year questions
Dates, fees and the syllabus are set by the GATE 2027 organising institute and can change. Always confirm at gate2027.iitm.ac.in.