C++ Operators
Operators are symbols used in C++ to carry out operations. Operators are applied on operands to perform various operations.
What are operators in C++?
The different types of operators used in C++ are the following.
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Assignment Operators
1. Arithmetic operators
In C++, arithmetic operators are used to performing arithmetic operations as described below.
Let us take an example of operands a, b with values 10 and 5 respectively.
Arithmetic operators in C++  
Operator  Description  Example 
+  Addition of two operands  a + b will give15. 
–  Subtraction of right operand from the left operand  a – b will give5. 
*  Multiplication of two operands  a * b
will give 50. 
/  Division of left operand by the right operand  a / b
will give 2. 
%  Modulus – the remainder of the division of left operand by the right  a % b will give 0. 
++  Increment Operator which increases the value of the operand by 1.  b++ will give 6. 
—  Decrement Operator which decreases the value of the operand by 1.  b — will give 4. 
2. Relational Operators
The relational operators are used to compare values between operands and return TRUE or FALSE according to the condition specified in the statement.
4.5 (705 ratings)
View Course
Relational Operators in C++  
Operator  Description  Example 
>  If the value of left operand is greater than that of the value of the right operand, the condition becomes true; if not then false.  a > b 
<  If the value of left operand is less than that of the value of the right operand, the condition becomes true; if not then false.  a < b 
==  If both the operands have equal value, the condition becomes true; if not then false.  a == b 
!=  If both the operands do not have equal value, the condition becomes true; if not then false.  a != b 
>=  If the value of left operand is greater than or equal to the right operand, the condition becomes true; if not then false.  a >= b 
<=  If the value of left operand is less than or equal to the right operand, the condition becomes true; if not then false.  a <= b 
Let us assume the value of operands a = 10, b = 5 and perform various operations to understand the relational operators.
 a > b will give result TRUE as 10 is greater than 5.
 a < b will give result FALSE as 10 is greater than 5.
 a == b will give result FALSE as 10 is not equal to 5.
 a != b will give result TRUE as 10 is not equal to 5.
 a >= b will give result TRUE as 10 is greater than 5.
 a <= b will give result FALSE as 10 is not equal to or less than 5.
3. Logical Operators
The logical operators used in C++ are shown below.
Logical Operators in C++  
Operator  Description  Example 
  It is the logical OR Operator. The condition becomes true if any of the two operands is nonzero.  a  b 
&&

It is the logical AND Operator. The condition becomes true if both of the two operands are nonzero.  a && b 
!

It is the logical NOT operator and reverses the state of the logical operator with which it is used.  !a 
Let us assume the value of operands a = 1, b = 0 and perform various operations to understand the logical operators.
 a  b will be TRUE as one of the two operands is nonzero.
 a && b will be FALSE as one of the operand is zero.
 !a will be 0 as it reverses the state of the operand.
4. Assignment Operators
The assignment operators used in C++ are as shown below.
Assignment Operators in C++  
Operator  Description  Example 
=  This is a simple assignment operator which assigns the value of the right side operand to left side operand.  x = y will assign the value of y to x. 
+=  This operator performs the addition of the right operand to the left operand and the result is assigned to the left operand.  x += y is interpreted as x = x + y 
=  This operator performs subtraction of the right operand from the left operand and the result is assigned to left operand.  x = y is equal to x = x – y 
*=  This operator performs multiplication of right operand with the left operand and the result gets assigned to the left operand.  x *= y is equal to x = x * y 
/=  This operator performs division of left operand with the right operand and the result is assigned to left operand.  x /= y is equal to x = x / y 
%=  This takes the modulus of the two operands and the result is assigned to the left operand.  x %= y is equal to x = x % y 
>>=  This is a binary right shift and assignment operator.  x >> 5 equals to x = x >> 5 
<<=  This is a binary left shift and assignment operator.  x << 5 equals to x = x << 5 
^=  This is called bitwise exclusive OR and assignment operator.  x ^= 5 equals to x = x ^ 5 
=  This is called a bitwise OR assignment operator.  x = 5 equals to x = x  5 
&=  This is called bitwise AND assignment operator.  x &= 5 equals to x = x & 5 
Let us assume the value of x as 5. Few examples of operations performed using a few assignment operators shown above.
 x = 5 will assign the value 5 to x.
 x += 3 will give the result as x = x +3 i.e. 5+3= 8 will be assigned to x.
 x =2 will give the result as x = x +3 i.e. 52= 3 will be assigned to x.
Recommended Articles
This has been a guide to C++ Operators. Here we have discussed the different types of operators used in C++ with their syntax and examples. You may also look at the following article to learn more –