c++

C++ Operators

In this tutorial, we will learn about the different types of operators in C++ with the help of examples. In programming, an operator is a symbol that operates on a value or a variable or operands.

Operators are symbols that perform operations on operands and produces the result. For example, + is an operator used for addition, while - is an operator used for subtraction.

Operators in C++ can be classified into 6 types:

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

1. C++ Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on variables and data. For example,

a + b;

Here, the + operator is used to add two variables a and b. Similarly there are various other arithmetic operators in C++.

Operator Operation
+ Addition(needs two operands to work)
- Subtraction(needs minimum one and maximum two operand to work)
* Multiplication(needs two operands to work)
/ Division(needs two operands to work)
% Modulo Operation (Remainder after division and needs two integer operands to work)

Example 1: Arithmetic Operators

#include <iostream>
using namespace std;
int main() {
     int x, y;
     x = 7;
     y = 2;

    // printing the sum of x and y
    cout << "x + y = " << (x + y) << endl;

     // printing the difference of x and y
     cout << "x - y = " << (x - y) << endl;

     // printing the product of x and y
     cout << "x * y = " << (x * y) << endl;

     // printing the division of x by y
     cout << "x / y = " << (x / y) << endl;

     // printing the modulo of x by y
     cout << "x % y = " << (x % y) << endl;

     return 0;
}

Output:

x + y = 9
x - y = 5
x * y = 14
x / y = 3
x % y = 1

Here, the operators +, - and * compute addition, subtraction, and multiplication respectively as we might have expected.


/ Division Operator

Note the operation (x / y) in our program. The / operator is the division operator.

As we can see from the above example, if an integer is divided by another integer, we will get the quotient. However, if either divisor or dividend is a floating-point number, we will get the result in decimals.

In C++,

//integer divided by int is an integer.

7/2 is 3

//float divided by int is a float.

7.0 / 2 is 3.5

7 / 2.0 is 3.5

//float divided by float is a float.

7.0 / 2.0 is 3.5

Note: The above example also shows implicit type coversion


% Modulo Operator

The modulo operator % computes the remainder. When x = 9 is divided by y= 4, the remainder is 1.

Note:The % operator can only be used with integers.


Increment and Decrement Operators

C++ also provides increment and decrement operators: ++ and -- respectively. ++ increases the value of the operand by 1, while -- decreases it by 1.

For example,

int a = 5;

// increasing num by 1
++a;

Here, the value of a gets increased to 6 from its initial value of 5.

Example 2: Increment and Decrement Operators

// Working of increment and decrement operators

#include <iostream>
using namespace std;
int main() {

    int x = 10, y = 100, result_x, result_y;
    // incrementing a by 1 and storing the result in result_x
    result_x = ++x;
    cout << "result_x = " << result_y << endl;

     // decrementing b by 1 and storing the result in result_y
     result_y = --y;
     cout << "result_y = " << result_y << endl;

     return 0;
}

Output:

result_x = 11
result_y = 99

In the above program, we used ++ and -- operator as prefixes. We can also use these operators as postfix.
There is a slight difference when these operators are used as a prefix versus when they are used as a postfix.

In prefix the value of the operand is first increased/decreased and then the next operation is performed whereas in postfix first the operation is performed on the operand then the value is increased/decreased. It depends on the situation whether you have to use postfix or prefix.


2. C++ Assignment Operators

In C++, assignment operators are used to assign values to variables. For example,

// assign 5 to x
x = 5;

Here, we have assigned a value of 5 to the variable a.

Operator Operation 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 2: Assignment Operators

#include <iostream>
using namespace std;
int main() {
     int x, y, temp;

     // 2 is assigned to x
     x = 2;

     // 7 is assigned to y
     y = 7;

     // value of a is assigned to temp
     temp = a; // temp will be 2
     cout << "temp = " << temp << endl;

     // assigning the sum of x and y to x
     x += y; // x= x + y
     cout << "x = " << x << endl;

     return 0;
}

Output:

temp = 2
x = 9

3. C++ Relational Operators

A relational operator is used to check the relationship between 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 relation is true, it returns 1 whereas if the relation is false, it returns 0

Operator Meaning Example
== Is Equal To 3 == 5 gives us false
!= Not Equal To 3 != 5 gives us true
> Greater Than 3 > 5 gives us false
< Less Than 3 < 5 gives us true
>= Greater Than or Equal To 3 >= 5 give us false
<= Less Than or Equal To 3 <= 5 gives us true

Note:The relational operator always returns only 2 values i.e. 1 or 0. It will never return the operands used with these operators.

Example 4: Relational Operators

#include <iostream>
using namespace std;
int main() {

     int a, b;
     x = 3;
     y = 5;

     int result;

     result = (x == y);   // false
     cout << "3 == 5 is " << result << endl;

     result = (x != y);   // true
     cout << "3 != 5 is " << result << endl;

     result = x > y;      // false
     cout << "3 > 5 is " << result << endl;

     result = x < y;      // true
     cout << "3 < 5 is " << result << endl;

     result = x >= y;     // false
     cout << "3 >= 5 is " << result << endl;

     result = x <= y;     // true
     cout << "3 <= 5 is " << result << endl;

     return 0;
}

Output:

3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1

Note:Relational operators are used in decision making and loops.


4. C++ Logical Operators

Logical operators are used to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0.

Operator Example Meaning
&& expression1 && expression 2 Logical AND.
True only if both the expressions are true.
|| expression1 || expression 2 Logical OR.
True if at least one of the expressions is true.
! !expression Logical NOT.
True only if the expression is false.

In C++, logical operators are commonly used in decision making. To further understand the logical operators, let's see the following examples,

Suppose,
x = 9
y = 15
Then,
(a > 3) && (b > 5) evaluates to true
(a > 3) && (b < 5) evaluates to false
(a > 3) || (b > 5) evaluates to true
(a > 3) || (b < 5) evaluates to true
(a < 3) || (b < 5) evaluates to false
!(a == 3) evaluates to true
!(a > 3) evaluates to false

Example 5: Logical Operators

#include 
using namespace std;
int main() {
 int result;
     result = (5 != 15) && (5 < 15); // true
     cout << "(5 != 15) && (5 < 15) is " << result << endl;

     result = (5 == 15) && (5 < 15); // false
     cout << "(5 == 15) && (5 < 15) is " << result << endl;

     result = (5 == 15) && (5 > 15); // false
     cout << "(5 == 15) && (5 > 15) is " << result << endl;

     result = (5 != 15) || (5 < 15); // true
     cout << "(5 != 15) || (5 < 15) is " << result << endl;

     result = (5 != 15) || (5 > 15); // true
     cout << "(5 != 15) || (5 > 15) is " << result << endl;

     result = (5 == 15) || (5 > 15); // false
     cout << "(5 == 15) || (5 > 15) is " << result << endl;

     result = !(5 == 20); // true
     cout << "!(5 == 20) is " << result << endl;

     result = !(5 == 5); // false
     cout << "!(5 == 5) is " << result << endl;

    return 0;
}

Output:

(5 != 15) && (5 < 15) is 1
(5 == 15) && (5 < 15) is 0
(5 == 15) && (5 > 15) is 0
(5 != 15) || (5 < 15) is 1
(5 != 15) || (5 > 15) is 1
(5 == 15) || (5 < 15) is 0
!(5 == 20) is 1
!(5 == 5) is 0

Explanation of logical operator program


5. C++ Bitwise Operators

In C++, bitwise operators are used to perform operations on individual bits. They can only be used alongside char and int data types.

Operator Description
& Binary AND
| Binary OR
^ Binary XOR
~ Binary One's Complement
<< Binary Shift Left
>> Binary Shift Right