Types of operators in C++

Types of operators in C++

Different types of operators are available in C++ as listed below. Arithmetic Operator, Relational Operators, Logical Operators and Assignment Operator.

Operator in any computer language works like a command or statement to tell the computer that what kind of action is required to be performed. The data on which action is performed by the operators is called operands. Some operators work on single operand and are called unary operators. Most of the operators need two operands to work on and are called binary operators. There is only one ternary operator in C++ which needs three operands.

Types of Operators in C++

Different types of operators are available in C++ as listed below.

  1. Arithmetic Operator
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operator
  5. Arithmetic Assignment Operators
  6. Increment/Decrement Operators
  7. Bitwise Operators
  8. Ternary Operator
  9. Special Operators (Comma, SizeOf)

 

Arithmetic Operators

 

All the arithmetic operators are binary operators having two operands. They arithmetically calculate a value and return the result.

Operators Description Examples
+ Used to arithmetically calculate sum of two values. a+b

if a=15 and b=2 then the result of above expression is 17

- Used to arithmetically calculate difference of two values. a-b

if a=15 and b=2 then the result of above expression is 13

* Used to arithmetically calculate product of two values. a*b

if a=15 and b=2 then the result of above expression is 30

/ Used to arithmetically divide one value by another and produces the quotient a/b

if a=15.0 and b=2.0 then the result of above expression is 7.5

% Used to arithmetically divide one value by another and produces the remainder. It is also called modulus operator and must have whole numbers as operands. a%b

if a=15 and b=2 then the result of above expression is 1

 

Relational Operators

 

All the relational operators are binary operators having two operands. They compare the numeric value at the left hand side with the value at the right hand side and return true or false. In C++, 0 is represented as false and any non-zero value is considered as true but usually 1 is used as true.

Operators Description Examples
<

Less than

Checks whether the value at left hand side is less than or not from the value at the right hand side. a<b

if a=15 and b=2 then the result of above expression is 0 (false)

 

if a=2 and b=2 then the result of above expression is 0 (false)

 

if a=15 and b=22 then the result of above expression is 1 (true)

>

Greater than

Checks whether the value at left hand side is greater than or not from the value at the right hand side. a>b

if a=15 and b=2 then the result of above expression is 1 (true)

 

if a=2 and b=2 then the result of above expression is 0 (false)

 

if a=15 and b=22 then the result of above expression is 0 (false)

<=

Less than or equal to

Checks whether the value at left hand side is less than or equal to the value at the right hand side or not. a<=b

if a=15 and b=2 then the result of above expression is 0 (false)

 

if a=2 and b=2 then the result of above expression is 1 (true)

 

if a=15 and b=22 then the result of above expression is 1 (true)

>=

Greater than or equal to

Checks whether the value at left hand side is greater than or equal to the value at the right hand side or not. a>=b

if a=15 and b=2 then the result of above expression is 1 (true)

 

if a=2 and b=2 then the result of above expression is 1 (true)

 

if a=15 and b=22 then the result of above expression is 0 (false)

==

Equal to

Checks whether the value at left hand side is equal to the value at right hand side or not. a==b

if a=2 and b=2 then the result of above expression is 1 (true) otherwise 0 (false)

 

!=

Not equal to

Checks whether the value at left hand side is equal to the value at right hand side or not. a!=b

if a=2 and b=2 then the result of above expression is 0 (false) otherwise 1 (true)

 

 

Logical Operators

 

These operators are used to form a complex expression comprising one or more logical expressions. Like relational operators, logical operators also return either true (1) or false (0) result.

Operators Description Examples
&&

AND

Used to combine two or more relational expressions to form  a single expression. It will return 1 (true) if all the conditions are true otherwise 0 (false). a>b)&&(a>c)

if a=15, b=2 and c=10, then the result of above expression is 1 (true)

 

a>b)&&(a>c)&&(c>d)

if a=15, b=2 , c=10 and d=20, then the result of above expression is 0 (false)

 

||

OR

Used to combine two or more relational expressions to form  a single expression. It will return 1 (true) if any of the given conditions is true otherwise 0 (false). a>b)||(a>c)

if a=15, b=2 and c=20, then the result of above expression is 1 (true)

 

a>b)||(a>c)

if a=1, b=2  and c=3 then the result of above expression is 0 (false) as all the conditions are false

 

!

NOT

Used to negate the value of another expression. If the result is true then it will convert it into false and vice versa. !(a>b)

if a=15, b=2 , then the result of above expression is 0 (false)

 

 

Assignment Operator

 

This is a binary operator used to assign a value to a variable. The value to be assigned is written on its right side and the variable to which value is assigned is written on its left side

Operators Description Examples
=

Equal to

This is a binary operator used to assign a value at the right hand side to the variable at the left hand. int x=10;

This will assign 10 to an integer variable ‘x’.

 

int a=10, b=20;

int c=a+b;

This will assign 30 to an integer variable ‘c’.

Arithmetic Assignment Operators

 

As the name suggests, these operators first arithmetically calculate a result and then assign to a variable at their left hand side.

Operators Description Examples
+= Used to add the value at right side to the existing value of the variable at left hand side and assign the result to the variable at the left hand side. int x=10;

x+=100;

This will add 100 to the existing value of x (i.e. 10) and assign the result (i.e. 110) to x. It is equal to the following statement.

 

x=x+100;

-= Used to subtract the value at right side from the existing value of the variable at left hand side and assign the result to the variable at the left hand side. int x=100;

x-=10;

This will subtract 10 from the existing value of x (i.e. 100) and assign the result (i.e. 90) to x. It is equal to the following statement.

 

x=x-10;

*= Used to multiply the value at right side to the existing value of the variable at left hand side and assign the result to the variable at the left hand side. int x=100;

x*=2;

This will multiply 2 to the existing value of x (i.e. 100) and assign the result (i.e. 200) to x. It is equal to the following statement.

 

x=x*2;

/= Used to divide the existing value of the variable at left hand side by the value at the right hand side and assign the result to the variable at the left hand side. int x=100.0;

x/=2;

This will divide 100 by 2 and assign the result (i.e. 50) to x. It is equal to the following statement.

 

x=x/2;

%= Used to divide the existing value of the variable at left hand side by the value at the right hand side and assign the remainder to the variable at the left hand side. int x=9;

x%=2;

This will divide 9 by 2 and assign the remainder (i.e. 1) to x. It is equal to the following statement.

 

x=x%2;

 

Increment/Decrement Operators

 

These are unary operators used to add or subtract 1 to/from the current value of a numeric variable and store the result in the same variable.

Operators Description Examples
++

Increment by 1

Used to increment the current value of a variable by 1. int x=10;

x++; or ++x;

This will add 1 to the current value of x (i.e. 10) and store the result (i.e. 11) back to x.  It is equal to the following statement.

 

x=x+1;

Decrement by 1

Used to decrement the current value of a variable by 1. int x=10;

x–; or –x;

This will subtract 1 from the current value of x (i.e. 10) and store the result (i.e. 9) back to x. It is equal to the following statement.

 

x=x-1;

 

Bitwise Operators

 

These operators allow us to compare the bits of two values.

Operators Description Examples
&

Bitwise AND

Performs the logical AND operation on every bit of both the operands. If the binary value of first operand is 01001000 and the binary value of second operand is 10001000, then

 

01001000 &

10001000

————-

00001000

|

Bitwise OR

Performs the logical OR operation on every bit of both the operands. If the binary value of first operand is 01001000 and the binary value of second operand is 10001000, then

 

01001000 |

10001000

————-

11001000

~

Bitwise NOT

Converts every bit of its operand to the opposite bit If the binary value of the operand is 01001000, then

~01001000 means 10110111

^

Bitwise XOR

Performs the logical XOR operation on every bit of both the operands. Used to add the corresponding bits and returns 0 if both the bits are same and returns 1 if they are opposite. If the binary value of first operand is 01001000 and the binary value of second operand is 10001000, then

 

01001000 ^

10001000

————-

11000000

<<

Bitwise Left Shift

It is a unary operator used to shift specified number of bits at left If the binary value of the operand is 01001000 then 01001000<<2 means that every bit will be shifted two places at the left and two 0s will be padded at the right. The result will be as below

 

00100000

>>

Bitwise Right Shift

It is a unary operator used to shift specified number of bits at right If the binary value of the operand is 01001000 then 01001000>>2 means that every bit will be shifted two places at the right and two 0s will be padded at the left. The result will be as below

 

00010010

 

Ternary Operator

 

This is called ternary operator as it has three operands. This is short form of if-else statement. We will discuss if-else statement in detail with examples later in this tutorial.

Operators Description Examples
?: Short for if-else statement. int x=10;

int y=20;

(x>y)?cout<<x:cout<<y;

 

First  x>y condition is checked, if true then the value of x will be displayed otherwise the value of y is displayed. As x is not greater than y so the value of y (i.e. 20) will be displayed.

 

Special Operators

 

C++ has some special operators to perform some specific tasks as given below.

Operators Description Examples
,

Comma

Comma (,) operator is used for two purposes.

 

  1. To separate variable names
  2. To separate two or more expressions
Example 1:

 

int a, b, c;

 

Example 1:

 

int a, b, c;

c=(a=10, b=20, a+b);

 

The above code is equivalent to below.

int a, b, c;

a=10;

b=20;

c=a+b;

 

sizeof

 

This is a unary operator used to check the size of the operand in number of bytes int a;

cout<<sizeof(a);

or

cout<<sizeof(int);

 

Returns the size of int data type.

 

 

More Related Articles For You

    C++ Tutorial

    C++ Quizzes

 

Search

Shortcodes Ultimate

 
Follow Us
 
BGP