C Operators
C provides a rich set of operators including arithmetic, relational, logical, bitwise, and assignment operators. Understanding operator precedence and bitwise operations is essential for systems programming.
35 min•By Priygop Team•Last updated: Feb 2026
Operator Categories
- Arithmetic: +, -, *, /, % (modulo). Integer division truncates: 7/2 = 3
- Relational: ==, !=, <, >, <=, >=. Returns 1 (true) or 0 (false)
- Logical: && (AND), || (OR), ! (NOT). Short-circuit evaluation: if left is false, && skips right
- Bitwise: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift). Operate on individual bits
- Assignment: =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
- Increment/Decrement: ++i (pre), i++ (post). Pre returns new value; post returns old value
- Ternary: condition ? true_value : false_value. Compact if/else for expressions
- Comma: (a=1, b=2, a+b). Evaluates left-to-right, returns rightmost value
Operators Code
Example
#include <stdio.h>
int main() {
// Bitwise operators (systems programming essential)
unsigned int a = 0b1010; // 10 in binary
unsigned int b = 0b1100; // 12 in binary
printf("a & b = %d\n", a & b); // AND: 1000 = 8
printf("a | b = %d\n", a | b); // OR: 1110 = 14
printf("a ^ b = %d\n", a ^ b); // XOR: 0110 = 6
printf("~a = %d\n", ~a); // NOT: inverts all bits
// Bit shifting
printf("a << 1 = %d\n", a << 1); // Left shift: 10100 = 20 (multiply by 2)
printf("a >> 1 = %d\n", a >> 1); // Right shift: 0101 = 5 (divide by 2)
// Practical: Set, clear, toggle bits
unsigned int flags = 0;
flags |= (1 << 3); // Set bit 3
flags &= ~(1 << 3); // Clear bit 3
flags ^= (1 << 3); // Toggle bit 3
// Check if bit is set
if (flags & (1 << 3)) {
printf("Bit 3 is set\n");
}
// Pre vs Post increment
int x = 5;
printf("%d\n", ++x); // 6 (increment, then use)
printf("%d\n", x++); // 6 (use, then increment)
printf("%d\n", x); // 7
// Ternary operator
int age = 20;
const char *status = (age >= 18) ? "adult" : "minor";
printf("%s\n", status); // "adult"
return 0;
}