Skip to main content

Command Palette

Search for a command to run...

🔹 Operators in C++

Published
4 min readView as Markdown

Operators are symbols that perform operations on variables and values.
For example:

int a = 10, b = 5;
cout << a + b;   // + is an operator (Addition)

C++ operators are mainly divided into categories:


1. Arithmetic Operators

Used for mathematical operations.

OperatorMeaningExample
+Additiona + b = 15
-Subtractiona - b = 5
*Multiplicationa * b = 50
/Divisiona / b = 2
%Modulus (Remainder)a % b = 0

2. Relational (Comparison) Operators

Used to compare two values.
They return true (1) or false (0).

OperatorMeaningExample
==Equal toa == b → false
!=Not equal toa != b → true
>Greater thana > b → true
<Less thana < b → false
>=Greater than or equal toa >= b → true
<=Less than or equal toa <= b → false

3. Logical Operators

Used for decision making (conditions).

OperatorMeaningExample
&&Logical AND (true if both are true)(a > 5 && b > 2) → true
``Logical OR (true if at least one is true)`(a > 5b > 10) → true`
!Logical NOT (reverses result)!(a > b) → false

4. Assignment Operators

Used to assign values.

OperatorMeaningExample
=Assigns valuex = 10
+=Adds and assignsx += 5; // x = x + 5
-=Subtract and assignx -= 5;
*=Multiply and assignx *= 2;
/=Divide and assignx /= 2;
%=Modulus and assignx %= 2;

5. Increment and Decrement Operators

Change the value by +1 or -1.

OperatorMeaningExample
++Increment by 1++a; (pre-increment) / a++; (post-increment)
--Decrement by 1--a; / a--;

6. Bitwise Operators

Work at the bit level (used in low-level programming).

OperatorMeaningExample (a=5=0101, b=3=0011)
&ANDa & b = 1 (0001)
``OR`ab = 7` (0111)
^XORa ^ b = 6 (0110)
~NOT (1’s complement)~a = -6
<<Left shifta << 1 = 10 (1010)
>>Right shifta >> 1 = 2 (0010)

7. Conditional (Ternary) Operator

Shorthand for if-else.

int a = 10, b = 20;
int max = (a > b) ? a : b;   // max = 20

8. Comma Operator

Used to separate multiple expressions, returns the last value.

int x = (5, 10); // x = 10

9. sizeof Operator

Gives size of a data type in bytes.

cout << sizeof(int);  // 4

10. Pointer Operators

Used with pointers.

OperatorMeaningExample
&Address-of operator&a gives address of a
*Dereference operator*ptr gives value stored at pointer

✅ Example program using multiple operators:

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5;

    cout << "Arithmetic: " << a + b << endl;
    cout << "Relational: " << (a > b) << endl;
    cout << "Logical: " << (a > 0 && b > 0) << endl;
    cout << "Assignment: " << (a += 2) << endl;
    cout << "Bitwise AND: " << (a & b) << endl;
    cout << "Sizeof int: " << sizeof(int) << endl;

    return 0;
}

More from this blog

Amit singh's blog

235 posts