Operators & Expressions

Master arithmetic, logical, bitwise, and assignment operators. Understand precedence, avoid undefined behavior, and write safe, predictable expressions that compile correctly and run reliably.

Arithmetic Operators

The foundation of computational expressions. Perform mathematical calculations on numerical values.

+

Addition

Adds two numbers together.

int sum = 10 + 5; // 15
-

Subtraction

Subtracts second number from first.

int diff = 20 - 8; // 12
*

Multiplication

Multiplies two numbers.

int prod = 6 * 7; // 42
/

Division

Divides first by second. Integer division truncates.

int div = 20 / 3; // 6
%

Modulus

Returns remainder. Integers only.

int rem = 20 % 3; // 2
++

Increment

Increases value by 1 (Pre or Post).

x++; // Post-increment
arithmetic.c
#include <stdio.h> int main() { int a = 20, b = 6; printf("Addition: %d\n", a + b); // 26 printf("Division: %d\n", a / b); // 3 printf("Modulus: %d\n", a % b); // 2 int x = 5; printf("x++ = %d\n", x++); // Prints 5, then x becomes 6 printf("++x = %d\n", ++x); // x becomes 7, then prints 7 return 0; }

Relational Operators

Compare two values and return boolean result: 1 (true) or 0 (false).

Operator Name Example Result
== Equal to 5 == 5 1 (True)
!= Not Equal 5 != 3 1 (True)
> Greater 10 > 5 1 (True)
< Less 3 < 8 1 (True)
>= Greater/Equal 5 >= 5 1 (True)
⚠ Common Mistake: Confusing == (comparison) with = (assignment). Using if (x = 5) assigns 5 to x instead of checking equality. Always use == for comparisons!

Logical Operators

Combine multiple boolean conditions.

&&

Logical AND

Returns true only if BOTH conditions are true.

if (x > 0 && x < 10)
||

Logical OR

Returns true if AT LEAST ONE condition is true.

if (x < 0 || x > 100)
!

Logical NOT

Inverts the boolean value (True becomes False).

if (!isValid)
logic.c
int age = 25; int hasLicense = 1; // AND: Both must be true if (age >= 18 && hasLicense) { printf("Can drive.\n"); } // OR: One must be true if (age < 18 || !hasLicense) { printf("Cannot drive.\n"); }

Bitwise Operators

Low-level operations on individual bits. Essential for flags and optimization.

&

Bitwise AND

Bits are 1 if both are 1.

5 & 3 -> 1 (0101 & 0011 = 0001)
|

Bitwise OR

Bits are 1 if either is 1.

5 | 3 -> 7 (0101 | 0011 = 0111)
^

Bitwise XOR

Bits are 1 if different.

5 ^ 3 -> 6 (0101 ^ 0011 = 0110)
~

Bitwise NOT

Inverts all bits (0->1, 1->0).

<<

Left Shift

Shifts bits left (Multiply by 2^n).

5 << 1 -> 10
>>

Right Shift

Shifts bits right (Divide by 2^n).

5 >> 1 -> 2

Assignment Operators

Shorthand syntax for modifying variables.

Operator Example Equivalent To
=x = 10Assign 10 to x
+=x += 5x = x + 5
-=x -= 3x = x - 3
*=x *= 2x = x * 2
/=x /= 2x = x / 2
%=x %= 3x = x % 3
&=x &= 5x = x & 5
|=x |= 5x = x | 5
^=x ^= 5x = x ^ 5

Operator Precedence

Order of operations. When in doubt, use parentheses ().

Level Operators Associativity
1() [] . ->Left to Right
2! ~ ++ -- + - * & (Unary)Right to Left
3* / %Left to Right
4+ -Left to Right
5<< >>Left to Right
6< <= > >=Left to Right
7== !=Left to Right
8&Left to Right
9^Left to Right
10|Left to Right
11&&Left to Right
12||Left to Right
13?: (Ternary)Right to Left
14= += -= etc.Right to Left
15,Left to Right

Advanced: Undefined Behavior

Writing valid C means avoiding ambiguous operations that confuse the compiler.

Short-Circuit Evaluation:
For A && B, if A is false, B is never evaluated.
For A || B, if A is true, B is never evaluated.

The Dangerous Pattern

⚠ NEVER DO THIS: i = i++; or arr[i] = i++;
This modifies the same variable multiple times in one sequence point. The result is UNDEFINED and varies by compiler.
unsafe.c
// WRONG - Result Unpredictable int i = 5; i = i++; // RIGHT - Clear Intent int i = 5; i++; // i is now 6 // WRONG - Buffer Overflow char buf[5]; strcpy(buf, "Hello World"); // RIGHT - Safe Copy char buf[20]; strncpy(buf, "Hello", 19);