Operators in C++

Video Tutorial
Operators Introduction thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules


Operators in C++ are the symbols used to perform the operations on the values or the variables. C++ operator tells the compiler to perform a certain mathematical or logical operation. In C++, we have so many operators of different types like arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and there are so many other operators.

Introduction to Operators in C++

Let's consider a scenario where you are writing an encryption algorithm that contains complex mathematical calculations. Here operators in C++ help you to design complex code with mathematical or logical operators.

Operators in C++ are nothing but the symbols which are used to perform certain operations. This symbol provides a layer of abstraction over so many mathematical and logical operations to make them easy to use.

There are multiple types of operators in C++, as shown in the following figure.

Operators in C++

Let's discuss all these different types of operators one by one with the help of coding examples. But before let's see what is an operand.

Operands & Type of Operators

As we know, the operator is the one that performs an operation, then the operand is the one on which the operation gets performed.

If the operator is of the type that can only be used with a single operand, it is called a unary operator.

Unary operator

If the operator is of the type that can be used with two operands, it is called a binary operator.

Operator and operands

This combination of operator and operands is combinedly called an expression.

1. Arithmetic Operators in C++

As the name suggests, arithmetic operators are used to perform arithmetic operations such as addition, subtraction, multiplication or division, etc.

There are two types of Arithmetic Operators: Unary and Binary.

Arithmetic Unary Operators

Unary arithmetic operators are the ones that are used only with one operand to perform the arithmetic operations.

The increment operators are used to increment the value of a variable by 1, and decrement operators are used to decrement the value of a variable by 1.

Pre-increment or pre-decrement operators first manipulate the value and then get assigned to the variables, whereas post-increment or post-decrement operators first get assigned to the variable, and then the value gets manipulated.

The unary arithmetic operators are shown in the following table.

Sr. noNameSymbolDescription
1pre-increment++(operand)value is first incremented and then assigned
2post-increment(operand)++value is first assigned and then incremented
3pre-decrement--(operand)value is first decremented and then assigned
4post-decrement(operand)--value is first assigned and then decremented

Code Example


Arithmetic Binary Operators

The binary arithmetic operators are the ones that are used with 2 operands to perform the arithmetic operations.

Examples of binary arithmetic operators are shown in the following table.

Sr. noNameSymbolDescription
1addition+Used to get the addtion of two operands
2subtraction-Used to get the subtraction of two operands
3multiplication*Used to get the multiplication of two operands
4division/Used to get the division of two operands
5reminder(modulo)%Used to get the remainder of two operands when they are divided.

Code Example


Special note on the Division Operator (//)

When we divide with the division operator, if any one of the numerators or denominators is a floating-point number, we get the floating-point number as a result.

7/2 is 3 because both the numerator and denominator values are in the form of integer, so the answer should also come as an integer. In other cases, it will come as a floating number.

For example -

Special Note on the Modulo Operator (%)

This modulo operator is used to find the remainder of a division. For example: 11 % 4 is 3. Because 3 is a remainder.

We can only use the modulo(%) operator with integer type values.

2. Assignment Operators in C++

Assignment operators are used to assign the value to the variable. These are always binary operators. For example: num = 6 will assign the value 6 to the variable num.

There are different versions of these assignment operators to combine with the arithmetic operations.

The list of commonly used assignment operators with their equivalent expressions is shown in the following table.

OperatorExampleEquivalent to
=a = ba = b
+=a += ba = a + b
-=a -= ba = a - b
*=a *= ba = a * b
%=a %= ba = a % b

Code Example


3. Relational Operators in C++

Relational operators are mainly used to check for specific conditions. These are always binary operators because 2 operands are needed to evaluate the condition. If the condition is true, it will return 1, else if the condition is false, it will return 0.

For example, if we want to check the greater number among the two numbers: 4, 5 then 4 > 5 will return 0 because it is false.

The examples of different relational operators are given in the following table.

==Equal to3 == 3 is true
!=Not equal to5 != 2 is true
>Greater than3 > 4 is false
<Less than9 < 2 is false
>=Greater than or equal to4 >= 4 is true
<=Less than or equal to2 <= 4 is true

Code Example


4. Logical Operators in C++

Logical operators are used to check logical conditions. They return the 1 when the result is true and 0 when the result is false.

Logical Unary Operator

There is only one unary logical operator known as logical not(!), used to invert the condition.

For eg, !(5 > 4) will return 0, i.e., false becuase 5 > 4 is true and it gets inverted to false.

!Logical NOT!(expression)

Logical Binary Operators

Logical AND and logical OR are the binary logical operators in C++.

These are as shown in the following table.

&&Logical AND(expression 1) && (expression 2)
||Logical OR(expression 1) || (expression 2)

These operators are written between the two expressions and they produce the following results as shown in the truth tables.

Code Example


5. Bitwise Operators in C++

Bitwise operators are mainly used to perform the bit-manipulation operations by converting operands to their binary form.

Bitwise Unary Operators

There is only one unary bitwise operator ~ us to find the one's complement as shown in the following table.

~One's complement

Bitwise Binary Operators

Binary bitwise operators are used to perform the bit manipulation operations with two operands. For eg., 4&5 comes out to be 4. In the following snippet, AND operation is performed with each bit of both the above operands and the resulting binary is converted to its respective decimal value.

There are so many different bitwise operators which are used to work with bits. The list of bitwise operators is shown in the following table.

&Binary ANDPerforms the AND operation on each bit of both the operands
|Binary ORPerforms the OR operation on each bit of both the operands
^Binary XORPerforms the OR operation on each bit of both the operands
<<Shift leftShifts each digit to left by the specified number of positions
>>Shift rightShifts each digit to right by the specified number of positions

Code Example


6. Other Operators in C++

Apart from the operators who are listed above in this article, there are some other operators as well which are frequently used in C++. These operators are also important, and you will often see them in programs.

?:expression ? (if true) : (if false);Evaluates the expression and return values based on that (It is ternary operator)
sizeofsizeof(int); // 4It returns the size of datatype
&&num //address of numUsed to represent the memory address of variable
.player.score = 91;Used to access the members like structure variables or class members
->ptr->score = 32;Used with the pointers to access the class or structure variables
<<cout << "Hello";Used to print the output on command line
>>cin >> num;Used to take input from command line

Operator's Precedence Order & Associativity in C++

There are so many operators in C++, and when they are used in the same expression, there is an order of precedence by which the operations get performed.

For eg., the * operator has the highest precedence than the + operator, so in the expression 8 * 2 + 3, the * operator gets first preference, and then +, so the answer comes as 19.

Associativity for operators in C++ is the direction from which the expression gets evaluated.

For eg. a = 5; Here 5 gets assigned in variable a, so the expression gets evaluated from right to left, so it is the associativity of the = operator.

The precedence order and the associativity of operators are given in the following table. The operators are from top to bottom as per their precedence.

Postfix() [] -> . ++ - -Left to right
Unary+ - ! ~ ++ - - (type)* & sizeofRight to left
Multiplicative* / %Left to right
Additive+ -Left to right
Shift<< >>Left to right
Relational< <= > >=Left to right
Equality== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %=>>= <<= &= ^= |=Right to left


In this article,

  1. We learned about the operators and operands with various types, like unary and binary operators.
  2. We explored the various types of operators with their C++ syntax and code example.
  3. We also saw their precedence and associativity in the code.