C++ Operators


Introduction

In C++, operators are special symbols or keywords used to perform operations on variables and values. Operators allow you to manipulate data and variables in your program, whether it's performing mathematical calculations, comparisons, or logical operations. C++ has a wide variety of operators that can be grouped into different categories, each serving a unique purpose.

In this post, we’ll discuss the following categories of operators in C++:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Increment and Decrement Operators
  • Conditional (Ternary) Operator
  • Other Operators (Sizeof, Type-casting, etc.)

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division.

List of Arithmetic Operators:

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulus (remainder of division)
Example:
#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5;
    cout << "Addition: " << a + b << endl;
    cout << "Subtraction: " << a - b << endl;
    cout << "Multiplication: " << a * b << endl;
    cout << "Division: " << a / b << endl;
    cout << "Modulus: " << a % b << endl;

    return 0;
}

Output:

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0

2. Relational Operators

Relational operators are used to compare two values and determine the relationship between them. These operators return a boolean value (true or false).

List of Relational Operators:

  • == : Equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to
Example:
#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5;
    cout << "a == b: " << (a == b) << endl;
    cout << "a != b: " << (a != b) << endl;
    cout << "a > b: " << (a > b) << endl;
    cout << "a < b: " << (a < b) << endl;
    cout << "a >= b: " << (a >= b) << endl;
    cout << "a <= b: " << (a <= b) << endl;

    return 0;
}

Output:

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

3. Logical Operators

Logical operators are used to perform logical operations, typically with boolean values (true or false).

List of Logical Operators:

  • && : Logical AND
  • || : Logical OR
  • ! : Logical NOT
Example:
#include <iostream>
using namespace std;

int main() {
    bool a = true, b = false;
    cout << "(a && b): " << (a && b) << endl;  // AND
    cout << "(a || b): " << (a || b) << endl;  // OR
    cout << "!(a && b): " << !(a && b) << endl; // NOT

    return 0;
}

Output:

(a && b): 0
(a || b): 1
!(a && b): 1

4. Bitwise Operators

Bitwise operators operate on the binary representation of numbers. These operators are useful for manipulating individual bits of data.

List of Bitwise Operators:

  • & : Bitwise AND
  • | : Bitwise OR
  • ^ : Bitwise XOR
  • ~ : Bitwise NOT
  • <<: Left shift
  • >>: Right shift
Example:
#include <iostream>
using namespace std;

int main() {
    int a = 5, b = 3;
    cout << "a & b: " << (a & b) << endl;   // AND
    cout << "a | b: " << (a | b) << endl;   // OR
    cout << "a ^ b: " << (a ^ b) << endl;   // XOR
    cout << "~a: " << (~a) << endl;         // NOT
    cout << "a << 1: " << (a << 1) << endl; // Left shift
    cout << "a >> 1: " << (a >> 1) << endl; // Right shift

    return 0;
}

Output:

a & b: 1
a | b: 7
a ^ b: 6
~a: -6
a << 1: 10
a >> 1: 2

5. Assignment Operators

Assignment operators are used to assign values to variables. C++ provides several types of assignment operators.

List of Assignment Operators:

  • = : Simple assignment
  • += : Add and assign
  • -= : Subtract and assign
  • *= : Multiply and assign
  • /= : Divide and assign
  • %= : Modulus and assign
Example:
#include <iostream>
using namespace std;

int main() {
    int a = 10;
    a += 5; // Equivalent to a = a + 5
    cout << "a after += 5: " << a << endl;
    a *= 2; // Equivalent to a = a * 2
    cout << "a after *= 2: " << a << endl;

    return 0;
}

Output:

a after += 5: 15
a after *= 2: 30

6. Increment and Decrement Operators

The increment (++) and decrement (--) operators are used to increase or decrease a variable's value by 1, respectively.

List of Increment/Decrement Operators:

  • ++a : Pre-increment (increase value before using it)
  • a++ : Post-increment (increase value after using it)
  • --a : Pre-decrement (decrease value before using it)
  • a-- : Post-decrement (decrease value after using it)
Example:
#include <iostream>
using namespace std;

int main() {
    int a = 10;
    cout << "Pre-increment: " << ++a << endl; // Increments a and then displays
    cout << "Post-increment: " << a++ << endl; // Displays a and then increments
    cout << "After post-increment: " << a << endl;

    return 0;
}

Output:

Pre-increment: 11
Post-increment: 11
After post-increment: 12

7. Conditional (Ternary) Operator

The conditional operator (?:) is a shorthand for an if-else statement. It is used to evaluate expressions based on a condition.

Syntax:

condition ? expression_if_true : expression_if_false;

Example:

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5;
    int max = (a > b) ? a : b;  // Assigns the larger value to 'max'
    cout << "Maximum value: " << max << endl;

    return 0;
}

Output:

Maximum value: 10

8. Other Operators

  • sizeof: Used to determine the size (in bytes) of a variable or data type.
  • Type-casting: Allows you to convert one data type to another explicitly.

Example of sizeof and Type-casting:

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    cout << "Size of x: " << sizeof(x) << " bytes" << endl;

    double pi = 3.14;
    cout << "Type-casting: " << (int)pi << endl;  // Convert double to int

    return 0;
}

Output:

Size of x: 4 bytes
Type-casting: 3