C++20 sections · 1024 units
Open in Course

Addition and Subtraction

Combining and removing values

The + operator adds two numbers: int total = 7 + 3; stores 10. The - operator subtracts: int diff = 7 - 3; stores 4. Both work identically for integers and floating-point.

You can chain operations: int result = 10 + 5 - 2; evaluates left to right, giving 13. C++ performs addition first, then subtraction, because they have equal precedence. Mixing types produces the wider type.

int a = 5; double b = 2.5; double c = a + b; stores 7.5. If you assign this to int, the decimal truncates to 7.