Operators
## Learning Objectives
- Master arithmetic operators
- Understand comparison operators
- Learn logical operators
- Work with bitwise operators
## Arithmetic Operators
### Basic Operations
```c
int a = 10, b = 3;
printf("%d\n", a + b); // 13 (addition)
printf("%d\n", a - b); // 7 (subtraction)
printf("%d\n", a * b); // 30 (multiplication)
printf("%d\n", a / b); // 3 (integer division)
printf("%d\n", a % b); // 1 (modulus/remainder)
```
### Division Behavior
```c
// Integer division truncates
printf("%d\n", 10 / 3); // 3, not 3.333
// For floating-point
printf("%f\n", 10.0 / 3.0); // 3.333...
```
### Increment/Decrement
```c
int x = 5;
// Pre-increment/decrement
printf("%d\n", ++x); // 6 (increments first)
printf("%d\n", --x); // 5 (decrements first)
// Post-increment/decrement
printf("%d\n", x++); // 5 (uses value, then increments)
printf("%d\n", x); // 6 (now incremented)
int y = 5;
printf("%d\n", y--); // 5 (uses value, then decrements)
printf("%d\n", y); // 4
```
## Comparison Operators
### Relational Operators
```c
int a = 5, b = 10;
printf("%d\n", a == b); // 0 (false)
printf("%d\n", a != b); // 1 (true)
printf("%d\n", a < b); // 1 (true)
printf("%d\n", a > b); // 0 (false)
printf("%d\n", a <= b); // 1 (true)
printf("%d\n", a >= b); // 0 (false)
```
### Don't Confuse
```c
// = is assignment
// == is comparison
int x = 5;
// if (x = 10) // WRONG! This assigns 10 to x
if (x == 5) // CORRECT! This compares
```
## Logical Operators
### AND, OR, NOT
```c
int a = 1, b = 0; // 1 = true, 0 = false
printf("%d\n", a && b); // 0 (AND - both must be true)
printf("%d\n", a || b); // 1 (OR - one must be true)
printf("%d\n", !a); // 0 (NOT - inverts)
```
### Short-Circuit Evaluation
```c
// && stops if first is false
if (x != 0 && y / x > 2) { // Safe - won't divide by zero
}
// || stops if first is true
if (ptr == NULL || ptr->value > 0) { // Safe - won't dereference NULL
}
```
### Truth Table
| A | B | A && B | A \|\| B | !A |
|---|---|--------|----------|-----|
| 1 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 0 | 0 | 0 | 0 | 1 |
## Assignment Operators
### Simple Assignment
```c
int x = 10;
```
### Compound Assignment
```c
int x = 10;
x += 5; // x = 15 (x = x + 5)
x -= 3; // x = 12 (x = x - 3)
x *= 2; // x = 24 (x = x * 2)
x /= 4; // x = 6 (x = x / 4)
x %= 5; // x = 1 (x = x % 5)
```
### Bitwise Compound Assignment
```c
int flags = 0b1010; // 10 in decimal
flags &= 0b0111; // flags = flags & 0b0111
flags |= 0b0001; // flags = flags | 0b0001
flags ^= 0b0100; // flags = flags ^ 0b0100
```
## Bitwise Operators
### Binary Operations
```c
int a = 5, b = 3; // 101 and 011 in binary
printf("%d\n", a & b); // 1 (AND: 101 & 011 = 001)
printf("%d\n", a | b); // 7 (OR: 101 | 011 = 111)
printf("%d\n", a ^ b); // 6 (XOR: 101 ^ 011 = 110)
printf("%d\n", ~a); // -6 (NOT: ~00000101 = 11111010)
printf("%d\n", a << 1); // 10 (left shift: 101 << = 1010)
printf("%d\n", a >> 1); // 2 (right shift: 101 >> = 10)
```
### Common Uses
```c
// Check if bit is set
int flags = 0b1010;
if (flags & 0b1000) {
printf("Bit 3 is set\n");
}
// Set a bit
flags = flags | 0b0001;
// Toggle a bit
flags = flags ^ 0b0100;
// Extract lower 4 bits
int lower = flags & 0xF;
```
### Signed Right Shift
**Warning:** Right shift on signed negative numbers is implementation-defined.
```c
// For positive numbers: arithmetic shift (fill with 0)
// For negative numbers: often arithmetic shift (fill with 1)
int neg = -4;
printf("%d\n", neg >> 1); // May be -2 or -1 depending on implementation
```
## Comma Operator
```c
// Evaluates left, then right, returns right value
int x = (a = 5, b = 10, a + b); // x = 15
// Common in for loops
for (int i = 0, j = 10; i < 5; i++, j--) {
printf("%d %d\n", i, j);
}
```
## Ternary Operator
### Syntax
```c
// condition ? valueIfTrue : valueIfFalse
int max = (a > b) ? a : b;
int status = (age >= 18) ? 1 : 0;
```
### Nested Ternary
```c
int grade = (score >= 90) ? 'A' :
(score >= 80) ? 'B' :
(score >= 70) ? 'C' :
(score >= 60) ? 'D' : 'F';
```
## sizeof Operator
```c
int x;
printf("%zu\n", sizeof(int)); // Size of type
printf("%zu\n", sizeof(x)); // Size of variable
printf("%zu\n", sizeof 10); // Size of constant
int arr[10];
printf("%zu\n", sizeof(arr)); // 40 (10 * 4 bytes)
```
## Operator Precedence
### Highest to Lowest
| Priority | Operators |
|----------|-------------------|
| 1 | `()` |
| 2 | `++`, `--`, `!`, `~` |
| 3 | `*`, `/`, `%` |
| 4 | `+`, `-` |
| 5 | `<<`, `>>` |
| 6 | `<`, `>`, `<=`, `>=` |
| 7 | `==`, `!=` |
| 8 | `&` |
| 9 | `^` |
| 10 | `\|` |
| 11 | `&&` |
| 12 | `\|\|` |
| 13 | `?:` |
| 14 | `=`, `+=`, `-=`, etc. |
| 15 | `,` |
### Use Parentheses
```c
// Clear precedence
if ((a > b) && (c < d)) {
}
// Instead of relying on memory
if (a > b && c < d) { // Same, but less clear
}
```
## Summary
- Arithmetic: `+`, `-`, `*`, `/`, `%`
- Increment/Decrement: `++x`, `x++`, `--x`, `x--`
- Comparison: `==`, `!=`, `<`, `>`, `<=`, `>=`
- Logical: `&&`, `||`, `!`
- Assignment: `=`, `+=`, `-=`, etc.
- Bitwise: `&`, `|`, `^`, `~`, `<<`, `>>`
- Ternary: `condition ? true : false`
- sizeof: returns size in bytes
- Use parentheses to clarify precedence
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →