Operators & expressions
10 min lesson
- An operator is a symbol that tells the computer to do something with values, like add them, compare them, or combine them.
- Operators matter because they let you compute results: totals, averages, conditions and decisions all rely on them.
- Think of operators as kitchen tools: + is a spoon that adds ingredients, == is a scale that compares two weights, and && is a rule that says both conditions must be true.
- Arithmetic operators do math: + - * / and % (remainder). They work like normal math but with integer division surprises.
- Comparison operators compare values and give 1 (true) or 0 (false): ==, !=, <, >, <=, >=.
- Logical operators combine conditions: && (and), || (or), ! (not). They also give 1 or 0.
- The computer evaluates expressions using precedence rules, much like math class: * and / before + and -, and parentheses override everything.
- The syntax is: result = a + b * 2; or if (age >= 18 && age < 65) { ... }.
- A realistic use: computing a total price with tax and checking whether a customer qualifies for a discount.
- A surprising detail: integer division drops the decimal part, so 7 / 2 gives 3, not 3.5. Use floats to keep the fraction.
- The example program computes the sum, difference, product and quotient of two integers and shows the integer division trap.
- Use parentheses when in doubt; they make your intent clear and prevent precedence bugs.
- Remember that / between two ints does integer division; cast to double if you need a fraction.
- Use == to compare, not = which assigns.
- Logical operators && and || short-circuit: they stop evaluating as soon as the result is known.
- You are now writing real calculations. Keep experimenting!
#include <stdio.h>
int main(void) {
// Two whole numbers to work with
int a = 7;
int b = 2;
// Basic arithmetic: add, subtract, multiply
printf("Sum: %d\n", a + b);
printf("Difference: %d\n", a - b);
printf("Product: %d\n", a * b);
// Integer division drops the decimal part, so 7 / 2 is 3
printf("Integer division: %d\n", a / b);
// The % operator gives the remainder of the division
printf("Remainder: %d\n", a % b);
// Cast to double to get a real decimal result
double exact = (double)a / (double)b;
// Print with two decimal places
printf("Exact division: %.2f\n", exact);
return 0;
}What happens: Sum: 9 Difference: 5 Product: 14 Integer division: 3 Remainder: 1 Exact division: 3.50
Breaking it down
Variables
int a = 7; int b = 2;Two whole numbers to experiment with. Using small values makes it easy to check the results by hand.
Addition and subtraction
a + b, a - bThe most familiar operators. They compute the sum and the difference of the two numbers.
Multiplication
a * bMultiplies the two numbers. Note the asterisk * is used, not the letter x.
Integer division
a / bBecause both are int, the decimal part is thrown away. 7 / 2 gives 3, not 3.5.
Remainder
a % bGives what is left after division. 7 divided by 2 is 3 with 1 left over, so the result is 1.
Casting to double
(double)a / (double)bConverts both numbers to double before dividing, so the decimal part is kept. Result: 3.50.
Common C operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 3 | 8 |
| - | Subtraction | 5 - 3 | 2 |
| * | Multiplication | 5 * 3 | 15 |
| / | Division | 7 / 2 | 3 (integer) |
| % | Remainder | 7 % 2 | 1 |
| == | Equal to | 5 == 5 | 1 (true) |
| != | Not equal to | 5 != 3 | 1 (true) |
| && | Logical AND | 1 && 0 | 0 (false) |
| || | Logical OR | 1 || 0 | 1 (true) |
| ! | Logical NOT | !0 | 1 (true) |
A helpful analogy
Operators are like tools in a kitchen. + is a spoon that adds ingredients, * is a mixer that combines them faster, / is a knife that splits things, and % is the leftover bits after cutting. Comparison operators are like a scale that tells you which side is heavier.
Nesting rules
- 1.Multiplication, division and remainder happen before addition and subtraction.
- 2.Use parentheses to force a different order or to make your intent obvious.
- 3.Comparison and logical operators have lower precedence than arithmetic, so a + b > c means (a + b) > c.
- 4.The assignment operator = has very low precedence, so write x = a + b; not x = a + b > c.
Common mistakes
Using = instead of == in a comparison
Avoid
if (x = 5) { ... } // assigns 5Use instead
if (x == 5) { ... } // comparesThe single = assigns the value and the result is 5 (true), so the condition always runs. The compiler often warns but not always.
Expecting a decimal from integer division
Avoid
double half = 7 / 2; // stores 3.0Use instead
double half = 7 / 2.0; // stores 3.5Both operands are int, so the division is done in integers before the result is stored. Make one operand a double.
Using % with floating-point numbers
Avoid
double r = 7.5 % 2.0; // compile errorUse instead
double r = fmod(7.5, 2.0); // use fmodThe % operator only works on integers. For floating-point remainder use the fmod function from math.h.
Quick tips
- •When a formula feels tricky, add parentheses to show the order you intend.
- •Print intermediate results to check each step of a calculation.
- •Remember that any nonzero value is true in C; zero is false.
- •Try changing a and b in the example to see how integer division and remainder change.
- •Read compiler warnings about 'assignment in condition'; they often catch the = vs == bug.