C Operators
Operators are the symbols that do work on values: add them, compare them, combine conditions. C has many operators; this page covers the ones you use every day.
Arithmetic
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | add | 17 + 5 | 22 |
- | subtract | 17 - 5 | 12 |
* | multiply | 17 * 5 | 85 |
/ | divide | 17 / 5 | 3 |
% | remainder | 17 % 5 | 2 |
#include <stdio.h>
int main(void) {
int a = 17, b = 5;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
printf("a / 5.0 = %.2f\n", a / 5.0);
return 0;
}a + b = 22 a - b = 12 a * b = 85 a / b = 3 a % b = 2 a / 5.0 = 3.40
Integer division surprises everyone once: when both numbers are integers, / throws the fraction away, so 17 / 5 is 3. Make one of the numbers a decimal (5.0), or convert it, to get 3.40. The remainder % works only with integers; n % 2 == 0 is the classic test for an even number. Dividing an integer by zero is not allowed: C does not define what happens, and a real program may crash or print garbage. In our editor the program stops with a clear division by zero message. Check the divisor before dividing.
Assignment
= stores a value. The shorthand operators change a variable using its own value: x += 3 means x = x + 3, and the same works for -=, *=, /= and %=.
Increment and decrement
x++ adds 1 to x, x-- subtracts 1. There is a prefix form too (++x): inside a bigger expression x++ gives the old value and ++x the new one. Keep increments on their own line until you are comfortable.
#include <stdio.h>
int main(void) {
int x = 10;
x += 5;
printf("x = %d\n", x);
x *= 2;
printf("x = %d\n", x);
x++;
printf("x = %d\n", x);
int y = x++; // y gets the old value, then x grows
printf("y = %d, x = %d\n", y, x);
return 0;
}x = 15 x = 30 x = 31 y = 31, x = 32
Comparison
Comparisons give 1 for true and 0 for false: == equal, != not equal, <, >, <=, >=. Note the double equals: a single = stores a value, which inside an if is a common bug.
Logical operators
&& (and) is true when both sides are true, || (or) when at least one is, and ! (not) turns true into false. C stops early: in a && b, if a is false, b is not even checked.
#include <stdio.h>
int main(void) {
int age = 15;
printf("age > 12: %d\n", age > 12);
printf("age == 18: %d\n", age == 18);
printf("teenager: %d\n", age >= 13 && age <= 19);
printf("child or senior: %d\n", age < 13 || age > 64);
printf("not adult: %d\n", !(age >= 18));
return 0;
}age > 12: 1 age == 18: 0 teenager: 1 child or senior: 0 not adult: 1
Precedence
As in mathematics, *, / and % are done before + and -, so 2 + 3 * 4 is 14. Comparisons come after arithmetic, && before ||, and assignment last. When in doubt, use brackets: (2 + 3) * 4 is 20, and brackets also make your intention clear to readers.
Practice exercises
- Convert 135 minutes into hours and minutes with
/and%. - Print 1 if a number is even and 0 if it is odd.
- Compute the average of three integers so that the result keeps its decimals.
- Print whether a year is between 2000 and 2099 using
&&.