Sub topic 20
Strategies for GATE-style C questions
This chapter assumes you have read the earlier chapters on pointers, arrays, dynamic memory, the call stack, and recursion. You should be able to read *p++, explain array decay, and write a simple recurrence. The goal here is a repeatable routine for exam questions, plus a list of the traps that cost marks.
The working model: a four-pass routine
GATE C questions are short, and the wrong answers are usually chosen to match one specific misreading. A fixed order of checks catches most of them.
- Pass 1: Is the behavior defined? Look for out-of-range access, use after
free, reads of uninitialized locals, signed overflow, and two unsequenced modifications of one object. If the code has any of these, the correct option is often "undefined" or "compiler dependent". - Pass 2: Fix the types and sizes. Write the type of every variable. Note
sizeofof each operand and whether an array has decayed into a pointer. - Pass 3: Draw memory. Sketch cells with addresses or indices. Mark where each pointer points.
- Pass 4: Execute step by step. Apply precedence, then trace loops or calls in order, keeping a table of values.
Pass 1: Undefined, unspecified, and implementation-defined
These three terms mean different things, and exam options use them carefully.
| Situation | Category | Example |
|---|---|---|
| Behavior has no rule at all | Undefined | `a[5]` on `int a[5]`, use after `free`, `i = i++ + 1` |
| Several valid orders, compiler picks one | Unspecified | Order of evaluating function arguments in `f(i++, i++)` |
| Compiler must document its choice | Implementation-defined | `sizeof(int)`, right shift of a negative `int` |
Two rules save time:
- If the same variable is modified twice, or modified and read, between sequence points and the read is not used to compute the new value, treat it as undefined.
- If a result depends on
sizeof(int)orsizeof(void *), check whether the question states the platform. If it does not, the answer may be "machine dependent".
Pass 2: Sizes and decay
The most common wrong answer comes from treating an array parameter as an array. Here is a short program that combines decay, pointer subtraction, and array indexing.
#include <stdio.h>
#include <string.h>
int main(void) {
int a[5] = {1, 2, 3, 4, 5};
int *p = a + 1;
printf("%d %d %d\n", p[2], *(a + 4) - *p, (int)(&a[4] - p));
char s[] = "hello";
char *t = "hello";
printf("%zu %zu %d\n", sizeof s, strlen(s), sizeof t == sizeof(char *));
return 0;
}The first line prints 4 3 3. Here p points at a[1], so p[2] is a[3], which is 4. Then *(a + 4) is 5 and *p is 2, so the difference is 3. The pointer difference &a[4] - p counts elements, giving 3.
The second line prints 6 5 1. The array s holds six bytes because of the terminator, while strlen stops before it. The size of t is the size of a pointer, not of the string.
Pass 3 and 4: Precedence and tracing
Postfix ++ and [] bind tighter than unary *. Unary * and prefix ++ have equal precedence and group right to left. From this:
*p++reads the pointee, then movesp.(*p)++changes the pointee and leavespalone.*++pmovesp, then reads.++*pincrements the pointee and yields the new value.
For recursion, write a call table. Add one row per call, and record the value printed before and after the recursive call.
#include <stdio.h>
void f(int n) {
if (n == 0) {
return;
}
printf("%d ", n);
f(n - 1);
printf("%d ", n);
}
int main(void) {
f(3);
printf("\n");
return 0;
}The output is 3 2 1 1 2 3. Statements before the recursive call run on the way down, so they print in call order. Statements after it run as the calls return, so they print in reverse order. When a question swaps the position of one printf, only one half of this output changes.
Recurrences and stack depth
For a recursive function, ask three questions in order:
- What does one call do besides recursing? That is the non-recursive work,
f(n). - How many recursive calls are made, and on what size of input?
- How deep can the chain go? Stack depth is the length of the longest chain, not the total number of calls.
| Code shape | Recurrence | Time | Stack depth |
|---|---|---|---|
| One call on `n - 1`, constant work | `T(n) = T(n-1) + c` | `O(n)` | `O(n)` |
| Two calls on `n - 1`, constant work | `T(n) = 2T(n-1) + c` | `O(2^n)` | `O(n)` |
| Two calls on `n / 2`, constant work | `T(n) = 2T(n/2) + c` | `O(n)` | `O(log n)` |
| One call on `n / 2`, constant work | `T(n) = T(n/2) + c` | `O(log n)` | `O(log n)` |
| One call on `n - 1`, linear work | `T(n) = T(n-1) + n` | `O(n^2)` | `O(n)` |
Tree recursion such as naive Fibonacci has exponential time but only linear stack depth, because only one root-to-leaf path is on the stack at once.
Common traps checklist
- An array parameter
int arr[]is reallyint *arr, sosizeof arris the pointer size. p + 1moves bysizeof *pbytes, not by one byte.sizeofyieldssize_t, which is unsigned. A comparison like-1 < sizeof(int)is false after the conversion.malloc(n)fornints is a bug. Usemalloc(n * sizeof *p).- A function that returns the address of a local variable returns a dangling pointer.
- A string literal must not be modified.
char *t = "hi"; t[0] = 'H';is undefined. - Integer division truncates toward zero, so
-7 / 2is-3and-7 % 2is-1.
Exercise
Give the output of this fragment and say whether it is defined. int a[] = {2, 4, 6, 8}; int p = a + 1; int x = (p)++; int y = *++p; printf("%d %d %d\n", x, y, a[1]);
Show answer
It is defined and prints "4 6 5". The pointer p starts at a[1], so (p)++ yields 4 and changes a[1] to 5, leaving p in place. Then ++p moves p to a[2] and reads 6. Each statement modifies each object once, so there is no undefined behavior.
Exercise
A function g(n) returns 1 when n == 0, and otherwise returns g(n-1) + g(n-1). How many calls does g(3) make in total, and what is the maximum number of frames on the stack at once?
Show answer
The call count satisfies C(n) = 2C(n-1) + 1 with C(0) = 1, so C(n) = 2^(n+1) - 1 and C(3) = 15 calls. The deepest chain is g(3), g(2), g(1), g(0), so at most 4 frames are on the stack at once (n + 1 frames).