Operators in C++: Types with Example
โก Smart Summary
Operators in C++ are symbols that perform operations on operands, ranging from arithmetic and comparison to logical, bitwise, and assignment actions. This resource explains every operator category with runnable example programs, output, and the precedence rules that decide evaluation order.

What are Operators?
An operator is a symbol used for performing operations on operands. An operator operates on operands, and the operations can be mathematical or logical. There are different types of operators in C++ for performing different operations.
Consider the following operation:
a = x + y;
In the above statement, x and y are the operands while + is an addition operator. When the C++ compiler encounters the above statement, it will add x and y and store the result in variable a.
Types Of Operators in C++
There are mainly 6 different types of operators in C++:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Other Operators
Let us begin with arithmetic operators, the most commonly used category.
Arithmetic Operators
They are the types of operators used for performing mathematical/arithmetic operations. They include:
| Operator | Description |
|---|---|
| + addition operator | Adds two operands. |
| โ subtraction operator | Subtracts 2nd operand from 1st operand. |
| * multiplication operator | Multiplies 2 operands. |
| / division operator | Divides numerator by denominator. |
| % modulus operator | Returns remainder after division. |
| ++ increment operator | Increases an integer value by 1. |
| โ decrement operator | Decreases an integer value by 1. |
For example:
#include <iostream> using namespace std; int main() { int a = 11; int b = 5; int c; cout << "a + b is :" << a+b << endl; //11+5 cout << "a - b is :" << a-b << endl; //11-5 cout << "a * b is :" << a*b << endl; //11*5 cout << "a / b is :" << a/b << endl; //11/5 cout << "a % b is :" << a%b << endl; //11%5 cout << "a++ is :" << a++ << endl; //11++ cout << "a-- is :" << a-- << endl; //12-- return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
- Including the iostream header file in our code. It will allow us to read from and write to the console.
- Including the std namespace so as to use its classes and functions without calling it.
- Calling the main() function inside which the logic of the program should be added. The { marks the start of the body of the main() function.
- Declaring an integer variable a and initializing it to 11.
- Declaring an integer variable b and initializing it to 5.
- Declaring an integer variable c.
- Printing the value of operation a+b alongside other text on the console.
- Printing the value of operation a-b alongside other text on the console.
- Printing the value of operation a*b alongside other text on the console.
- Printing the value of operation a/b alongside other text on the console.
- Printing the value of operation a%b alongside other text on the console.
- Printing the value of operation a++ alongside other text on the console.
- Printing the value of operation aโ alongside other text on the console.
- The main() function should return a value if the program runs fine.
- End of the body of the main() function.
Relational Operators
These types of operators perform comparisons on operands. For example, you may need to know which operand is greater than the other, or less than the other. They include:
| Operator | Description |
|---|---|
| == equal to operator | Checks equality of two operand values. |
| != not equal to operator | Checks inequality of two operand values. |
| > greater than operator | Checks whether the value of the left operand is greater than the value of the right operand. |
| < less than operator | Checks whether the value of the left operand is less than the value of the right operand. |
| >= greater than or equal to operator | Checks whether the value of the left operand is greater than or equal to the value of the right operand. |
| <= less than or equal to operator | Checks whether the value of the left operand is less than or equal to the value of the right operand. |
For example:
#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:
Here is a screenshot of the code:
Code Explanation:
- Including the iostream header file in our code. It will allow us to read from and write to the console.
- Including the std namespace so as to use its classes and functions without calling it.
- Calling the main() function inside which the logic of the program should be added.
- Declaring an integer variable a and initializing it to 11.
- Declaring an integer variable b and initializing it to 5.
- Printing some text on the console stating the values of variables a and b.
- Checking whether a is equal to b using an if decision-making statement.
- Text to print on the console if the operation a==b is true.
- The else part of the above if statement, stating what to do if a==b is false.
- Text to print on the console if the operation a==b is false.
- Checking whether a is less than b using an if statement.
- Text to print on the console if the operation a<b is true.
- The else part for when a<b is false.
- Text to print on the console if the operation a<b is false.
- Checking whether a is greater than b using an if statement.
- Text to print on the console if the operation a>b is true.
- The else part for when a>b is false.
- Text to print on the console if the operation a>b is false.
- The main() function should return a value if the program runs fine.
- End of the body of the main() function.
Logical Operators
The logical operators combine two or more constraints/conditions. Logical operators also complement the evaluation of the original condition under consideration. They include:
| Operator | Description |
|---|---|
| && logical AND operator | The condition is true if both operands are non-zero. |
| || logical OR operator | The condition is true if one of the operands is non-zero. |
| ! logical NOT operator | It reverses the operand’s logical state. If the operand is true, the ! operator makes it false. |
For example:
#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:
Here is a screenshot of the code:
Code Explanation:
- Including the iostream header file in our code. It will allow us to read from and write to the console.
- Including the std namespace so as to use its classes and functions without calling it.
- Calling the main() function inside which the logic of the program should be added.
- The { marks the start of the body of the main() function.
- Declaring 4 integer variables a, b, c, and d and assigning them different values.
- Using the && (AND) operator inside the if statement. It joins two conditions. The first condition is false and the second is true, so false&&true is false; the outcome of if is false.
- Text to print on the console if the above if statement is true. This will not be executed.
- The part to be executed if the above if statement is false.
- Text to print on the console if the if statement is false. This will be executed.
- Using the || (OR) operator within an if statement. The first condition is false and the second is true, so false||true is true; the outcome of if is true.
- Text to print on the console if the above if statement is true. This will be executed.
- The part to be executed if the above if statement is false.
- Text to print on the console if the if statement is false. This will not be executed.
- Checking whether the value of variable b is 0.
- Text to print on the console if the above if statement is true. This will not be executed.
- The part to be executed if the above if statement is false.
- Text to print on the console if the if statement is false. This will be executed.
- The main() function should return a value if the program runs fine.
- End of the body of the main() function.
Bitwise Operators
Bitwise operators perform bit-level operations on operands. First, the operands are converted to bit level, then operations are performed. When arithmetic operations like addition and subtraction are done at bit level, results can be achieved faster. They include:
| Operator | Description |
|---|---|
| & (bitwise AND) | Takes 2 numbers (operands) then performs AND on each bit of the two numbers. If both are 1, AND returns 1, otherwise 0. |
| | (bitwise OR) | Takes 2 numbers (operands) then performs OR on every bit of the two numbers. It returns 1 if one of the bits is 1. |
| ^ (bitwise XOR) | Takes 2 numbers (operands) then performs XOR on every bit of the 2 numbers. It returns 1 if both bits are different. |
| << (left shift) | Takes two numbers then left shifts the bits of the first operand. The second operand determines the total places to shift. |
| >> (right shift) | Takes two numbers then right shifts the bits of the first operand. The second operand determines the number of places to shift. |
| ~ (bitwise NOT) | Takes a number then inverts all its bits. |
#include <iostream> using namespace std; int main() { unsigned int p = 60; // 60 = 0011 1100 unsigned int q = 13; // 13 = 0000 1101 int z = 0; z = p & q; cout << "p&q is : " << z << endl; // 12 = 0000 1100 z = p | q; cout << "p|q is : " << z << endl; // 61 = 0011 1101 z = p ^ q; cout << "p^q is : " << z << endl; // 49 = 0011 0001 z = ~p; cout << "~p is : " << z << endl; // -61 = 1100 0011 z = p << 2; cout << "p<<2 is: " << z << endl; // 240 = 1111 0000 z = p >> 2; cout << "p>>2 is : " << z << endl; // 15 = 0000 1111 return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
- Including the iostream header file in our code. It will allow us to read from and write to the console.
- Including the std namespace so as to use its classes and functions without calling it.
- Calling the main() function inside which the logic of the program should be added.
- Declaring an unsigned integer variable p and assigning it a value of 60, which is 0011 1100 in binary.
- Declaring an unsigned integer variable q and assigning it a value of 13, which is 0000 1101 in binary.
- Declaring an integer variable z and initializing it to 0.
- Performing the bitwise & (AND) operation on variables p and q and storing the result in variable z.
- Printing the result of the above operation on the console alongside other text.
- Performing the bitwise | (OR) operation on variables p and q and storing the result in variable z.
- Printing the result of the above operation on the console alongside other text.
- Performing the bitwise ^ (XOR) operation on variables p and q and storing the result in variable z.
- Printing the result of the above operation on the console alongside other text.
- Performing the bitwise ~ (NOT) operation on variable p and storing the result in variable z.
- Printing the result of the above operation on the console alongside other text.
- Performing the left shift operation on variable p and storing the result in variable z.
- Printing the result of the above operation on the console alongside other text.
- Performing the right shift operation on variable p and storing the result in variable z.
- Printing the result of the above operation on the console alongside other text.
- The main() function should return a value if the program runs fine.
- End of the body of the main() function.
Assignment Operators
Assignment operators assign values to variables. The operand/variable is added to the left side of the operator while the value is added to the right side. The variable and the value must belong to the same data type, otherwise the C++ compiler will raise an error.
For example:
x = 5;
In the above example, x is the variable/operand, = is the assignment operator, while 5 is the value. Here are the popular assignment operators in C++:
| Operator | Description |
|---|---|
| = (simple assignment operator) | Assigns the value on the right to the variable on the left. |
| += (Add AND assignment operator) | Adds the value of the right operand to the left operand, then assigns the result to the variable on the left. |
| -= (Subtract AND assignment operator) | Subtracts the value of the right operand from the left operand, then assigns the result to the variable on the left. |
| *= (Multiply AND assignment operator) | Multiplies the value of the left operand with the right operand, then assigns the result to the variable on the left. |
| /= (Division AND assignment operator) | Divides the value of the left operand by the right operand, then assigns the result to the variable on the left. |
For example:
#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:
Here is a screenshot of the code:
Code Explanation:
- Including the iostream header file in our code. It will allow us to read from and write to the console.
- Including the std namespace so as to use its classes and functions without calling it.
- Calling the main() function inside which the logic of the program should be added.
- The { marks the start of the body of the main() function.
- Declaring an integer variable x and assigning it a value of 5.
- Printing the value of variable x alongside other text on the console. The \n is a new line character.
- Adding 5 to the value of variable x and assigning the result to variable x.
- Printing the value of variable x on the console alongside other text.
- Subtracting 5 from the value of x and assigning the result to variable x.
- Printing the value of variable x on the console alongside other text.
- Multiplying the value of variable x with 5 and assigning the result to variable x.
- Printing the value of variable x on the console alongside other text.
- Dividing the value of variable x by 5 and assigning the result to variable x.
- Printing the value of variable x on the console alongside other text.
- The main() function should return a value if the program runs fine.
- End of the body of the main() function.
Other Operators
Other Operators include the sizeof operator, Comma Operator, Conditional Operator, and Operators Precedence. Let us discuss the other operators supported by C++:
sizeof operator
This operator determines a variable’s size. Use the sizeof operator to determine the size of a data type.
For 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:
Here is a screenshot of the code:
Code Explanation:
- Including the iostream header file in our code. It will allow us to read from and write to the console.
- Including the std namespace so as to use its classes and functions without calling it.
- Calling the main() function inside which the logic of the program should be added.
- Determining the size of the integer data type using the sizeof operator and printing it alongside other text on the console.
- Determining the size of the character data type using the sizeof operator and printing it alongside other text on the console.
- Determining the size of the float data type using the sizeof operator and printing it alongside other text on the console.
- Determining the size of the double data type using the sizeof operator and printing it alongside other text on the console.
- The main() function should return a value if the program runs fine.
- End of the body of the main() function.
Comma Operator
The comma operator (,) triggers the performance of a sequence of operations. It expresses the first operand and discards the result. Next, it evaluates the second operand and returns the value and type.
#include <iostream> using namespace std; int main() { int x, y; y = 100; x = (y++, y + 10, 99 + y); cout << x; return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
- Including the iostream header file in our code. It will allow us to read from and write to the console.
- Including the std namespace so as to use its classes and functions without calling it.
- Calling the main() function inside which the logic of the program should be added.
- Declaring two integer variables x and y.
- Assigning the variable y a value of 100.
- Using the comma operator. It starts with y at 100, then increments it to 101 (y++). Next, y is added to 10. Finally, y, still at 101, is added to 99, giving 200. x is now 200.
- Printing the value of variable x on the console.
- The main() function should return a value if the program runs fine.
- End of the body of the main() function.
Conditional Operator
This operator evaluates a condition and acts based on the outcome of the evaluation.
Syntax:
Condition ? Expression2 : Expression3;
Parameters:
- The Condition is the condition that is to be evaluated.
- Expression2 is the expression to be executed if the condition is true.
- Expression3 is the expression to be executed if the condition is false.
For 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:
Here is a screenshot of the code:
Code Explanation:
- Including the iostream header file in our code. It will allow us to read from and write to the console.
- Including the std namespace so as to use its classes and functions without calling it.
- Calling the main() function inside which the logic of the program should be added.
- Declaring two integer variables a and b. Variable a has been assigned a value of 1.
- Assigning a value to variable b. If variable a is less than 10, b will be assigned the value 2, otherwise b will be assigned a value of 5.
- Printing the value of variable b on the console alongside other text.
- The main() function should return a value if the program runs fine.
- End of the body of the main() function.
Operators Precedence
A single operation may have more than one operator. In that case, operator precedence determines the one evaluated first.
The following list shows the precedence of operators in C++, with decreasing precedence from left to right:
(), [], *, /, %, +/-, <<, >>, ==, !=, ^, |, &&, ||, ?:, =, +=, -=, *=, /=













