C++ Operators
An operator is a symbol that is used to perform certain mathematical or logical manipulations. There are following types of operators in C++ programming language:
- Arithmatic operators
- Relational operators
- Logical operators
- Assignment operators
- Increment and decrement operators
- Conditional operators
- Bitwise operators
- Special operators
Arithmatic Operators
Operator | Description | Example |
---|---|---|
+ | Addition | 2+3 = 5 |
- | Subtraction | 10-5 = 5 |
* | Multiplication | 2*5 = 10 |
/ | Division | 10/2 = 5 |
% | Modulus | 11%5 = 1 |
Relational Operators
Operator | Description | Example |
---|---|---|
== | Is equal to | a == b is not true |
!= | Not equal to | a == b is true |
> | Greater than | a>b is not true |
< | Less than | a<b is true |
>= | Greater than or equal to | a>=b is not true |
<= | Less than or equal to | a<=b is true |
Logical Operators
Operator | Description | Example |
---|---|---|
&& | Logical AND | 5==5 && 7==9 is false |
|| | Logical OR | 5==5 || 7==9 is true |
! | Logical Not | !(7==9) is true |
Assignment Operators
Operator | Description | Example |
---|---|---|
= | Assign the values from right side operand to the left side operand | a = 5 |
+= | Add the values of right operand to the left operand and assigns the result to the left operand | a += 5 is equivalent to a = a+5 |
-= | Subtract the values of right operand to the left operand and assigns the result to the left operand | a -= 5 is equivalent to a = a-5 |
*= | Multiply the values of right operand to the left operand and assigns the result to the left operand | a *= 5 is equivalent to a = a*5 |
/= | Divide the values of right operand to the left operand and assigns the result to the left operand | a /= 5 is equivalent to a = a/5 |
%= | Take the modulus value using two operands and assigns the result to the left operand. | a %= 5 is equivalent to a = a%5 |
Increment and decrement Operators
Operator | Description | Example |
---|---|---|
++ | Increment | a++ (it increases the value of variable a by one) |
-- | Decrement | a-- (it decreases the value of variable a by one) |
Conditional Operator
Operator | Description | Example |
---|---|---|
?: | Conditional expression | exp1 ? exp2 : exp3 (where exp1, exp2, exp3 are expressions) |
Bitwise Operators
Operator | Description | Example |
---|---|---|
& | Bitwise AND | 3==5 & 7==9 is false |
| | Bitwise OR | 3==5 || 7==9 is false |
^ | Bitwise XOR | 3==5 ^ 7==9 is false |
~ | Bitwise NOT | (~2) = -2 |
<< | Bitwise Left Shift | (10<<2) = 40 |
>> | Bitwise Right Shift | (10>>2) = 2 |
Special Operators
Operator | Description |
---|---|
?: | Conditional Operator returns value based on the condition. |
sizeof() | Returns the size of a variable. |
* | Pointer to a variable. |
& | Returns the address of a variable. |
, | Comma operator. |