Skip to main content

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 RankOperatorsDescriptionAssociativity
1 (Highest)() [] -> .Postfix, Subscript, Member AccessLeft-to-Right
2++ -- + - ! ~ * & sizeofPrefix Increment/Decrement, Unary operations, Address-of, DereferenceRight-to-Left
3* / %Multiplicative (Arithmetic)Left-to-Right
4+ -Additive (Arithmetic)Left-to-Right
5<< >>Bitwise Shift Left / RightLeft-to-Right
6< <= > >=Relational ComparisonsLeft-to-Right
7== !=Equality ComparisonsLeft-to-Right
8&Bitwise ANDLeft-to-Right
9^Bitwise XORLeft-to-Right
10|Bitwise ORLeft-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 OperatorsRight-to-Left
15 (Lowest),Comma OperatorLeft-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​

  1. Integer Types Only: The modulus operator % operates exclusively on integral types (int, char, long). Floating-point operands (float, double) trigger compile-time errors.
  2. Sign of Result: In C99 and later standards, the result of a % b takes the sign of the dividend (a):
    • 7 % 3 →\rightarrow 1
    • -7 % 3 →\rightarrow -1
    • 7 % -3 →\rightarrow 1
    • -7 % -3 →\rightarrow -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), and NULL evaluate to False (0).

Short-Circuiting Behavior:​

  • Logical AND (&&): If the left-hand side operand evaluates to 0 (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.
  • x is 1 (True). Since the left operand of || is True, short-circuit evaluation kicks in.
  • The right operand (++y && ++z) is skipped entirely. Thus, y remains 0 and z remains 5.
  • Output: 1 0 5.