Back to Study Material GATE Exam

C Programming Language

C Programming is a cornerstone topic in the GATE CS syllabus, typically contributing 4 to 6 marks every year. A solid understanding of C is essential not o...

C Programming Language

Foundations of C for GATE CS

C Programming is a cornerstone topic in the GATE CS syllabus, typically contributing 4 to 6 marks every year. A solid understanding of C is essential not only for the programming section but also because many data structures and algorithm questions expect you to trace or write C-style code. GATE tests C across areas such as pointers, arrays, functions, recursion, memory layout, and undefined behaviour.

The first concept to master is the memory model. Variables in C live in one of four regions: stack (local variables), heap (dynamic allocation via malloc/calloc), data segment (global and static variables), and code segment (instructions). Understanding this layout helps you predict how pointer arithmetic and array indexing behave. For example, when an array int a[5] is declared locally, a is a pointer to the first element on the stack, and a+2 points to the element at index 2, computed as base address plus 2 multiplied by sizeof(int).

Pointer dereferencing and pointer to pointer are favourite GATE topics. Consider int x = 10; int *p = &x; int **q = &p;. Evaluating **q gives 10, while *q gives the address of x. Questions often chain multiple levels of indirection to test whether you can track addresses correctly.

Recursion in C frequently appears in the form of tree traversal or mathematical functions. A key tip is to track the activation stack. For a recursive factorial function, fact(4) calls fact(3), which calls fact(2), and so on, building stack frames until the base case returns. GATE may ask you to count the total number of function calls or compute stack depth.

Bitwise operations such as AND, OR, XOR, left shift, and right shift are tested regularly. A common trick: x & (x-1) clears the lowest set bit of x. Another: x >> n divides x by 2 raised to n for non-negative integers.

For GATE preparation, practise tracing the output of short C programs involving pointer arithmetic, pre/post increment operators, and function call sequences. Many 1-mark questions hinge on operator precedence: remember that the unary operators and function call operator bind more tightly than arithmetic operators, and that assignment is right-associative.

Key Syntax Pitfalls and Output Tracing

A large portion of GATE C questions ask you to trace program output rather than write code from scratch. To succeed at this, you must internalise the rules of sequence points, operator precedence, and undefined behaviour. The expression i = i++ is undefined in C because i is modified and read between two sequence points without a well-defined ordering. GATE setters do include such traps, and the correct answer is often "undefined behaviour" or a specific value that requires careful rule application.

String handling in C is another recurring area. Strings are null-terminated character arrays. The function strlen counts characters up to but not including the null terminator, while sizeof on a string literal includes the null character. Mixing these two is a classic source of off-by-one errors. If char s[] = "gate"; then strlen(s) is 4 and sizeof(s) is 5.

Structure and union layout questions test your understanding of memory alignment and padding. A struct with a char followed by an int will typically be padded so the int starts at a 4-byte boundary, making the struct 8 bytes rather than 5. A union, on the other hand, allocates space equal to its largest member and overlaps all fields.

Dynamic memory functions malloc, calloc, realloc, and free are tested in the context of memory leaks and dangling pointers. malloc allocates uninitialized memory, while calloc zero-initialises. Calling free on a pointer twice leads to undefined behaviour. GATE questions sometimes show code that frees a pointer but then dereferences it, asking whether the code is valid.

A worked example: given int a[] = {1, 2, 3, 4, 5}; int *p = a + 2; printf("%d", *(p - 1));, the output is 2. The pointer p starts at index 2 (value 3), and p minus 1 moves back one integer, landing at index 1 (value 2).

To maximise marks in this section, solve at least 50 GATE PYQs on C programming. Focus on questions from 2010 onwards. Pay special attention to questions involving pointer casting, void pointers, and function pointers, as these appear with increasing frequency in recent papers.

Functions, Storage Classes, and Preprocessor Directives

Functions in C support pass-by-value semantics. When you pass a variable to a function, the function receives a copy, and modifications inside the function do not affect the caller. To modify the caller's variable, you must pass its address and dereference the pointer inside the function. GATE questions often present a swap function and ask you to determine whether the values actually get swapped based on how arguments are passed.

Storage classes determine the lifetime and visibility of variables. The four storage classes are auto, register, static, and extern. Local variables are auto by default and live only for the duration of the function call. A static local variable retains its value across calls, which is a commonly tested behaviour. Example: a function that increments and returns a static counter will return 1 on the first call, 2 on the second, and so on, regardless of how many times the function is invoked from different call sites.

The extern keyword declares a variable without defining it, allowing multiple source files to share a global variable. Questions on multi-file programs sometimes ask whether a program will compile and link correctly given a set of extern declarations and definitions.

The C preprocessor runs before compilation and handles directives beginning with the hash symbol. The macro define replaces text literally, which can cause subtle bugs. For instance, define SQUARE(x) x*x evaluates SQUARE(2+3) as 2+3*2+3, which equals 11, not 25. The fix is to wrap arguments in parentheses: define SQUARE(x) ((x)*(x)).

Conditional compilation using ifdef, ifndef, and endif is used to write platform-specific code and prevent header file double inclusion using include guards. GATE may ask whether a piece of code compiles or which branch executes given a set of defined macros.

Command-line arguments through argc and argv allow programs to receive input at launch. argc holds the count of arguments including the program name, and argv is an array of strings. A question might show main(int argc, char *argv[]) and ask the value of argc when the program is invoked with two additional arguments: the answer is 3.

Practise writing and tracing small C programs to build the mental model needed for GATE. Timed practice under exam conditions, especially on 1-mark output-tracing questions, will improve speed and accuracy significantly.