C++ Operators
In C++ programming, operators are used to perform operations on operands (i.e. variables and values).
C++ categorizes the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Relational operators
- Logical operators
- Bit-wise operators
- Conditional Operators
Finally, miscellaneous operators such as sizeof, cast, member, and pointer are discussed here.
1. Arithmetic Operators
These operators are used with numeric values to perform common mathematical operations.
Operator | Meaning | Example |
---|---|---|
+ | Add two operands | x + y |
– | Subtract right operand from the left | x – y |
* | Multiply two operands | x * y |
/ | Divide left operand by the right one (always results into float) | x / y |
% | Modulus – remainder of the division of left operand by the right | x % y |
++ | Increment – Increases the value of a variable by 1 | x++ |
– – | Decrement – Decreases the value of a variable by 1 | x – – |
Example 1
//arithmetic.cpp
#include
using namespace std;
int main() {
int a = 100;
int b = 5;
int c, d;
//arithmetic operators demo
cout << "ADD: " << (a + b) << endl;
cout << "SUB: " << (a - b) << endl;
cout << "MUL: " << (a * b) << endl;
cout << "DIV: " << (a / b) << endl;
cout << "MOD: " << (a % b) << endl;
//post increment or decrement
c = b++; d = b--;
cout << "Post ++: " << c << endl;
cout << "Post --: " << d << endl;
//pre increment or decrement
c = ++b; d = --b;
cout << "Pre ++: " << c << endl;
cout << "Pre --: " << d << endl;
return 0;
}
Output:
ADD: 105
SUB: 95
MUL: 500
DIV: 20
MOD: 0
Post ++: 5
Post --: 6
Pre ++: 6
Pre --: 5
2. Assignment Operators
These operators are used to assign values to variables.
Operator | Example | Equivalent to |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x – 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
&= | x &= 5 | x = x & 5 |
|= | x |= 5 | x = x | 5 |
^= | x ^= 5 | x = x ^ 5 |
>>= | x >>= 5 | x = x >> 5 |
<<= | x <<= 5 | x = x << 5 |
Example 2
//assignment.cpp
#include
using namespace std;
int main() {
int x = 5;
//assignment operators demo
x = x + 5; // x += 5
cout << "x = " << x << endl;
x = x - 5; // x -= 5
cout << "x = " << x << endl;
x = x * 5; // x *= 5
cout << "x = " << x << endl;
x = x / 5; // x /= 5
cout << "x = " << x << endl;
x = x % 5; // x %= 5
cout << "x = " << x << endl;
return 0;
}
Output:
x = 10
x = 5
x = 25
x = 5
x = 0
3. Relational Operators
These operators are used to compare two values.
Operator | Meaning | Example |
---|---|---|
> | Greater that – True if left operand is greater than the right | x > y |
< | Less that – True if left operand is less than the right | x < y |
== | Equal to – True if both operands are equal | x == y |
!= | Not equal to – True if operands are not equal | x != y |
>= | Greater than or equal to – True if left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to – True if left operand is less than or equal to the right | x <= y |
Example 3
//relational.cpp
#include
using namespace std;
int main() {
int a = 10, b = 5;
//relational operators demo
cout << (a>b) << endl; //1
cout << (a<b) << endl; //0
cout << (a==b) << endl; //0
cout << (a!=b) << endl; //1
cout << (a>=b) << endl; //1
cout << (a<=b) << endl; //0
return 0;
}
Output:
1
0
0
1
1
0
4. Logical Operators
These operators are used to combine conditional statements.
Operator | Meaning | Example |
---|---|---|
&& | Logical AND — True if both the operands are true | x && y |
|| | Logical OR — True if either of the operands is true | x || y |
! | Logical NOT — True if operand is false (complements the operand) | !x |
Example 4
//logical.cpp
#include
using namespace std;
int main() {
int result;
//logical operators demo
result = ((10>5) && (9<7));
cout << "Logical AND = " << result << endl; //0
result = ((10>5) || (9<7));
cout << "Logical OR = " << result << endl; //1
result = !(9<7);
cout << "Logical NOT = " << result << endl; //1
return 0;
}
Output:
Logical AND = 0
Logical OR = 1
Logical NOT = 1
5. Bit-wise Operators
These operators are used to compare binary numbers.
For example, in the table below, let x = 10 (0000 1010
in binary) and y = 4 (0000 0100
in binary)
Operator | Meaning | Example |
---|---|---|
& | Bit-wise AND | x& y = 0 (0000 0000 ) |
| | Bit-wise OR | x | y = 14 (0000 1110 ) |
~ | Bit-wise NOT | ~x = -11 (1111 0101 ) |
^ | Bit-wise XOR | x ^ y = 14 (0000 1110 ) |
>> | Bit-wise right shift | x>> 2 = 2 (0000 0010 ) |
<< | Bit-wise left shift | x<< 2 = 40 (0010 1000 ) |
Example 5
//bitwise.cpp
#include
using namespace std;
int main() {
int x = 10;
int y = 4;
//bitwise operators demo
cout << "Bit-wise AND: " << (x & y) << endl; //0
cout << "Bit-wise OR: " << (x | y) << endl; //14
cout << "Bit-wise NOT: " << (~x) << endl; //-11
cout << "Bit-wise XOR: " << (x ^ y) << endl; //14
cout << "Bit-wise right shift: " << (x >> 2) << endl; //2
cout << "Bit-wise left shift: " << (x << 2) << endl; //40
return 0;
}
Output:
Bit-wise AND: 0
Bit-wise OR: 14
Bit-wise NOT: -11
Bit-wise XOR: 14
Bit-wise right shift: 2
Bit-wise left shift: 40
6. Conditional Operators
The conditional operator is the only ternary operator (operator that takes three arguments) in C++. The operator evaluates the first argument and, if true, evaluates the second argument. If the first argument evaluates to false, then the third argument is evaluated. The conditional operator is the expression equivalent of the if-else statement.
Example 6
//conditional.cpp
#include
using namespace std;
int main() {
int a = 15, b = 12;
int result;
//conditional operators demo
result = a > b ? 1 : 0;
cout << "Result = " << result << endl; //1
return 0;
}
Output:
Result = 1
Miscellaneous Operators
The following text describes some other operators supported by C++ language.
sizeof operator: The sizeof is a compile-time operator that determines the size, in bytes, of a variable or data type. An example is given below:
int a = 10 ; //sizeof operator cout << sizeof(a) << endl; //4
cout << sizeof(double) << endl; //8
cast operator: A cast is a unary operator that converts one data type into another. An example is given below:
float f = 10.25; int i ; //casting operator i = (int) f;
cout << i; //10
member operators: The . (dot) operator and the -> (arrow) operator are the member operators which are used to refer individual members of classes, structures, and unions. We will explain this topic later with respect to C++ structures.
pointer operators: C++ provides two pointer operators, which are –
(i) address of operator & and
(ii) Indirection operator *.
We will explain this topic later with respect to C++ pointers.