RAPID EXAM REFERENCE · 1-PAGE SUMMARIES

Programming in C Cheat Sheets

One page per topic for when you have 10 minutes before an exam: the core rule, the formulas, and the traps.

Operator Precedence, Short-Circuit & Storage Classes

Operator Precedence, Associativity & Short-Circuit Evaluation

Open Full Lesson →

Unary, Ternary, and Assignment operators associate Right-to-Left; logical && and || short-circuit evaluation from left to right, bypassing right operands when outcome is determined.

Must-Remember Principles:
  • —Precedence order: Postfix > Unary (R-to-L) > Multiplicative > Additive > Shift > Relational > Equality > Bitwise > Logical > Ternary (R-to-L) > Assignment (R-to-L) > Comma.
  • —expr1 && expr2: If expr1 is 0, expr2 is NOT evaluated.
  • —expr1 || expr2: If expr1 is non-zero, expr2 is NOT evaluated.
  • —*p++ evaluates as *(p++): dereferences old p, then increments pointer.
  • —(*p)++ dereferences p and increments the target integer value.
Critical Pitfalls & Exam Traps:
  • ×Do not assume bitwise & has higher precedence than equality ==; relational and equality operators have HIGHER precedence than bitwise operators in C!
Operator Precedence, Short-Circuit & Storage Classes

Storage Classes, Variable Scope & Linkage Dynamics

Open Full Lesson →

Static variables are initialized once at startup in the Data segment (default 0); register variables cannot be referenced with &; extern provides external linkage.

Must-Remember Principles:
  • —auto: Stack segment; local lifetime; garbage default.
  • —static local: Data/BSS segment; program lifetime; initialized once (0 default).
  • —static global: Internal linkage (visible only in declaring file).
  • —register: Suggested CPU register; taking address &x is illegal.
  • —extern: External linkage declaration; no memory allocated until definition.
Critical Pitfalls & Exam Traps:
  • ×Do not attempt to pass the address of a register variable to scanf; scanf requires an address, causing a compile-time error.
Pointers, Array Decay & Function Pointers

Pointer Arithmetic & Multi-Dimensional Array Subscripts

Open Full Lesson →

Adding k to pointer p adds k * sizeof(*p) bytes; A[i] is identical to *(A + i); &A points to the entire array while A decays to a pointer to element 0.

Must-Remember Principles:
  • —Address(p + k) = Address(p) + k * sizeof(*p).
  • —A[i][j] == *(*(A + i) + j).
  • —sizeof(A) yields total array bytes; sizeof(&A) yields pointer size (4 or 8 bytes).
  • —&A + 1 skips the entire array capacity.
Critical Pitfalls & Exam Traps:
  • ×Remember that subtracting two pointers (p2 - p1) yields the number of ELEMENTS between them, NOT the byte count!
Pointers, Array Decay & Function Pointers

Function Pointers, Callbacks & String Memory Layouts

Open Full Lesson →

Function pointer int (*fp)(int) stores code entry address; char *s points to read-only memory while char s[] allocates mutable stack memory.

Must-Remember Principles:
  • —Function pointer syntax: type (*name)(args).
  • —Array of function pointers: type (*arr[N])(args).
  • —char *s = "text": Read-only text segment (modifying causes segmentation fault).
  • —char s[] = "text": Stack-allocated mutable array of size strlen + 1.
Critical Pitfalls & Exam Traps:
  • ×Never forget the trailing null character '\0' when calculating sizeof on string arrays: sizeof("GATE") is 5 bytes, not 4!
Functions, Recursion & The Call Stack

Recursion Tracing, Stack Frames & Static Variables

Open Full Lesson →

Each recursive call pushes an activation record with fresh local variables; static variables reside in the Data segment and persist across all recursive frames.

Must-Remember Principles:
  • —Activation record contains: Parameters, Return Address, Dynamic Link, Local Variables.
  • —Static variable initialized only once; retains accumulated state.
  • —Tail recursion: Recursive call is the very last operation; can be optimized to a loop with O(1) stack space.
Critical Pitfalls & Exam Traps:
  • ×When unwinding expressions like fun(n-1) + x, evaluate static x at the time of addition, NOT at the time fun was originally called.
Functions, Recursion & The Call Stack

Parameter Passing (Value vs Pointer) & Tail Recursion

Open Full Lesson →

C is strictly call-by-value; to mutate caller data, pass pointers; tail recursion allows compiler stack reuse.

Must-Remember Principles:
  • —Call-by-value: Function operates on local copies of parameters.
  • —Swap requires pointers: void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }
  • —Tail recursive functions perform zero operations after the recursive call returns.
Critical Pitfalls & Exam Traps:
  • ×Passing an array passes a pointer to element 0; modifying array elements inside a function WILL modify the caller's array!
Structures, Unions & Memory Alignment

Structure Padding, Memory Alignment & Dynamic Allocation

Open Full Lesson →

Members align at offsets divisible by their individual size; total struct size is padded to a multiple of the largest member's size.

Must-Remember Principles:
  • —char (1B): offset % 1 == 0.
  • —short (2B): offset % 2 == 0.
  • —int (4B): offset % 4 == 0.
  • —double (8B): offset % 4 or 8 == 0.
  • —Total size % max_member_size == 0 (trailing padding added).
  • —Member reordering tip: Sort members by descending size to minimize padding bytes.
Critical Pitfalls & Exam Traps:
  • ×Do not forget trailing padding: a structure ending at byte 10 with an int member will always pad to 12 bytes.
Structures, Unions & Memory Alignment

Unions, Bit-Fields & Dynamic Memory Management

Open Full Lesson →

Union size equals its largest member; free(p) releases heap memory but leaves p as a dangling pointer until assigned NULL.

Must-Remember Principles:
  • —Union: All members share the same starting address.
  • —malloc: Uninitialized garbage memory.
  • —calloc: Zero-initialized memory.
  • —Dangling pointer: Pointer pointing to deallocated memory.
  • —Memory leak: Heap memory allocated without corresponding free.
Critical Pitfalls & Exam Traps:
  • ×Calling free on an unallocated address or double-freeing causes undefined behavior and heap corruption crashes.
Preprocessor Directives, Macros & File I/O

Preprocessor Macros, Token Pasting & Side Effect Traps

Open Full Lesson →

Macros perform pure textual substitution; always parenthesize macro parameters ((x)*(x)); # stringifies and ## pastes tokens.

Must-Remember Principles:
  • —#define SQUARE(x) ((x)*(x)): Parentheses prevent operator precedence errors.
  • —#param converts param to string literal "param".
  • —token1 ## token2 concatenates into token1token2.
  • —#ifdef / #ifndef / #endif: Conditional compilation guards against multiple inclusion.
Critical Pitfalls & Exam Traps:
  • ×Never pass expressions with side effects (like i++) to macros that use their parameter more than once.
Preprocessor Directives, Macros & File I/O

Standard I/O, Format Specifiers & File Streams

Open Full Lesson →

printf returns count of characters printed; scanf returns count of items successfully matched; fseek repositions file pointer.

Must-Remember Principles:
  • —printf return: Number of characters emitted.
  • —scanf return: Number of inputs matched (or EOF on failure).
  • —SEEK_SET (beginning), SEEK_CUR (current position), SEEK_END (end of file).
  • —ftell(fp): Returns current byte offset in file stream.
Critical Pitfalls & Exam Traps:
  • ×Remember scanf requires memory addresses (&var for primitives, but array names without &).