Operators in C++

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

Operators are symbols in programming that allow us to do math or make decisions. They're like the tools we use to add numbers, compare values, or perform other tasks in code. In C++, there are special built-in symbols we use for different operations, making it easier to write programs.

Classification of Operators in C++

Operators in C++ are categorized into six main types, each serving specific purposes:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Ternary or Conditional Operators

Operators in C++

Arithmetic Operators in C++

Arithmetic operators, as implied by their name, are employed for executing fundamental mathematical operations including addition, subtraction, multiplication, and division, among others. They are fundamental tools in C++ for performing numerical computations and manipulating numerical data.

Arithmetic operators in C++ are divided into two types: Unary and Binary, each fulfilling distinct roles in mathematical calculations.

Unary Operator

Unary arithmetic operator in C++ is 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.
Unary operator 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

Output

Binary Operator

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

Operator and operands

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

Output

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.

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

OperatorMeaningExample
==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

Output

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.
There are three logical operators in c++:

  1. And operator in c++
  2. Or operator in c++
  3. Not operator in c++

These are as shown in the following table.

OperatorMeaningDescriptionExpression
&&Logical ANDAnd in C++ returns true only if all the operands are true or non-zero(expression 1) && (expression 2)
||Logical OROr in C++ returns true if either of the operands is true or non-zero(expression 1) || (expression 2)
!Logical NOTNot in C++ returns true if the operand is false or zero!(expression)

Code Example

Output

Bitwise Operators in C++

Bitwise operators are tools for manipulating individual bits in binary data, allowing for efficient low-level operations such as setting, clearing, and checking specific bits. They're crucial for tasks requiring precise control over binary representations of data and are commonly used in systems programming and optimization.

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.

OperatorMeaningDescription
~One's complementIt flips binary digits, changing 1s to 0s and 0s to 1s.
&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

Output

Assignment Operators in C++

This operator in C++ is 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 toDescription
=a = ba = bThe assignment operator (=) is responsible for assigning the value on its right side to the variable specified on its left side.
+=a += ba = a + bThe compound addition assignment operator (+=) adds the value on the right to the current value of the variable on the left and assigns the result back to the variable on the left.
-=a -= ba = a - bThe compound subtraction assignment operator (-=) subtracts the value on the right from the current value of the variable on the left, and then assigns the result back to the variable on the left.
*=a *= ba = a * bThe compound multiplication assignment operator (*=) multiplies the current value of the variable on the left by the value on the right and assigns the result back to the variable on the left.
/=a /= ba = a / bThe compound division assignment operator (/=) performs division by the value on the right side, and then assigns the result back to the variable on the left, updating its value accordingly.

Code Example

Output

Ternary/Conditional Operators in C++

The ternary operator in c++, often denoted as "? :", is a concise way to express conditional logic in many programming languages. It takes three operands:

Here's how it works:

  • The condition (Expression1) is evaluated first.
  • If the condition is true, the value of Expression2 is returned.
  • If the condition is false, the value of Expression3 is returned.

Code example

Output:

Other Common Operators in C++

Apart from the c++ 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.

OperatorExampleDescription
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

Code example:

Output:

C++ Operators Precedence Chart

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 C++ operators are given in the following table. The operators are from top to bottom as per their precedence.

CategoryOperatorAssociativity
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

Conclusion

  1. Operators: We have seen what is operator in c++. Operators in C++ are symbols used to perform operations on values or variables.
  2. Types of Operators: They are classified into Arithmetic, Relational, Logical, Bitwise, Assignment, and Ternary operators.
  3. Arithmetic Operators: Used for basic arithmetic operations like addition, subtraction, multiplication, and division.
  4. Relational Operators: Check for specific conditions and return either true (1) or false (0).
  5. Logical Operators: Used to evaluate logical conditions and return true or false.
  6. Bitwise Operators: Manipulate individual bits in binary data, crucial for low-level operations.
  7. Assignment Operators: Assign values to variables, with compound versions for combined arithmetic operations.
  8. Ternary Operator: Offers a concise way to express conditional logic with three operands.