Home | » | C++ Programming | » | C++ Operators |

One reason for the power of C++ is its wide range of useful operators. An operator is a function which is applied to values to give a result. You should be familiar with operators such as +,-,/. Arithmetic operators are the most common. Others operators are used for comparison of values, combination of logical states, and manipultation of individual binary digits.

## Contents

- What is Operators
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Increment/Decrement Operators
- Conditional Operators or Ternary Operators
- Bitwise Operators
- Special Operators

# 4.1 What is Operators

As C++ rich in data types, same as C++ is rich in Operator. From every Operator, C++-expression can be defined. By using some Operators we can solve or compute a formula, we can compare two expresssions or variable value, or can create logical relationship between compound statement, can solve a conditinal expresssion, can do low-level programming and special types of Operators.

Mainly Operators are used to operate two or more than two operands depending upon their processing and given conditions. The relationship of operator with the operands is shown as in the below common statement:

Operand1 **Operator** Operand2

**Or**

Operand1 **Operator** Operand2 **Operator** Operand3

Here Operand1, Operands, Operand3 may be variable, a constand or an expression etc. There are mainly eight operators used in C++-Language.

Level | Operator | Description | Grouping |
---|---|---|---|

1 | :: | scope | Left-to-right |

2 | () [] . -> ++ -- dynamic_cast static_cast reinterpret_cast const_cast typeid | postfix | Left-to-right |

3 | ++ -- ~ ! sizeof new delete | unary (prefix) | Right-to-left |

* & | indirection and reference (pointers) | ||

+ - | unary sign operator | ||

4 | (type) | type casting | Right-to-left |

5 | .* ->* | pointer-to-member | Left-to-right |

6 | * / % | multiplicative | Left-to-right |

7 | + - | additive | Left-to-right |

8 | << >> | shift | Left-to-right |

9 | < > <= >= | relational | Left-to-right |

10 | == != | equality | Left-to-right |

11 | & | bitwise AND | Left-to-right |

12 | ^ | bitwise XOR | Left-to-right |

13 | | | bitwise OR | Left-to-right |

14 | && | logical AND | Left-to-right |

15 | || | logical OR | Left-to-right |

16 | ?: | conditional | Right-to-left |

17 | = *= /= %= += -= >>= <<= &= ^= |= | assignment | Right-to-left |

18 | , | comma | Left-to-right |

The list of various operators as shown above are described in details as:

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Increment/Decrement Operators
- Conditional Operators or Ternary Operators
- Bitwise Operators
- Special Operators

## Arithmetic Operators

Arithmetic Operators are used for arithmetic operations like Addition Subraction, Multiplication, Division etc. Mostly arithmetic operators are used in the all the computer languages. There are five arithmetic operators used in C++-language. These all are given in the table below

Operator | Meaning |

* | multiplication |

/ | division |

% | modulus (remainder after division) |

+ | addition |

- | subtraction |

a*b | (multiplication of a with b) |

a/b | (division of a by b) |

a%b | (to find module of a with b) |

a+b | (addition of a and b) |

a-b | (subtraction of b from a) |

## Relational Operators

These operators are used to create logical relationship between two operands. Relational operators are used for comparison purpose. The expression having two operands and one relational is called Relational Expression. There are mainly six relational operators used in the C++-Language. These all are given in the table as:

Operator | Meaning |

< | less than |

<= | less than and equal to |

> | greater than |

>= | greater than and equal to |

== | equal to |

!= | not equal to |

a | True |

a<=b | True |

a>b | false |

a>=b | false |

a==b | false |

a!=b | True |

## Logical Operators

Logical operators are used for logical operations. These operations are used for compound relational expressions or logical expressions. When more than one reltional expression occur in a C expression using logical operators, then such type of expressions are called Compound Relational Expressions or Logical expression. These are used in decision-making statement adn some looping statement like if,switch, do while, while and for etc. These statement have either True (1) bracnh or false (0) branch. There are mainly three logical operators used in C++ language as in the table below :

Operator | Meaning | Priority |

! | Logical NOT (Not process the condition) | Highest |

|| | Logical OR (Process any one of the conditions.) | Intermediate |

&& | Logical AND (Process two or more than two relational expressions) | Lowest |

## Assignment Operators

Assignment operators are used for assigning an expression or value (constant) to a variable. Assignment operators are further subdivided into two types:

a) Simple Assignment Operator

b) Short hand Assignment Operator

OR

Arithmetic Assignment Operator

The general syntax :

**v= constant value or variable or expression;**

Where v is the variable and expression be any arithmetic expression. for example :

sum = 0;

i = 1

a = b;

Shorthand Assignment **R
i+= 1;
p*= a-b;
l/=1;**

## Conditional Operators

Conditional Operators are aslo called **? :** operators or ternary operator. These operators are used instead of block if statement. The general syntax of conditional operator are as :

**exp1 ? exp2 : exp3;**

for example

**c =(a>b) ? a-b :a + b;**

## Increment/Decrement Operators:

These operators are also sometimes called special operators or unary special operators. Another name or Increment/Decrement Operators is Counter Operator. These are two as: ++ (increment operator) and -- (decrement operator). Increment operator are used for incrementing the value one by one. Similarly decrement operator are used for decrementing the value one by one. These are further sub-divided into two categories:

a) Prefix Increment / Decrement Operator

b) Postfix Increment / Decrement Operator

**a) Prefix Operator:**

In the Prefix increment operator, first of all value will be increment and the incremented value will be assigned to a variable. Similarly in the prefix decrement operator first of all value will be decrement and then decremented value be assigned to the variabl. The general way is represendted as:

**++v;**

**--v;**

**b) Postfix Operator:**

In the postfix increment operator, first of all value will be assigned to a variable and then it will be incremented. Similarly in the postfix decrement operator first of all value will be assigned and then it will be decremented. The general way is represendted as:

**v++;**

**v--;**

## Bitwise Operators:

These are special operators for low level programming. These operators are used for the manipulation of binary data (bits). Ther are six types of Bitwise Operator. The table for Bitwise Operators is as:

Operator | Meaning |

& | Bitwise AND |

| (pipe symbol) | Bitwise OR |

^ | Bitwise exclusive OR |

<< | Bitwise left |

>> | Bitwise right |

~ (tilde) | Bitwise NOT (complement operator) |

## Special Operators:

These are used for special purposes in C++-language. These operators are used in pointers, structures and unions etc. Some types of special operators are as:

1. | Unary Operator |

2. | Comma Operator |

3. | Sizeof Operator |

4. | Type Operator |

5. | Pointer Operator |

6. | Member Selection Operator |