C++ Operators Explained: Types, Examples, and Sample Programs
What Are Operators?
In C++, an operator is a symbol that performs an operation on one or more operands. Operators can be arithmetic, logical, relational, bitwise, or assignment, among others.
Example:
a = x + y;
Here, x and y are operands, and + is the addition operator. The compiler adds x and y and stores the result in a.
In this tutorial you’ll learn:
- What operators are
- Types of operators in C++
- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
- Assignment operators
- Other operators (sizeof, comma, conditional, precedence)
Types of Operators in C++
C++ defines six main categories of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Other Operators
Arithmetic Operators
These operators perform mathematical calculations.
| Operator | Description |
|---|---|
| + | Adds two operands. |
| - | Subtracts the second operand from the first. |
| * | Multiplies two operands. |
| / | Divides the numerator by the denominator. |
| % | Returns the remainder of a division. |
| ++ | Increments an integer by one. |
| -- | Decrements an integer by one. |
Example program:
#include <iostream>
using namespace std;
int main() {
int a = 11;
int b = 5;
int c;
cout << "a + b is :" << a+b << endl;
cout << "a - b is :" << a-b << endl;
cout << "a * b is :" << a*b << endl;
cout << "a / b is :" << a/b << endl;
cout << "a % b is :" << a%b << endl;
cout << "a++ is :" << a++ << endl;
cout << "a-- is :" << a-- << endl;
return 0;
}
Output:

Code explanation is omitted for brevity.
Relational Operators
These operators compare operands and return a boolean result.
| Operator | Description |
|---|---|
| == | Checks equality. |
| != | Checks inequality. |
| > | Greater than. |
| < | Less than. |
| >= | Greater than or equal to. |
| <= | Less than or equal to. |
Example program:
#include <iostream>
using namespace std;
int main() {
int a = 11;
int b = 5;
cout << "a=11, b=5" << endl;
if (a == b) {
cout << "a == b is true" << endl;
} else {
cout << "a == b is false" << endl;
}
if (a < b) {
cout << "a < b is true" << endl;
} else {
cout << "a < b is false" << endl;
}
if (a > b) {
cout << "a > b is true" << endl;
} else {
cout << "a > b is false" << endl;
}
return 0;
}
Output:

Logical Operators
Logical operators combine boolean expressions.
| Operator | Description |
|---|---|
| && | Logical AND – true if both operands are non‑zero. |
| || | Logical OR – true if at least one operand is non‑zero. |
| ! | Logical NOT – inverts the boolean value. |
Example program:
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 2, c = 6, d = 4;
if (a == b && c > d)
cout << "a equals to b AND c is greater than d\n";
else
cout << "AND operation returned false\n";
if (a == b || c > d)
cout << "a equals to b OR c is greater than d\n";
else
cout << "Neither a is equal to b nor c is greater than d\n";
if (!b)
cout << "b is zero\n";
else
cout << "b is not zero";
return 0;
}
Output:

Bitwise Operators
Bitwise operators perform operations on individual bits of integer values.
| Operator | Description |
|---|---|
| & | Bitwise AND – 1 only if both bits are 1. |
| | | Bitwise OR – 1 if at least one bit is 1. |
| ^ | Bitwise XOR – 1 if bits differ. |
| << | Left shift – shifts bits left, filling with 0. |
| >> | Right shift – shifts bits right, discarding lower bits. |
| ~ | Bitwise NOT – inverts all bits. |
Example program:
#include <iostream>
using namespace std;
int main() {
unsigned int p = 60; // 0011 1100
unsigned int q = 13; // 0000 1101
int z = 0;
z = p & q;
cout << "p&q is : " << z << endl; // 12
z = p | q;
cout << "p|q is : " << z << endl; // 61
z = p ^ q;
cout << "p^q is : " << z << endl; // 49
z = ~p;
cout << "~p is : " << z << endl; // -61
z = p << 2;
cout << "p<<2 is: " << z << endl; // 240
z = p >> 2;
cout << "p>>2 is : " << z << endl; // 15
return 0;
}
Output:

Assignment Operators
These operators assign values to variables, often combining an arithmetic operation.
| Operator | Description |
|---|---|
| = | Simple assignment. |
| += | Adds and assigns. |
| -= | Subtracts and assigns. |
| *= | Multiplies and assigns. |
| /= | Divides and assigns. |
Example program:
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << "Initial value of x is " << x << "\n";
x += 5;
cout << "x += 5 gives :" << x << "\n";
x -= 5;
cout << "x -= 5 gives : " << x << "\n";
x *= 5;
cout << "x *= 5 gives :" << x << "\n";
x /= 5;
cout << "x /= 5 gives : " << x << "\n";
return 0;
}
Output:

Other Operators
sizeof Operator
Returns the size, in bytes, of a type or object.
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Size of int : " << sizeof(int) << "\n";
cout << "Size of char : " << sizeof(char) << "\n";
cout << "Size of float : " << sizeof(float) << "\n";
cout << "Size of double : " << sizeof(double) << "\n";
return 0;
}
Output:

Comma Operator
Executes a sequence of expressions, returning the value of the last.
Example:
#include <iostream>
using namespace std;
int main() {
int x, y;
y = 100;
x = (y++, y + 10, 99 + y);
cout << x;
return 0;
}
Output:

Conditional Operator
Also known as the ternary operator, it evaluates a condition and returns one of two expressions.
Syntax: condition ? expr1 : expr2
Example:
#include <iostream>
using namespace std;
int main() {
int a = 1, b;
b = (a < 10) ? 2 : 5;
cout << "value of b: " << b << endl;
return 0;
}
Output:

Operator Precedence
When multiple operators appear in a single expression, precedence rules determine the order of evaluation. From highest to lowest precedence:
(), [], *, /, %, +/-, <<, >>, ==, !=, ^, |, &&, ||, ?:, =, +=, -=, *=, /=
Summary
- Operators are the building blocks for performing calculations and logic in C++.
- Arithmetic operators handle basic math.
- Relational operators enable comparisons.
- Logical operators support boolean logic.
- Bitwise operators manipulate individual bits.
- Assignment operators store values, often combining arithmetic.
- The sizeof operator reveals the memory footprint of types.
- The comma operator sequences expressions.
- The conditional operator offers concise inline decisions.
C Language
- C++ For Loops Explained: Syntax, Workflow, and Practical Examples
- C++ Operator Overloading – A Practical Guide with Code Examples
- Mastering std::stack in C++: A Comprehensive Guide with Practical Examples
- C++ Structs Explained with a Practical Example
- C++ Classes & Objects: A Practical Guide with Code Examples
- C++ Polymorphism Explained: Practical Examples & Key Concepts
- Mastering std::list in C++: Syntax, Functions & Practical Examples
- C++ Struct vs Class: Key Differences, Syntax, and Best Use Cases
- Understanding C# Interfaces: Definition, Example, and Practical Use
- Java Variables and Data Types – A Comprehensive Guide with Examples