GATE 2018 C Programming Questions: Topic Breakdown and Solving Lessons
Remembering an output is not enough. Trace four GATE 2018 C questions to see which variables change, when calls count, and where loops stop.
KnowledgeGate Team
Exam prep & CS education

Remembering a program's answer does not tell you which C rule it tests. These four GATE 2018 CS questions give you a topic map and a reusable way to trace unfamiliar code.
What this GATE 2018 C analysis includes
The four C Programming questions cover structures, pointer parameters, recursion and successive loops. Each walkthrough connects the code to the rule that determines its answer.
Use the official historical GATE paper-and-key archive to access the original material. Explore the GATE, PSU and engineering exam resources.
Treat each solution as a method to reuse: identify the object being accessed, separate caller and local variables, count function entries, and test loop boundaries.
Topic distribution across these four C questions
The table covers four questions, one per topic. Use the revision takeaway to choose what to practise after tracing each solution.
Topic | Count | Decisive operation | Revision takeaway |
|---|---|---|---|
Structures and character access | 1 | Cast, then offset | Check layout and formatting |
Pointer parameters and swapping | 1 | Assign through | Separate caller and callee |
Recursion and call counting | 1 | Divide | Count entries, not products |
Successive halving loops | 1 | Divide by 2 twice in phases | Trace each stopping condition |
Character access is short but assumption-sensitive; swapping requires two scopes; recursion requires a call trace; and loops require separate phase counts.
Structures: read characters through the cast pointer
The GATE 2018 structure question uses these statements:
struct Ournode { char x, y, z; };
struct Ournode p = {'1', '0', 'a'+2};
struct Ournode *q = &p;
printf("%c, %c", *((char*)q+1), *((char*)q+2));Under the paper's assumed layout and character encoding, p.x='1', p.y='0' and p.z='c'. Casting q to char* makes the offsets advance in character-sized units. Offset +1 selects '0'; offset +2 selects 'c'.
The format %c prints a character without surrounding quote marks. The intended answer is (a), 0, c. The expression 'a'+2 is evaluated during initialisation, so a+2 is not printed literally.
Keep the assumptions attached to this answer: ISO C does not guarantee universally padding-free structures or consecutive alphabetic character codes. Resolve stored values before reading through the cast pointer.
The exam's intended trace treats the three char members as consecutive bytes. That assumption is the reason the cast offsets line up with y and z; it is separate from the arithmetic that turns 'a'+2 into 'c'.
Pointer parameters: why only the second swap survives

In the GATE 2018 pointer-swap question, the caller starts with str1 -> "Hi" and str2 -> "Bye".
void fun1(char *s1, char *s2) {
char *tmp = s1;
s1 = s2;
s2 = tmp;
}
void fun2(char **s1, char **s2) {
char *tmp = *s1;
*s1 = *s2;
*s2 = tmp;
}Calling fun1(str1, str2) copies the two pointer values into local parameters. After the swap, local s1 points to "Bye" and local s2 to "Hi". Neither assignment targets a caller variable. The first print therefore produces Hi Bye , including its trailing space.
Calling fun2(&str1, &str2) copies addresses of the caller's pointer variables. Now *s1 designates str1, and *s2 designates str2. The temporary preserves the original "Hi" pointer while the caller bindings swap. The second print produces Bye Hi.

Combined output: (a), Hi Bye Bye Hi. C passes arguments by value in both calls; copied addresses permit indirect mutation. The strings themselves never change. Practise this separation with Pointers in C for GATE: Memory Diagrams and Output Traces.
At every assignment, ask whether the left side is a local pointer variable or a dereferenced address. fun1 changes only its local variables, whereas fun2 writes through addresses that still identify the caller's pointer variables. This two-column check prevents a pointer swap from being mistaken for string mutation.
Recursion: count entries before evaluating returned powers
The recursion question increments counter on every entry to calc(a,b). It returns a*a*a when b==3; otherwise it calls calc(a,b/3) once, stores the result in c, and returns c*c*c.
For calc(4,81), trace the entries:
Entry | Call | Counter |
|---|---|---|
1 |
| 1 |
2 |
| 2 |
3 |
| 3 |
4 |
| 4 |
The intended call-count answer is 4. Multiplying c three times does not make three recursive calls.
The changing argument is 81, then 27, then 9, then 3. There is one call for each value in that chain, so the counter records four entries before the base case returns. The exponentiation-like unwinding changes the returned value, but it does not add entries to the trace.
On unwinding, the conceptual values are 4³, 4⁹, 4²⁷ and 4⁸¹. Signed int multiplication overflows on conventional implementations, causing undefined behaviour. Thus 4 is the answer under the exam's call-count abstraction, not a portable runtime-output guarantee.
Two halving loops: follow the boundary all the way to one
The halving-loop question assumes a 64-bit unsigned long int. Its input is mathematical 2⁴⁰, expressible as 1UL << 40 under that assumption. In C, 2 ^ 40 means XOR, not exponentiation.
The first loop starts i=n, increments j while i>1, and halves i after each iteration. It enters at 2⁴⁰, 2³⁹, ..., 2¹: exactly 40 iterations, so j=40.
The second loop increments sum while j>1, halving j after each iteration:
Entering | 40 | 20 | 10 | 5 | 2 |
|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 |
Integer division takes 5 to 2, then 2 to 1. The condition fails at 1, giving (b), 5. These loops are successive, not nested. Use C Programming for GATE: A 6-Week Topic-by-Topic Plan to organise practice around such boundary mistakes.
A compact way to check the second loop is to list its entering values rather than estimate a logarithm: 40, 20, 10, 5 and 2. There are five successful tests, and the next value is 1, where j>1 is false. Keeping the two loop counters separate also prevents multiplying their iteration counts.
Turn the 2018 mistakes into a focused revision session
Try this suggested 40-minute study session:
10 minutes: draw caller and local pointer bindings.
10 minutes: trace recursion entries without expanding powers.
10 minutes: record loop condition, body and update separately.
10 minutes: retry your weakest item without its answer.
Record one failed rule per item, not just the correct option. For example, “Swapped local parameters but changed the caller in the diagram” tells you exactly what to repair. If these traces expose gaps in the underlying concepts, rebuild them with the C Programming course before attempting another timed set.
Keep learning
GATE 2017 C Programming Questions: Topic Breakdown and Worked Traces
Trace the values that decide the answer: copied pointer values, persistent static counts and recursive calls that never progress. This selected 2017 question set turns those mistakes into a focused revision session.
GATE 2013 C Programming Questions: Topic Breakdown and Worked Traces
Track one shared variable through recursion, then catch repeated character replacements. Three selected GATE 2013 questions show how to diagnose mistakes with state tables.
GATE 2014 C Programming Questions: Topic Analysis and Worked Traces
Trace seven selected GATE 2014 C questions to understand pointer input, double swaps, search boundaries and integer arithmetic. Use the mistakes to guide a focused revision session.
GATE 2012 C Programming Questions: Switch and Storage-Class Analysis
Knowing C syntax is not enough when execution falls through a switch or a local variable survives a call. Trace three GATE CS 2012 C questions and compare the storage rules behind their outputs.





