Skip to main content
Ajay Dhangar
EditReport

Operators in C++

An operator is a symbol that tells the compiler to perform specific mathematical, relational, or logical manipulations on values or variables. The data values that operators act upon are called operands.

C++ provides a rich set of built-in operators classified by their functionality and the number of operands they require (unary, binary, or ternary).

1. Arithmetic Operators

Arithmetic operators execute standard mathematical operations. These require numerical operands.

OperatorOperationMathematical DescriptionExample (x=10, y=3)Result
+AdditionSum of two valuesx + y13
-SubtractionDifference between two valuesx - y7
*MultiplicationProduct of two valuesx * y30
/DivisionQuotient of divisionx / y3 (Integer truncation)
%ModulusRemainder of an integer divisionx % y1
Note on Division

When both operands are integers (int), the / operator performs integer division, discarding any fractional remainder. To get a decimal result, at least one operand must be a floating-point type (e.g., 10.0 / 3).

ArithmeticOperators.cpp
#include <iostream>

int main() {
int x = 10, y = 5;
std::cout << (x + y) << "\n"; // Output: 15
std::cout << (x / y) << "\n"; // Output: 2
std::cout << (x % y) << "\n"; // Output: 0
return 0;
}

2. Relational Operators

Relational operators compare two values to determine their relationship. They always evaluate to a boolean state: true (1) or false (0).

OperatorDescriptionCondition for trueExample (x=10, y=5)Result
==Equal toLeft operand equals right operandx == yfalse
!=Not equal toLeft operand does not equal right operandx != ytrue
>Greater thanLeft operand is strictly larger than rightx > ytrue
<Less thanLeft operand is strictly smaller than rightx < yfalse
>=Greater than or equal toLeft operand is larger or equal to rightx >= ytrue
<=Less than or equal toLeft operand is smaller or equal to rightx <= yfalse
RelationalOperators.cpp
#include <iostream>

int main() {
int x = 10, y = 5;
std::cout << (x > y) << "\n"; // Output: 1 (true)
std::cout << (x == y) << "\n"; // Output: 0 (false)
std::cout << (x <= y) << "\n"; // Output: 0 (false)
return 0;
}

3. Logical Operators

Logical operators are used to connect or invert expressions containing relational or boolean states.

OperatorOperationBehaviorExample (x=10, y=5)Result
&&Logical ANDReturns true only if both expressions are true.(x > 5 && y < 10)true
``Logical ORReturns true if at least one expression is true.
!Logical NOTInverts the boolean value of the expression.!(x > 5)false
Short-Circuit Evaluation

C++ optimizes logical evaluations. For &&, if the left side is false, the right side is never evaluated because the entire statement cannot be true. For ||, if the left side is true, the right side is skipped.

LogicalOperators.cpp
#include <iostream>

int main() {
int x = 10, y = 5;
std::cout << (x > 5 && y < 10) << "\n"; // Output: 1 (true)
std::cout << (x > 15 || y == 5) << "\n"; // Output: 1 (true)
std::cout << !(x > 5) << "\n"; // Output: 0 (false)
return 0;
}

4. Assignment Operators

Assignment operators evaluate the expression on the right side and store the resulting value into the memory variable specified on the left side.

OperatorSyntaxEquivalent Expression
=x = yx = y
+=x += yx = x + y
-=x -= yx = x - y
*=x *= yx = x * y
/=x /= yx = x / y
%=x %= yx = x % y
AssignmentOperators.cpp
#include <iostream>

int main() {
int x = 10;
x += 5; // x is now 15
x *= 2; // x is now 30
std::cout << x << "\n"; // Output: 30
return 0;
}

5. Increment and Decrement Operators

These unary operators modify a single numerical variable's value by exactly 1. They come in two distinct behaviors: Prefix and Postfix.

OperatorSyntaxNameBehavior
++++xPrefix IncrementIncrements x first, then returns the updated value.
++x++Postfix IncrementReturns the current value of x first, then increments x.
----xPrefix DecrementDecrements x first, then returns the updated value.
--x--Postfix DecrementReturns the current value of x first, then decrements x.
IncrementDecrementOperators.cpp
int x = 10;
std::cout << ++x; // Increments x to 11, prints 11
std::cout << x++; // Prints current value (11), then increments x to 12
std::cout << x; // Prints current value (12)

6. Bitwise Operators

Bitwise operators manipulate the raw binary bits within integer data types directly.

OperatorNameBitwise Operation
&Bitwise ANDResults in 1 if both corresponding bits are 1.
``Bitwise OR
^Bitwise XORResults in 1 if the corresponding bits are different.
~Bitwise NOTFlips all bits (1 becomes 0, 0 becomes 1).
<<Left ShiftShifts bits left, filling empty spaces on the right with zeros.
>>Right ShiftShifts bits right, moving bits down in value.
BitwiseOperators.cpp
#include <iostream>
// Example using 4-bit representation for simplicity:
int a = 5; // Binary: 0101
int b = 9; // Binary: 1001

std::cout << (a & b); // Binary: 0001 -> Output: 1
std::cout << (a | b); // Binary: 1101 -> Output: 13
std::cout << (a ^ b); // Binary: 1100 -> Output: 12
std::cout << (~a); // Binary: 1010 -> Output: -6 (two's complement)
std::cout << (a << 1); // Binary: 1010 -> Output: 10
std::cout << (a >> 1); // Binary: 0010 -> Output: 2

7. Ternary Conditional Operator (?:)

The ternary operator is the only operator in C++ that acts on three operands. It serves as a clean, inline alternative to simple if-else blocks.

Syntax

TernaryOperatorSyntax.cpp
condition ? expression_if_true : expression_if_false;

Example

TernaryOperatorExample.cpp
int testScore = 85;
std::string classification = (testScore >= 50) ? "Pass" : "Fail";
std::cout << classification; // Output: Pass

8. Operator Precedence and Associativity

When evaluating a complex expression like int result = 5 + 3 * 2;, C++ relies on predefined rules to determine which operator executes first.

  1. Precedence: Dictates the execution priority of different operators. For example, multiplication (*) has a higher priority rank than addition (+). Therefore, 3 * 2 is processed first.
  2. Associativity: Dictates the processing direction (Left-to-Right or Right-to-Left) when two operators with the exact same precedence rank appear adjacent to each other.

Best Practice: When in doubt about operator precedence rules, wrap your planned execution segments in **parentheses ()**. Parentheses hold the absolute highest priority rank in C++ and make code significantly easier to read.

OperatorPrecedence.cpp
int evaluationA = 10 + 5 * 2;   // Evaluates to 20 (Multiplication occurs first)
int evaluationB = (10 + 5) * 2; // Evaluates to 30 (Parentheses override standard precedence)

Conclusion

Mastering C++ operators is essential for writing efficient and effective code. Understanding how to use them correctly allows you to perform complex calculations, make decisions, and manipulate data with precision. Always remember to consider operator precedence and associativity to ensure your expressions evaluate as intended.

Finished reading? Mark this topic as complete.