C++ Operators

Operators are fundamental building blocks in programming languages that perform operations on variables and values. In C++, operators are symbols that specify which operation to perform on one or more operands (variables or values). They are essential for constructing expressions and statements in C++ programs.

Types of C++ Operators

C++ provides a rich set of operators to handle various operations, which can be categorized into several types:

1. Arithmetic Operators: Used for basic arithmetic operations like addition, subtraction, multiplication, and division.

• + (Addition)

• - (Subtraction)

• * (Multiplication)

• / (Division)

• % (Modulus)

Example:

#include <iostream>

int main() {
    int x = 15;
    int y = 4;

    std::cout << "Arithmetic Operators:\n";
    std::cout << "x + y = " << (x + y) << "\n";     // Addition
    std::cout << "x - y = " << (x - y) << "\n";     // Subtraction
    std::cout << "x * y = " << (x * y) << "\n";     // Multiplication
    std::cout << "x / y = " << (x / y) << "\n";     // Division
    std::cout << "x % y = " << (x % y) << "\n";     // Modulus

    return 0;
}

Output:

Arithmetic Operators:
x + y = 19
x - y = 11
x * y = 60
x / y = 3
x % y = 3

2. Relational Operators: Used to compare two values and return a boolean result (true or false). These operators are essential for conditional statements and loops.

• == (Equal to)

• != (Not equal to)

• < (Less than)

• <= (Less than or equal to)

• > (Greater than)

• >= (Greater than or equal to)

Example:

#include <iostream>

int main() {
    int a = 10;
    int b = 20;

    std::cout << "Relational Operators:\n";
    std::cout << "a == b: " << (a == b) << "\n";     // Equality
    std::cout << "a != b: " << (a != b) << "\n";     // Inequality
    std::cout << "a > b: " << (a > b) << "\n";     // Greater than
    std::cout << "a < b: " << (a < b) << "\n";     // Less than
    std::cout << "a >= b: " << (a >= b) << "\n";     // Greater than or equal to
    std::cout << "a <= b: " << (a <= b) << "\n";     // Less than or equal to

    return 0;
}

Output:

Relational Operators:
a == b: 0
a != b: 1
a > b: 0
a < b: 1 a>= b: 0 a <= b: 1

3. Logical Operators: Used to perform logical operations on boolean values. They help in combining multiple conditions in control flow statements.

• && (Logical AND)

• || (Logical OR)

• ! (Logical NOT)

Example:

#include <iostream>

int main() {
    bool p = true;
    bool q = false;

    std::cout << "Logical Operators:\n";
    std::cout << "p && q: " << (p && q) << "\n";     // Logical AND
    std::cout << "p || q: " << (p || q) << "\n";     // Logical OR
    std::cout << "!p: " << (!p) << "\n";     // Logical NOT

    return 0;
}

Output:

Logical Operators:
p && q: 0
p || q: 1
!p: 0

4. Bitwise Operators: Operate on the binary representations of integers, performing operations like bitwise AND, OR, XOR, and shifts.


• & (Bitwise AND)

• | (Bitwise OR)

• ^ (Bitwise XOR)

• ~ (Bitwise NOT)

• << (Left shift)

• >> (Right shift)

Example:

#include <iostream>

int main() {
    int m = 12; // 1100 in binary
    int n = 6; // 0110 in binary

    std::cout << "Bitwise Operators:\n";
    std::cout << "m & n: " << (m & n) << "\n"; // Bitwise AND (1000 in binary, 8 in decimal)
    std::cout << "m | n: " << (m | n) << "\n"; // Bitwise OR (1110 in binary, 14 in decimal)
    std::cout << "m ^ n: " << (m ^ n) << "\n"; // Bitwise XOR (0110 in binary, 6 in decimal)
    std::cout << "~m: " << (~m) << "\n"; // Bitwise NOT (~1100 in binary, -13 in decimal)
    std::cout << "m << 2: " << (m << 2) << "\n"; // Left shift (110000 in binary, 48 in decimal)
    std::cout << "m >> 2: " << (m >> 2) << "\n"; // Right shift (0011 in binary, 3 in decimal)

    return 0;
}

Output:

Bitwise Operators:
m & n: 4
m | n: 14
m ^ n: 10
~m: -13
m << 2: 48 m>> 2: 3

5. Assignment Operators: Used to assign values to variables. They also include compound assignment operators that combine assignment with another operation.

• = (Assignment)

• += (Add and assign)

• -= (Subtract and assign)

• *= (Multiply and assign)

• /= (Divide and assign)

• %= (Modulus and assign)

• <<= (Left shift and assign)

• >>= (Right shift and assign)

• &= (Bitwise AND and assign)

• ^= (Bitwise XOR and assign)

• |= (Bitwise OR and assign)

#include <iostream>

int main() {
    int a = 5;

    std::cout << "Assignment Operators:\n";
    a += 3; // Equivalent to a = a + 3
    std::cout << "a += 3: " << a << "\n";

    a -= 2; // Equivalent to a = a - 2
    std::cout << "a -= 2: " << a << "\n";

    a *= 4; // Equivalent to a = a * 4
    std::cout << "a *= 4: " << a << "\n";

    a /= 2; // Equivalent to a = a / 2
    std::cout << "a /= 2: " << a << "\n";

    a %= 3; // Equivalent to a = a % 3
    std::cout << "a %= 3: " << a << "\n";

    return 0;
}

Output:

Assignment Operators:
a += 3: 8
a -= 2: 6
a *= 4: 24
a /= 2: 12
a %= 3: 0

6. Unary Operators: Operate on a single operand and include operations like increment, decrement, and logical NOT.

• + (Unary plus)

• - (Unary minus)

• ++ (Increment)

• -- (Decrement)

• ! (Logical NOT)

• ~ (Bitwise NOT)

• * (Pointer dereference)

• & (Address of)

Example:

#include <iostream>

int main() {
    int num = 5;

    std::cout << "Unary Operators:\n";
    std::cout << "++num: " << ++num << "\n"; // Pre-increment
    std::cout << "num--: " << num-- << "\n"; // Post-decrement
    std::cout << "num after post-decrement: " << num << "\n";
    std::cout << "-num: " << -num << "\n"; // Unary minus

    return 0;
}

Output:

Unary Operators:
++num: 6
num--: 6
num after post-decrement: 5
-num: -5

7. Ternary or Conditional Operator: A shorthand for if-else statements that returns one of two values based on a condition.


• ?: (Condition ? true_value : false_value)

Example:

#include <iostream>

int main() {
    int x = 10;
    int y = 20;

    std::cout << "Ternary Operator:\n";
    int max = (x > y) ? x : y; // Conditional operator
    std::cout << "Max of x and y: " << max << "\n";

    return 0;
}

Output:

Ternary Operator:
Max of x and y: 20

9. Miscellaneous Operators: Include operators for specific purposes such as comma separation, member access, function calls, and type identification.

• , (Comma operator)

• . (Member access)

• -> (Pointer to member access)

• [] (Array subscript)

• () (Function call)

• typeid (Type identification)

• sizeof (Size of data type or object)

Example:

#include <iostream>

int main() {
    int arr[3] = {1, 2, 3};
    int *ptr = &arr[0];

    std::cout << "Miscellaneous Operators:\n";
    std::cout << "Array element arr[1]: " << arr[1] << "\n"; // Array subscript
    std::cout << "Pointer dereference *ptr: " << *ptr << "\n"; // Pointer dereference
    std::cout << "Size of int: " << sizeof(int) << " bytes\n"; // Sizeof operator

    return 0;
}

Output:

Miscellaneous Operators:
Array element arr[1]: 2
Pointer dereference *ptr: 1
Size of int: 4 bytes

Operator Precedence and Associativity

Operator precedence determines the order in which operators are evaluated. Associativity determines the direction of evaluation for operators with the same precedence.

Category Operator Associativity
Postfix () [] -> . ++ -- Left to right
Unary + - ! ~ ++ -- (type) * & sizeof Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= >>= <<= &= ^= Right to left
Comma , Left to right