Operators in C++ - GeeksforGeeks
Operators in C++ - GeeksforGeeks
Search...
Operators in C++
C++ Last
Data Updated
Types C++ Sign In
: 20Input/Output
Jan, 2025 C++ Arrays C++ Pointers C++ OOPs C++ STL C++ In
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10 + 20;
cout << a;
return 0;
}
Output
30
Explanation: Here, ‘+‘ is an addition operator and does the addition of 10 and
20 operands and return value 30 as a result.
In C++, operators are classified into 6 types on the basis of type of operation
they perform:
Table of Content
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Ternary or Conditional Operators
https://www.geeksforgeeks.org/operators-in-cpp/ 1/15
4/24/25, 12:23 PM Operators in C++ | GeeksforGeeks
1. Arithmetic Operators
Arithmetic operators are used to perform arithmetic or mathematical
operations on the operands. For example, ‘+’ is used for addition.
Example:
#include <bits/stdc++.h>
using namespace std;
int main() {
int a = 8, b = 3;
// Addition
cout << "a + b = " << (a + b) << endl;
// Subtraction
cout << "a - b = " << (a - b) << endl;
// Multiplication
cout << "a * b = " << (a * b) << endl;
// Division
cout << "a / b = " << (a / b) << endl;
// Modulo
cout << "a % b = " << (a % b) << endl;
https://www.geeksforgeeks.org/operators-in-cpp/ 2/15
4/24/25, 12:23 PM Operators in C++ | GeeksforGeeks
// Increament
cout << "++a = " << ++a << endl;
// Decrement
cout << "--b = " << --b;
return 0;
}
Output
a + b = 11
a - b = 5
a * b = 24
a / b = 2
a % b = 2
++a = 9
--b = 2
Important Points:
The Modulo operator (%) operator should only be used with integers. Other
operators can also be used with floating point values.
++a and a++, both are increment operators, however, both are slightly
different. In ++a, the value of the variable is incremented first and then It is
used in the program. In a++, the value of the variable is assigned first and
then It is incremented. Similarly happens for the decrement operator.
You may have noticed that some operator works on two operands while other
work on one. On the basis of this operators are also classified as:
2. Relational Operators
Relational operators are used for the comparison of the values of two
operands. For example, ‘>’ check right operand is greater.
https://www.geeksforgeeks.org/operators-in-cpp/ 3/15
4/24/25, 12:23 PM Operators in C++ | GeeksforGeeks
Less Than or Equal Checks first operand is lesser than equal to the
<=
To second operand
Example
#include <bits/stdc++.h>
using namespace std;
int main() {
int a = 6, b = 4;
// Equal operator
cout << "a == b is " << (a == b) << endl;
return 0;
}
https://www.geeksforgeeks.org/operators-in-cpp/ 4/15
4/24/25, 12:23 PM Operators in C++ | GeeksforGeeks
Output
a == b is 0
a > b is 1
a >= b is 1
a < b is 0
a <= b is 0
a != b is 1
3. Logical Operators
Logical operators are used to combine two or more conditions or constraints or
to complement the evaluation of the original condition in consideration. The
result returns a Boolean value, i.e., true or false.
Logical Returns true only if all the operands are true or non-
&&
AND zero.
Example:
#include <bits/stdc++.h>
using namespace std;
int main() {
int a = 6, b = 4;
// Logical OR operator
cout << "a || b is " << (a || b) << endl;
https://www.geeksforgeeks.org/operators-in-cpp/ 5/15
4/24/25, 12:23 PM Operators in C++ | GeeksforGeeks
return 0;
}
Output
a && b is 1
a || b is 1
!b is 0
4. Bitwise Operators
Bitwise operators are works on bit-level. So, compiler first converted to bit-
level and then the calculation is performed on the operands.
One’s
~ Changes binary digits 1 to 0 and 0 to 1
Complement
Note: Only char and int data types can be used with Bitwise Operators.
https://www.geeksforgeeks.org/operators-in-cpp/ 6/15