C Language
3. Operators & ExpressionsBeginner10 min read

Operators, Expressions & Precedence in C

Deep dive into Arithmetic, Relational, Logical, Bitwise, Assignment, Ternary operators, and the Operator Precedence table.

Key Takeaways & Core Concepts
  • Arithmetic operators include +, -, *, /, and modulus % (integers only).
  • Prefix (++x) increments before value usage; postfix (x++) evaluates current value first.
  • Logical operators (&&, ||) use short-circuit evaluation.
  • Bitwise operators (&, |, ^, ~, <<, >>) manipulate data at the individual bit level.
  • Ternary operator condition ? exp1 : exp2 provides concise conditional branching.

Major Operator Categories in C

Operators tell the compiler to perform specific mathematical, relational, or logical manipulations:

  1. 1
    Arithmetic: +, -, *, /, % (remainder/modulo). Note: % only works with integer operands.
  2. 2
    Relational: == (equal), != (not equal), >, <, >=, <=. Evaluate to 1 (true) or 0 (false).
  3. 3
    Logical:
  • && (AND): Returns 1 if both operands are non-zero.
  • || (OR): Returns 1 if at least one operand is non-zero.
  • ! (NOT): Inverts boolean truth value.
  1. 4
    Bitwise:
  • & (AND), | (OR), ^ (XOR), ~ (NOT / 1's complement).
  • << (Left shift by $n$ bits, multiplies integer by $2^n$).
  • >> (Right shift by $n$ bits, divides integer by $2^n$).
  1. 5
    Conditional / Ternary: condition ? expr1 : expr2;
Beware: Undefined Behaviour with Multiple Modifications

Writing `x = x++ + ++x;` or `printf("%d %d", x++, x++);` causes undefined behaviour in C because there is no sequence point between modifications. Never modify the same variable twice within a single expression!

Bitwise Operators & Operator Precedence

Bitwise operations are critical in systems programming, embedded firmware, cryptography, and network packet parsing.

Bitwise Operations & Bitmasking in C
1#include <stdio.h>
2
3int main(void) {
4 unsigned char a = 0b00001100; // 12 in decimal
5 unsigned char b = 0b00011001; // 25 in decimal
6
7 printf("a & b = %d\n", a & b); // Bitwise AND: 0b00001000 = 8
8 printf("a | b = %d\n", a | b); // Bitwise OR: 0b00011101 = 29
9 printf("a ^ b = %d\n", a ^ b); // Bitwise XOR: 0b00010101 = 21
10 printf("~a = %d\n", (unsigned char)~a); // Inversion: 243
11 printf("a << 2 = %d (12 * 4)\n", a << 2); // Left shift by 2: 48
12 printf("b >> 1 = %d (25 / 2)\n", b >> 1); // Right shift by 1: 12
13
14 // Ternary operator example
15 int maxVal = (a > b) ? a : b;
16 printf("Maximum of a and b is: %d\n", maxVal);
17
18 return 0;
19}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
19 lines
Note: Bitwise operations execute in 1 CPU cycle, making them the fastest operations on modern hardware.
Practice & AssessmentC Programming Complete Course

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 2 correct(0 attempted)

1

What is the result of 17 % 5 in C?

Beginner
2

What will (8 >> 2) evaluate to in C?

Intermediate