Operators and Expressions in C and C++
Why does 17 / 5 give 3, not 3.4? Learn how C and C++ evaluate arithmetic, comparison and logical operators, and which one runs first.
- Arithmetic operators, including integer division and %
- Compound assignment (+=, *=) and increment (++)
- Comparisons and && / || that give true or false
>compare last+then adda*multiply firstb2
20
How C and C++ evaluate an expression
The problem
Calculations are the core of most programs, but C and C++ have rules that surprise beginners: dividing two integers drops the fraction, and some operators run before others. Knowing them avoids wrong results that compile without warnings.
Think of it like this
Operators follow rules like the order of operations you learned in school. Multiplication comes before addition, and parentheses decide when you want something else. Integer division is like sharing 17 sweets among 5 children: each gets 3, and 2 are left over.
a / b🍬 Sweets per child: 3a % b🧺 Sweets left over: 2a / 5.0🍰 Cut a cake instead: 3.4 eacha > 10 && b > 10✅ Both conditions must hold: falseOperators combine values into expressions: arithmetic (+ - * / %), assignment (+=), comparison (> ==) and logical operators (&& ||), evaluated by fixed precedence rules.
Important points
a / bwith two ints gives 3, anda % bgives the remainder 2.a / 5.0uses a double operand, so the result keeps the fraction: 3.4.total += 5andtotal *= 2update a variable in place;count++adds 1.- In C, comparisons give 1 or 0; in C++ they are bool, printed as true or false with
std::boolalpha.
When to use it
- Every calculation: totals, averages, remainders and counters.
- Checking even and odd numbers or wrapping indexes with
%. - Combining conditions with
&&and||.
When not to use it
- Long expressions that rely on precedence you have to look up: split them or add parentheses.
- Clever one-liners with
++inside other expressions: write the steps separately.
C and C++ code examples
#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); /* integer division drops the fraction */
printf("a %% b = %d\n", a % b); /* remainder */
printf("a / 5.0 = %.1f\n", a / 5.0); /* one double operand gives a double */
int total = 10;
total += 5;
total *= 2;
printf("total = %d\n", total);
int count = 0;
count++;
printf("count = %d\n", count);
/* Comparisons and logical operators give 1 (true) or 0 (false). */
printf("a > b: %d, a == 17: %d\n", a > b, a == 17);
printf("a > 10 && b > 10: %d\n", a > 10 && b > 10);
printf("a > 10 || b > 10: %d\n", a > 10 || b > 10);
return 0;
}
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
a / 5.0 = 3.4
total = 30
count = 1
a > b: 1, a == 17: 1
a > 10 && b > 10: 0
a > 10 || b > 10: 1
#include <iostream>
int main() {
int a = 17, b = 5;
std::cout << "a + b = " << a + b << '\n';
std::cout << "a - b = " << a - b << '\n';
std::cout << "a * b = " << a * b << '\n';
std::cout << "a / b = " << a / b << '\n'; // integer division drops the fraction
std::cout << "a % b = " << a % b << '\n'; // remainder
std::cout << "a / 5.0 = " << a / 5.0 << '\n'; // one double operand gives a double
int total = 10;
total += 5;
total *= 2;
std::cout << "total = " << total << '\n';
int count = 0;
count++;
std::cout << "count = " << count << '\n';
// bool values print as true/false with std::boolalpha.
std::cout << std::boolalpha;
std::cout << "a > b: " << (a > b) << ", a == 17: " << (a == 17) << '\n';
std::cout << "a > 10 && b > 10: " << (a > 10 && b > 10) << '\n';
std::cout << "a > 10 || b > 10: " << (a > 10 || b > 10) << '\n';
}
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
a / 5.0 = 3.4
total = 30
count = 1
a > b: true, a == 17: true
a > 10 && b > 10: false
a > 10 || b > 10: true
C and C++ comparison
Dividing two ints drops the fraction, so 17 / 5 is 3; make one operand a double to keep it. Comparisons and && / || produce 1 or 0 in C and true or false in C++. std::boolalpha prints the C++ results as words.
C exposes small procedural APIs and makes representation details explicit.
C++ retains the low-level model while adding safer library types and abstractions.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Add invalid and boundary-value inputs.
- Compile with -Wall -Wextra -Wpedantic.
- Move reusable declarations into a header and implementation file.
Operators in C and C++
+, -, *, /, % | Arithmetic; / on two integers drops the decimals, % is the remainder |
17 / 5, 17 % 5 | Integer division and remainder: 3 and 2 |
17.0 / 5 | Division with a double operand: 3.4 |
x += 2, x *= 3 | Compound assignment |
++x, x++ | Increment (prefix returns the new value, postfix the old one) |
==, !=, <, >, <=, >= | Comparison |
&&, ||, ! | Logical operators |
&, |, ^, ~, <<, >> | Bitwise operators |
(double)a / b | Cast before dividing to keep the decimals (static_cast<double>(a) in C++) |
sizeof(int) | Size of a type in bytes |
Common mistakes with operators
Integer division where you wanted decimals
Dividing two ints gives an int, even when the result is stored in a double.
int passed = 7, total = 9;
double rate = passed / total; /* 0.0 */
int passed = 7, total = 9;
double rate = (double)passed / total; /* 0.777... */
Using ^ for powers
^ is bitwise XOR. Use pow() from <math.h> (or <cmath>) for powers.
int square = 3 ^ 2; /* 1, not 9 */
double square = pow(3, 2); /* 9.0 */
Using & or | instead of && or ||
& and | are bitwise and always evaluate both sides; && and || stop as soon as the answer is known.
if (p != NULL & p->value > 0) { /* p->value is read even when p is NULL */ }
if (p != NULL && p->value > 0) { /* safe: stops when p is NULL */ }
Frequently asked questions
What does % do in C?
% returns the remainder of integer division, so 17 % 5 is 2. It is often used to check for even numbers: n % 2 == 0.
Why is 5 / 2 equal to 2 in C?
Both operands are integers, so C performs integer division and drops the fractional part. Make one operand a double, for example 5.0 / 2 or (double)a / b, to get 2.5.
What is the difference between ++i and i++?
Both add 1 to i. ++i gives the new value and i++ gives the old value, which matters only when the expression's value is used, as in x = i++.
How do I calculate a power in C or C++?
Use pow(base, exponent) from <math.h> or <cmath>. For small integer powers, multiplying (x * x) is simpler and exact.
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Celsius to Fahrenheit Convert a temperature from Celsius to Fahrenheit. Easy
- Simple Interest Find the total amount after simple interest. Easy