Operators in C++ – Types and Examples

In this article, we will discuss operators in C++.

Operators form the basis of a programming language. Operators are symbols that operate on operands. These operands can be variables or values. Operators help us to perform mathematical and logical computations.

Types of Operators in C++

In C++, operators are classified into 6 types:

1. Arithmetic Operators in C++

We use arithmetic operators to perform mathematical operations.

Operator Operation Description
+ Addition Adds two values
Subtraction Subtracts one value from another
* Multiplication Multiplies two values
/ Division Divides one value by another
% Modulus Computes the remainder
++ Increment Increases value of a variable by 1
Decrement Decreases value of a variable by 1

Here, Increment (++) and decrement (–) are unary operators, which means these work with a single operand. Rest are binary operators.

Example of C++ Arithmetic Operators

#include <iostream>
using namespace std;

int main() 
{
    int a, b;
    a = 8;
    b = 3;

    // Addition
    cout << "a + b = " << (a + b) << endl;

    // Subtraction
    cout << "a - b = " << (a - b) << endl;

    // Multiplication
    cout << "a * b = " << (a * b) << endl;

    // Division
    cout << "a / b = " << (a / b) << endl;

    // Modulus
    cout << "a % b = " << (a % b) << endl;
    
    // Increment
    cout << "++a = " << ++a << endl;
    
    //Decrement
    cout<< "--b = " << --b <<endl;

    return 0;
}

Output

a + b = 11
a – b = 5
a * b = 24
a / b = 2
a % b = 2
++a = 9
–b = 2

2. Assignment Operators in C++

To assign value to a variable, we use assignment operators. Both the operands should be of same data type.

Operator Example Equivalent to
= x = y x = y
+= x += y x = x + y
-= x -= y x = x – y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y

Example of C++ Assignment Operators

#include <iostream>
using namespace std;

int main() 
{
    int x, y;
    x = 8;
    y = 3;

    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    
    x /= y;
    cout << "x = " << x;

    return 0;
}

Output

x = 8
y = 3
x = 2

3. Relational Operators in C++

Relational operators are used to compare two operands. The result of comparison is either true or false. In case of true, it returns 1 and 0 when false.

Operator Operation Example
== Equal to 5==7 returns 0
!= Not equal to 5!=7 returns 1
> Greater than 7>5 returns 1
< Less than 7<5 returns 0
>= Greater than or equal to 5>=7 returns 0
<= Less than or equal to 5<=7 returns 1

Example of C++ Relational Operators

#include <iostream>
using namespace std;

int main() 
{
   int x, y;
   x=5;
   y=9;
   bool result;
   
   result = (x!=y);
   cout<<"5!=9 is "<<result<<endl;
   result = (x>y);
   cout<<"5>9 is "<<result;
    
    return 0;
}

Output

5!=9 is 1
5>9 is 0

4. Logical Operators in C++

Logical operators determine the logic between operands and check if expressions are true or false.

Operator Operation Description
&& Logical AND Returns true(1) only if both operands are true
|| Logical OR Returns true(1) if atleast one of the operands is true
! Logical NOT Returns true(1) if the operand is false and false(0) when the operand is true.

Logical NOT (!) is a unary operator.

Example of C++ Logical Operators

#include <iostream>
using namespace std;

int main() 
{
   int x=3, y=9;
   bool result;
   
   result = (x>4) && (y>6);        //false
   cout<<"(x>4) && (y>6) is "<<result<<endl;
   result = (x>4) || (y>6);            //true
   cout<<"(x>4) || (y>6) is "<<result<<endl;
   result = !(x<4);                       //false
   cout<<"!(x<4) is "<<result;
    
    return 0;
}

Output

(x>4) && (y>6) is 0
(x>4) || (y>6) is 1
!(x<4) is 0

5. Bitwise Operators in C++

We use bitwise operators to perform bit-level operations. Values of both operands are first converted into binary before operating on them. 

Operator Operation Description
& Bitwise AND AND operation bit by bit. 
| Bitwise OR OR operation bit by bit.
^ Bitwise XOR Exclusive OR operation bit by bit.
~ Bitwise One’s Complement Converts operand into its one’s complement form. It is a unary operator.
<< Bitwise left shift Shifts all bits by a specified number of bits to the left.
>> Bitwise right shift Shifts all bits by a specified number of bits to the right.

The right hand side operand of << and >> operators specifies the number of bits to be shifted.

Example of C++ Bitwise Operators

#include <iostream>
using namespace std;

int main() 
{
   int x=14, y=20;
   
   cout<<"x&y = "<< (x&y) <<endl;
   cout<<"x|y = "<< (x|y) <<endl;
   cout<<"x^y = "<< (x^y) <<endl;
   cout<<"1's Complement of x is "<< (~x) <<endl;
   cout<<"Left shift x by 2 bits, result = "<< (x<<2) <<endl;
   cout<<"Right shift y by 1 bit, result = "<< (y>>1);
    
    return 0;
}

Output

x&y = 4
x|y = 30
x^y = 26
1’s Complement of x is -15
Left shift x by 2 bits, result = 56
Right shift y by 1 bit, result = 10

6. Other Operators in C++

Some other operators in C++ are:

Operator Description Example
sizeof() Computes size of its operand in bytes sizeof(int);  //4
Condition ? exp1 : exp2  It is the ternary conditional operator. If the condition is true, it returns exp1, else exp2. (9>5) ? cout<<“true” : cout<<“false”;  //true
, (Comma) It first evaluates the first operand followed by the second and then returns the result of the second operand. int i = (5, 10);  //i=10
. (dot) Used to access members of struct and class. obj.age=25;
-> (arrow) Used with pointers to access members of struct and class. ptr->age=25;
& (‘Address of’ Pointer operator) Returns the memory address. a = 100;

p = &a;  //p=some memory address

* (‘Indirection’ Pointer operator) Pointer to a variable. a = 100;

p = &a;

v = *p;  //v=100

(type) Cast Converts one data type to another. double a = 12.85467;

int b;

b = (int) a;  //b=12

Precedence of Operators in C++

In the table given below, the precedence of operators decreases from top to bottom.

Operator Description Associativity
:: Scope resolution Left to right
Postfix Unary

++ —

()

[]

. ->

Increment DecrementFunction call

Subscript

Member access

Prefix Unary

++ —

+ –

! ~

(type)

*

&

sizeof()

Increment DecrementUnary plus minus

Logical NOT, Bitwise complement

Cast

Dereference

Address of/reference

size of operand in bytes

Right to left
.* .-> Pointer to member Left to right
* / %

+ –

Arithmetic
<< >> Bitwise left shift, Bitwise right shift
< <=

> >=

== !=

Relational 
&

^

|

Bitwise AND

Bitwise XOR

Bitwise OR

&&

||

Logical AND

Logical OR

?: Ternary Conditional Right to left

+= -= 

*= /= %= 

<<= >>= 

&= ^= |= 

Assignment
, Comma  Left to right

Example to illustrate operator precedence in C++

#include <iostream>
using namespace std;

int main() {
  int result1 = 5 + 10 / 3;
  int result2 = (5 + 10) / 3;
  cout<<result1<<endl;
  cout<<result2<<endl;
  result1==result2 ? cout<<"Equal" : cout<<"Unequal";
  return 0;
}

Output

8
5
Unequal

Observe that we obtained different results for both the expressions because of operator precedence.

Summary

From this article, we learnt operators in C++. In C++, operators are mainly divided into 6 categories. We covered operators of each category with examples followed by the precedence of these operators.