What is the Operators in C++ ???
An Operators is a symbol which is used by user for giving instruction to computer for doing certain mathematical or logical manipulations on one or more operands. Operands is the quantity on which an operation is to be performed. For Example:
3 + 6 = 9
In the above example, “+” is the operator for the operation called addition. Hence 3 an 6 both are operands used for doing that operations.
Types of Operators
In C++, we have 8 Types of Operators
1. Arithmetic Operator
These operators are used to perform basic arithmetic operations.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
– | Subtraction | a – b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus | a % b |
++ | Increment | ++a or a++ |
— | Decrement | –a or a– |
2. Relational Operator
These operators are used to compare two values.
Operator | Description | Example |
---|---|---|
== | Equal to | a = = b |
!= | Not Equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
3. Logical Operators
These operators are used to perform logical operations.
Operator | Description | Example |
---|---|---|
&& | Logical AND | a && b |
|| | Logical OR | a || b |
! | Logical NOT | !a |
4. Assignment Operator
These operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assign | a = b |
+= | Add and assign | a += b |
-= | Subtract and assign | a -= b |
*= | Multiply and assign | a *= b |
/= | Divide and assign | a /= b |
%= | Modulus and assign | a %= b |
&= | Bitwise AND and assign | a &= b |
|= | Bitwise OR and assign | a |= b |
^= | Bitwise XOR and assign | a ^= b |
<<= | Left shift and assign | a <<= b |
>>= | Right shift and assign | a >>= b |
5. Conditional Operator
This operator is used to evaluate a condition and return one of two values.
Operator | Description | Example |
---|---|---|
?: | Ternary | condition ? expr1 : expr2 |
6. Comma Operator
This operator is used to separate two or more expressions.
Operator | Description | Example |
---|---|---|
, | Comma | a = (b = 3, b + 2) |
6. Unary Operator
These operators operate on a single operand.
Operator | Description | Example |
---|---|---|
+ | Unary plus | +a |
– | Unary minus | -a |
++ | Increment | ++a or a++ |
— | Decrement | –a or a– |
! | Logical NOT | !a |
& | Address-of | &a |
* | Dereference | *a |
sizeof | Size of | sizeof(a) |
8. Bitwise Operators
These operators are used to perform bit-level operations.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~a |
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |