Operators in C++ | Programming Concepts

Operator

An operator is a symbol that instructs the compiler to perform certain mathematical or logical operations on a value or a variable. Operators are the substructure of any programming language. Example, - is an operator used for subtraction, while / is an operator used for division and so on.

C++ has plentiful built-in operators and can be classified into 6 different types:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Relational Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Other Mis Operators

1. Arithmetic Operators

The operators which are used to perform arithmetic operations on variables are known as Arithmetic Operators.

Arithmetic Operator can be classified into two types:

  1. Binary Operators: These are operators that are used to operate or perform arithmetic/mathematical operations with two operands. For example: Addition(+), Subtraction(-), Division(/), Multiplication(*).
  2. Unary Operators: These are operators that operate with a single operand. For example: (++ , –)

Example for Binary Operator,

#include<iostream>
using namespace std;
int main()
{
int a=1, b=2;
cout<< a+b; // arithmetic operation a+b
return 0;
}

OUTPUT
------
3

In the above example, the Addition(+) operator is being used to add two variables a and b. Similarly there are various different arithmetic operators used in C++.

OperatorOperation
+Addition
-Subtraction
*Multiplication
/Division
%Modulo Operation (Remainder after division)
Arithmetic operators supported by C++ language

Example for Unary Operators,

  • Increment: The Increment ‘++’ operator is used to increment the value of an integer. When placed before the variable name (pre-increment operator), the value of the variable is incremented immediately. Example, ++x
    And when it is placed after the variable name (post-increment operator), the value of the variable is preserved temporarily until the execution of this statement and it gets incremented before the execution of the next statement. For example, x++.
  • Decrement: The Decrement ‘ – – ‘ operator is used to decrement the value of an integer. When placed before the variable name (pre-decrement operator), the value of the variable is decremented immediately. For example, – – x
    And when it is placed after the variable name (post-decrement operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, x – –.

2. Assignment Operators

Assignment operators are used to assign values to variables.

For example,
Here, we have assigned a integer number 10 to the variable x.

// assign 10 to x
x = 10;

The left side operand is a variable and the right side is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will show an error.

Assignment operators supported by C++ language are as follows:

OperatorExampleEquivalent to
=a = b;a = b;
+=a += b;a = a + b;
-=a -= b;a = a - b;
*=a *= b;a = a * b;
/=a /= b;a = a / b;
%=a %= b;a = a % b;

3. Relational Operators

A relational operator is used to compare the relationship between the values of two operands.
For example,

// checks if x is greater than y
x > y;

Here, > is a relational operator. It checks if x is greater than y or not. If the x is greater than y is true, it returns 1 whereas if y is greater than x is false, it returns 0.

Relational operators supported by C++ language are:

OperatorMeaningExample
==Is Equal To3 == 5 gives us false
!=Not Equal To3 != 5 gives us true
>Greater Than3 > 5 gives us false
<Less Than3 < 5 gives us true
>=Greater Than or Equal To3 >= 5 give us false
<=Less Than or Equal To3 <= 5 gives us true

4. Logical Operators

Logical Operators are used for combining two or more constraints or to complement the evaluation of the original condition in consideration. The outcome of the operation of a logical operator is a boolean value either true or false. 

In short, They are used to determine the logic between two variables.

Logical operators supported by C++ language are:

OperatorExampleMeaning
&&expression1 && expression2Logical AND.
True only if all the operands are true.
||expression1 || expression2Logical OR.
True if at least one of the operands is true.
!!expressionLogical NOT.
True only if the operand is false.

In C++ Programming Language, logical operators are frequently used in decision making.

5. Bitwise Operators

Bitwise operators are used to operate on individual bits and perform bit by bit operation on the operands. The operators, in the beginning, are converted to bit-level, and then the calculation is performed on the operands. They can only be used with char and int data types.

Bitwise operator supported in C++ are:

OperatorDescription
&Binary AND
|Binary OR
^Binary XOR
~Binary One’s Complement
<<Binary Shift Left
>>Binary Shift Right

Misc Operators

The following table lists some other operators that are supported in C++.

Sr.NoOperator & Description
1sizeof– returns the size of a variable. For example, sizeof(x), where ‘x’ is an integer, and will return 4.
2Condition ? X : Y- If Condition is true then it returns the value of X otherwise returns the value of Y.
3, -Comma operator helps to perform a sequence of operations.
4. (dot) and -> (arrow)– Member Operator are used to reference individual members of classes, structures, unions.
5Cast– Casting operators are used to convert one data type to another.
6&– Returns the address of a variable.
7*(Pointer Operator)- Pointer to a variable.

Operators Precedence in C++

Operator Precedence specifies the order in which the operations in expressions that contain more than one operator will be performed.

Operator Precedence are as follows:

Category Operator Associativity 
Postfix () [] -> . ++ – –  Left to right 
Unary + – ! ~ ++ – – (type)* & sizeof Right 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 
Comma Left to right 
We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?