C Programming Fundamentals for GATE
C Programming is a foundational topic in the GATE Computer Science & IT syllabus, contributing 4–8 marks every year. Questions heavily test operator precedence, side-effects, evaluation order, pointer arithmetic, recursion, and memory representation.
1. Operator Precedence & Associativity
When an expression contains multiple operators, operator precedence determines which operator is evaluated first. When operators have equal precedence, associativity dictates whether the expression is evaluated from left-to-right (L → R) or right-to-left (R → L).
| Precedence Rank | Operators | Description | Associativity |
|---|---|---|---|
| 1 (Highest) | () [] -> . | Postfix, Subscript, Member Access | Left-to-Right |
| 2 | ++ -- + - ! ~ * & sizeof | Prefix Increment/Decrement, Unary operations, Address-of, Dereference | Right-to-Left |
| 3 | * / % | Multiplicative (Arithmetic) | Left-to-Right |
| 4 | + - | Additive (Arithmetic) | Left-to-Right |
| 5 | << >> | Bitwise Shift Left / Right | Left-to-Right |
| 6 | < <= > >= | Relational Comparisons | Left-to-Right |
| 7 | == != | Equality Comparisons | Left-to-Right |
| 8 | & | Bitwise AND | Left-to-Right |
| 9 | ^ | Bitwise XOR | Left-to-Right |
| 10 | | | Bitwise OR | Left-to-Right |
| 11 | && | Logical AND (Short-circuit) | Left-to-Right |
| 12 | || | Logical OR (Short-circuit) | Left-to-Right |
| 13 | ?: | Conditional (Ternary) | Right-to-Left |
| 14 | = += -= *= /= %= | Assignment Operators | Right-to-Left |
| 15 (Lowest) | , | Comma Operator | Left-to-Right |
Key Takeaway for GATE: Unary operators, Ternary (
?:), and Assignment operators (=) evaluate from Right to Left (R → L). Almost all other operators evaluate from Left to Right (L → R).
2. Modulus Operator (%) Rules
- Integer Types Only: The modulus operator
%operates exclusively on integral types (int,char,long). Floating-point operands (float,double) trigger compile-time errors. - Sign of Result: In C99 and later standards, the result of
a % btakes the sign of the dividend (a):7 % 31-7 % 3-17 % -31-7 % -3-1
3. Truth Values & Short-Circuit Evaluation
In C:
- Any non-zero numeric value or non-null address evaluates to True (
1). 0,'\0'(null character), andNULLevaluate to False (0).
Short-Circuiting Behavior:
- Logical AND (
&&): If the left-hand side operand evaluates to0(False), the right-hand side operand is never executed. - Logical OR (
||): If the left-hand side operand evaluates to non-zero (True), the right-hand side operand is never executed.
4. scanf() and printf() Return Values
scanf() Return Value
scanf() returns the number of input items successfully matched and assigned.
int a, b;
int count = scanf("%d %d", &a, &b);
// If user inputs "10 20", count is 2.
// If user inputs "10 abc", count is 1.
printf() Return Value
printf() returns the total number of characters printed to stdout.
int n = printf("GATE2026\n"); // Prints 9 characters (including '\n')
// n will be 9.
Solved GATE Exam Practice Questions
Question 1 (GATE CS)
What is the output of the following C program snippet?
#include <stdio.h>
int main() {
int x = 1, y = 0, z = 5;
int result = x || ++y && ++z;
printf("%d %d %d\n", x, y, z);
return 0;
}
Options:
- (A)
1 1 6 - (B)
1 0 5 - (C)
1 1 5 - (D)
1 0 6
Correct Answer: (B) 1 0 5
Explanation:
- In the expression
x || ++y && ++z,||has lower precedence than&&, but||is evaluated left-to-right with short-circuiting behavior. xis1(True). Since the left operand of||is True, short-circuit evaluation kicks in.- The right operand (
++y && ++z) is skipped entirely. Thus,yremains0andzremains5. - Output:
1 0 5.