GATE CS - PROGRAMMING & DATA STRUCTURES:Programming Fundamentals

Mastering programming fundamentals concepts and implementation.

Programming Fundamentals for GATE CS

GATE does not ask you to write a full app. It asks you to read short C (or C-like) snippets and say what prints, what the final values are, or how deep the recursion goes. This chapter is that skill — not a tour of if/for syntax.

Programming & Data Structures together are usually the highest-weightage core area on the paper (often roughly 10–15 marks across PDS topics). Fundamentals questions are fewer than trees/graphs, but they are easy marks if you can simulate execution by hand.

What you are really being tested on

  1. Operator and side-effect order — pre- vs post-increment
  2. Pointers and arrays — *(p+1) vs p[1], pointer arithmetic
  3. Call stack — pass-by-value vs pass-by-pointer; recursion depth and return order
  4. Storage — static locals that survive across calls

If you only know Python, practise these in C syntax anyway. The exam language is C-flavoured.

Recursion: draw the stack

int f(int n) {
    if (n <= 1) return 1;
    return n * f(n - 1);
}
// f(3) → 3 * f(2) → 3 * (2 * f(1)) → 3 * (2 * 1) = 6

GATE variants hide the same idea behind Fibonacci, GCD, or “how many calls?” Count activation records, including the base case, unless the question excludes it.

Common trap: assuming tail recursion is optimized away. In GATE, count the calls the code as written would make.

Pass by value vs pointer

void inc_val(int x) { x++; }
void inc_ptr(int *x) { (*x)++; }

int a = 3, b = 3;
inc_val(a);   // a still 3
inc_ptr(&b);  // b becomes 4

Arrays decay to pointers in function parameters, so void g(int a[]) can modify the caller’s elements even though the pointer itself is passed by value.

Side effects: prefer unambiguous questions

int a[] = {1, 2, 3, 4};
int *p = a;
printf("%d
", *p++);  // prints 1, then p points to 2
printf("%d
", *p);    // prints 2

Avoid relying on unsequenced side effects inside a single printf with multiple *p++ arguments — evaluation order is implementation-defined. GATE-safe habit: rewrite into separate statements when you practise.

Static storage

int counter() {
    static int c = 0;
    return ++c;
}
// counter(); counter(); → 1 then 2

Static locals initialize once and keep their value between calls. That is a frequent 1-mark pattern.

What not to grind

  • Memorizing every C standard library function
  • Writing linked-list code from scratch without tracing given code
  • Java/new idioms — the paper is overwhelmingly C-style

When this feels solid, move to Arrays and Linked Lists and practise timed questions from the GATE hub.