Arithmetic Operators in C
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division (integer) | 10 / 3 | 3 (NOT 3.33!) |
| % | Modulo (Remainder) | 10 % 3 | 1 |
| ++ | Increment (add 1) | x++ or ++x | x becomes x+1 |
| -- | Decrement (minus 1) | x-- or --x | x becomes x-1 |
c
#include <stdio.h>
int main() {
int a = 17, b = 5;
printf("a + b = %d\n", a + b); // 22
printf("a - b = %d\n", a - b); // 12
printf("a * b = %d\n", a * b); // 85
printf("a / b = %d\n", a / b); // 3 (integer division!)
printf("a %% b = %d\n", a % b); // 2 (17 = 5*3 + 2)
// Float division — use 1.0 to get decimal result
printf("17.0/5 = %.2f\n", (float)a / b); // 3.40
// Pre vs Post Increment
int n = 10;
printf("n++ = %d\n", n++); // prints 10 THEN increments (now 11)
printf("n = %d\n", n); // 11
printf("++n = %d\n", ++n); // increments FIRST (now 12) THEN prints
printf("n = %d\n", n); // 12
return 0;
}▶ Output
a + b = 22 a - b = 12 a * b = 85 a / b = 3 a % b = 2 17.0/5 = 3.40 n++ = 10 n = 11 ++n = 12 n = 12
Pre-increment vs Post-increment
| Operation | When Increment Happens | Effect on Expression |
|---|---|---|
| ++x (pre) | BEFORE using in expression | Expression sees new value |
| x++ (post) | AFTER using in expression | Expression sees original value |
⚠ Warning: Integer division:
17 / 5 = 3, NOT 3.4! C drops the decimal part. To get 3.4, cast to float first: (float)17 / 5