C/C++ Basics

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.

  • Beginner
  • 5 min read
  • 2 runnable examples
What you'll learn
  • Arithmetic operators, including integer division and %
  • Compound assignment (+=, *=) and increment (++)
  • Comparisons and && / || that give true or false
Evaluation order of a + b * 2 > 20
  • >compare last
    • +then add
      • a
      • *multiply first
        • b
        • 2
    • 20
The deepest operator runs first. With a = 17 and b = 5: 5 * 2 = 10, 17 + 10 = 27, 27 > 20 is true.

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.

From sharing sweets to code
a / b🍬 Sweets per child: 3
a % b🧺 Sweets left over: 2
a / 5.0🍰 Cut a cake instead: 3.4 each
a > 10 && b > 10✅ Both conditions must hold: false
Integer division keeps whole parts; one double keeps the fraction.
In one sentence

Operators combine values into expressions: arithmetic (+ - * / %), assignment (+=), comparison (> ==) and logical operators (&& ||), evaluated by fixed precedence rules.

Important points

  1. a / b with two ints gives 3, and a % b gives the remainder 2.
  2. a / 5.0 uses a double operand, so the result keeps the fraction: 3.4.
  3. total += 5 and total *= 2 update a variable in place; count++ adds 1.
  4. 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

C
Run code →
main.c
#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;
}
Get a Run button for this example Result of running the code
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
C++
Run code →
main.cpp
#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';
}
Get a Run button for this example Result of running the code
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

C exposes small procedural APIs and makes representation details explicit.

C++

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 % 5Integer division and remainder: 3 and 2
17.0 / 5Division with a double operand: 3.4
x += 2, x *= 3Compound assignment
++x, x++Increment (prefix returns the new value, postfix the old one)
==, !=, <, >, <=, >=Comparison
&&, ||, !Logical operators
&, |, ^, ~, <<, >>Bitwise operators
(double)a / bCast 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.

Problem
int passed = 7, total = 9;
double rate = passed / total;      /* 0.0 */
Fix
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.

Problem
int square = 3 ^ 2;      /* 1, not 9 */
Fix
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.

Problem
if (p != NULL & p->value > 0) { /* p->value is read even when p is NULL */ }
Fix
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.

All challenges