GateAI

GATE CSE Programming in C PYQs

High yield44.9% of Programming and Data Structures

In GATE CS, Programming in C is tested extensively through rigorous code tracing, pointer mechanics, and fundamental semantics rather than high-level syntax tri… Read the guide

Questions
74
Asked
since 2011
Total marks
111
Marks / paper
4.3

74 GATE CSE Programming in C PYQs with solutions, from Programming and Data Structures. Browse all GATE CSE PYQs.

Questions

Every official paper that asked this topic, newest first.

What GATE tests in Programming in C

Concepts, patterns, formulas and traps distilled from past papers.

In GATE CS, Programming in C is tested extensively through rigorous code tracing, pointer mechanics, and fundamental semantics rather than high-level syntax trivia. Questions emphasize pointer arithmetic and multidimensional array decay, storage classes (especially static local variables), evaluation order, side effects of increment/decrement operators, parameter passing mechanisms (pass-by-value vs pass-by-reference), and recursive program state tracking. The topic features a nearly balanced split between NAT and MCQ items across 1-mark and 2-mark categories, demanding precise, bug-free execution traces without a compiler.

Core concepts

  • Pointer arithmetic and scale factors based on base type
  • Multidimensional array decaying into pointers (pointer to array vs array of pointers)
  • Storage classes (auto, register, static, extern) and lifetime vs scope
  • Pre-increment vs post-increment evaluation and sequence point rules
  • Short-circuit evaluation of logical AND (&&) and logical OR (||)
  • Pass-by-value vs pass-by-reference/pointer mechanics
  • Recursive call stack tracking with local vs static state retention
  • Dynamic memory management (malloc, free, memory leaks, dangling pointers)
  • String literals, ASCII arithmetic, character pointers, and null termination ('\0')
  • Bitwise operators (&, |, , >) and binary number manipulation
  • Switch-case fall-through behavior without break statements
  • Recursive function execution and call stack reduction
  • Base conditions and termination checks
  • Implementation of Euclidean algorithm via repeated subtraction in C
  • Return value propagation across nested call frames

Recurring question patterns

  • Pointer Arithmetic and Multidimensional Array Indexing

    Commonmixed2 marks2026, 2025, 2024, 2022

    Evaluating complex pointer expressions involving 1D/2D/3D arrays, array decaying, pointer subtraction, array subscript commutativity (i[p]≡p[i]i[p] \equiv p[i]), and double dereferencing (∗(∗(a+i)+j)*(*(a+i)+j)).

  • Storage Classes, Variable Scope, and Shadowing

    CommonNAT2 marks2026, 2023, 2020, 2019

    Tracing code containing static local variables retaining state across multiple recursive or iterative function calls, global vs local variable shadowing, and lifetime vs scope distinctions.

  • Parameter Passing and Pointer Swapping Idioms

    Commonmixed1 marks2026, 2025, 2024, 2019

    Testing the distinction between pass-by-value and pass-by-reference using pointers, including classic traps where pointer formal parameters are reassigned locally without dereferencing.

  • Short-Circuit Evaluation, Precedence, and Side Effects

    Commonmixed1 marks2024, 2022, 2021, 2017

    Evaluating expressions involving logical operators (&&, ||) where side-effects like pre/post increment (++i, i++) or assignment are skipped due to short-circuiting, or custom operator precedence tables.

  • Loop Invariants, Algorithm Semantics, and Verification

    OccasionalMCQ2 marks2025, 2024, 2017, 2016

    Formulating correct pre/post-conditions, identifying loop invariants (e.g., division by repeated subtraction, binary exponentiation), or filling missing loop termination/update guards.

  • String Manipulation and Null Termination Pitfalls

    Occasionalmixed1 marks2025, 2024, 2017, 2015

    Tracing pointer-based string copies or scans that miss copying the null terminator '\0', unsigned size_t differences producing underflow, or ASCII arithmetic offsets.

  • Dynamic Memory Allocation and Memory Layout

    OccasionalMCQ1 marks2026, 2021, 2017

    Identifying memory leaks, dangling pointers, uninitialized heap memory accesses, or matching C declarations with memory segments (Stack, Heap, Data/BSS).

  • Mathematical Algorithm Trace / Output Evaluation

    CommonNAT1 marks2021

    A C recursive function implementing a fundamental mathematical operation (e.g., Euclidean GCD via repeated subtraction) is given with multiple base conditions and recursive steps. Candidates must trace the execution or identify the closed-form computation to determine the return value for given arguments.

Formulas GATE uses

Pointer Subtraction

p2−p1=Address(p2)−Address(p1)sizeof(∗p1)p_2 - p_1 = \frac{\text{Address}(p_2) - \text{Address}(p_1)}{\text{sizeof}(*p_1)}

Used when computing the numerical result of subtracting two pointers pointing to elements of the same array.

2D Array Element Address Calculation

Address(A[i][j])=Base(A)+(i×N+j)×sizeof(type)\text{Address}(A[i][j]) = \text{Base}(A) + (i \times N + j) \times \text{sizeof}(\text{type})

Used for mapping multi-dimensional array indices to flat memory offsets in row-major layout.

Array Subscript Equivalence

A[i]≡∗(A+i)≡∗(i+A)≡i[A]A[i] \equiv *(A + i) \equiv *(i + A) \equiv i[A]

Used to resolve commutative and unorthodox array indexing expressions in C snippets.

Cyclomatic Complexity

V(G)=P+1V(G) = P + 1

Used to calculate McCabe's cyclomatic complexity where PP is the count of predicate/decision conditions in the control flow.

Integer Division Loop Invariant

x=q⋅y+r∧0≤r<yx = q \cdot y + r \quad \wedge \quad 0 \le r < y

Used to verify formal loop invariants in repeated subtraction integer division routines.

Euclidean Subtraction GCD

gcd⁡(x,y)={xif x=ygcd⁡(x−y,y)if x>ygcd⁡(x,y−x)if y>x\gcd(x, y) = \begin{cases} x & \text{if } x = y \\ \gcd(x - y, y) & \text{if } x > y \\ \gcd(x, y - x) & \text{if } y > x \end{cases}

Used when analyzing recursive functions that reduce two numbers by repeated subtraction until equality or 1 is reached.

Examiner traps in Programming in C

  • Assuming pointer subtraction returns byte difference rather than the number of typed elements.
  • Forgetting switch-case fall-through when break statements are omitted.
  • Evaluating the right operand of && or || when the left operand already determines the result (short-circuit skipping side-effects like i++).
  • Confusing (*p)++ (incrementing the dereferenced value) with *p++ (dereferencing current address then advancing pointer).
  • Forgetting that static local variables in recursive functions retain cumulative updates across all activation records.
  • Overlooking missing null terminators ('\0') in manual string copying loops, causing trailing characters to persist in memory.
  • Failing to account for unsigned arithmetic wrap-around when subtracting unsigned types like sizet from strlen().
  • Thinking a function that takes pointer parameters swaps them in main when the function swaps pointer values locally instead of dereferenced targets.
  • Simulating all subtraction steps manually one-by-one rather than recognizing the modulo/GCD invariant, leading to arithmetic mistakes or time loss.
  • Overlooking premature termination base cases (e.g., `x == 1 || y == 1`).
  • Off-by-one or parameter swapping errors during the recursive calls.

How questions are asked

  • Exact output tracing of C code snippets (NAT/MCQ)
  • Return value determination of recursive functions with static variables (NAT)
  • Identification of missing loop invariant, condition, or expression (MCQ/MSQ)
  • Pointer arithmetic and multidimensional array evaluation (NAT/MCQ)
  • Short-circuit operator and side-effect expression evaluation (MCQ/NAT)
  • Memory allocation behavior analysis (memory leak, segmentation fault, dangling pointer) (MCQ)
  • Cyclomatic complexity computation of C control structures (NAT)
  • Evaluate return value of a recursive C function for specific input arguments

Paper trends

  • Shift towards Numerical Answer Type (NAT) questions requiring exact printed output calculations rather than multiple-choice guessing.

    2026, 2025, 2023, 2021, 2020, 2019, 2018, 2017, 2015

  • Introduction of non-standard operator precedence tables where candidates must compute expression values based on customized grammar rules rather than default C precedence.

    2024

  • Inclusion of Multi-Select Questions (MSQ) examining algorithmic invariants, asymptotic function bounds, and string pointer validity across multiple scenarios.

    2024

  • Increased focus on subtle C standard specifications such as unsigned arithmetic wrap-around with strlen(), uninitialized struct next pointer dereferencing, and block-scope variable shadowing.

    2026, 2025, 2024, 2021, 2017

  • Focus on concise ANSI C code implementing arithmetic algorithms via recursion, testing understanding of control flow and reduction to base cases within NAT format.

    2021

Difficulty notes

Easy questions typically test direct syntax rules, single function calls with pass-by-value/pass-by-pointer, basic switch fall-through, or standard 1D pointer dereferencing. Medium questions combine multiple concepts: nested loops with complex pointer arithmetic on 2D/3D arrays, static variables in mutual/deep recursion, short-circuit boolean expressions with post-increment side effects, or unsigned arithmetic traps. Hard/complex questions require formulating algebraic invariants for loops, identifying obscure memory leak conditions, or tracing customized operator precedence grammars.