C++

Arithmetic Operators in C++

Arithmetic operators are the most fundamental tools in C++, allowing a program to perform mathematical calculations. In C++, these operators are symbols that trigger specific mathematical actions on one or more operands.

Theoretically, these operators are divided into two categories based on the number of operands they require: Unary (one operand) and Binary (two operands).

1. The Theory: Operands and Data Types

The behavior of an arithmetic operator depends heavily on the data type of the operands involved. This is a critical theoretical concept in C++:

  • Integer Arithmetic: If both operands are integers, the result is an integer. This is particularly important for division (e.g., $10 / 3$ results in $3$, not $3.33$).

  • Floating-Point Arithmetic: If at least one operand is a float or double, the result is a floating-point number.

  • The Assignment Rule: The result of an arithmetic expression is calculated first, and then it is assigned to the variable on the left of the = operator.

2. Binary Arithmetic Operators

Binary operators require two operands to perform a calculation.

OperatorOperationDescription
+AdditionAdds two operands together.
-SubtractionSubtracts the second operand from the first.
*MultiplicationMultiplies both operands.
/DivisionDivides the numerator by the denominator.
%ModuloReturns the remainder of an integer division.

Deep Dive: The Modulo Operator (%)

The modulo operator is unique because it only works with integers. It is used to find the remainder. For example, $13 \% 5$ is $3$ because 5 goes into 13 twice, with 3 left over.

  • Theory: Modulo is frequently used to check for even/odd numbers ($num \% 2 == 0$) or to keep a value within a certain range (circular arrays).

3. Unary Arithmetic Operators

Unary operators act upon a single operand to modify its value. These are the most commonly used operators in loops.

A. Increment (++)

Increases the value of the operand by 1.

  • Prefix (++x): Increments the value first, then uses it in the expression.

  • Postfix (x++): Uses the current value in the expression first, then increments it.

B. Decrement (--)

Decreases the value of the operand by 1.

  • Prefix (--x): Decrements then uses.

  • Postfix (x--): Uses then decrements.

4. Operator Precedence and Associativity

When multiple operators appear in a single expression (e.g., $x = 5 + 3 * 2$), C++ follows strict mathematical rules to determine which happens first.

  1. Parentheses (): Highest priority. Always calculated first.

  2. Unary Operators ++, --: Second priority.

  3. Multiplicative *, /, %: Third priority.

  4. Additive +, -: Lowest priority among arithmetic operators.

Associativity: If two operators have the same precedence (like * and /), they are calculated from Left to Right.

5. Practical Code Example


#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 3;
    float x = 10.0, y = 3.0;

    cout << "--- Binary Operators ---" << endl;
    cout << "Addition (10 + 3): " << a + b << endl;
    cout << "Integer Division (10 / 3): " << a / b << endl; // Truncates decimal
    cout << "Float Division (10.0 / 3.0): " << x / y << endl;
    cout << "Modulo (10 % 3): " << a % b << endl; // Remainder is 1

    cout << "\n--- Unary Operators ---" << endl;
    int c = 5;
    cout << "Original C: " << c << endl;
    cout << "Postfix C++: " << c++ << " (Shows 5, but C is now 6)" << endl;
    cout << "Prefix ++C: " << ++c << " (Increments 6 to 7, then shows 7)" << endl;

    // Complex Expression: 10 + 3 * 2
    // Multiply first: 3 * 2 = 6. Then add: 10 + 6 = 16.
    int result = a + b * 2;
    cout << "\nResult of 10 + 3 * 2: " << result << endl;

    return 0;
}

6. Summary Table for EMBLAb

Operator TypeSymbolPrecedenceAssociativity
Primary()1Left to Right
Unary++, --2Right to Left
Multiplicative*, /, %3Left to Right
Additive+, -4Left to Right
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now