# Operators in C++

• Last Updated : 31 May, 2022

An operator is a symbol that operates on a value to perform specific mathematical or logical computations. They form the foundation of any programming language. In C++, we have built-in operators to provide the required functionality.

An operator operates the operands. For example,

`int c = a + b;`

Here, ‘+’ is the addition operator. ‘a’ and ‘b’ are the operands that are being ‘added’.

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

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Ternary or Conditional Operators ### 1) Arithmetic Operators

These operators are used to perform arithmetic or mathematical operations on the operands. For example, ‘+’ is used for addition, ‘-‘ is used for subtraction ‘*’ is used for multiplication, etc.

Arithmetic Operators can be classified into 2 Types:

A) Unary Operators: These operators operate or work with a single operand. For example: Increment(++) and Decrement(–) Operators.

Example:

## C++

 `// CPP Program to demonstrate the increment``// and decrement operators``#include ``using` `namespace` `std;` `int` `main()``{``    ``int` `a = 10;``    ``cout << ``"a++ is "` `<< a++ << endl;``    ``cout << ``"++a is "` `<< ++a << endl;` `    ``int` `b = 15;``    ``cout << ``"b-- is "` `<< b-- << endl;``    ``cout << ``"--b is "` `<< --b << endl;``  ` `    ``return` `0;``}`

Output

```a++ is 10
++a is 12
b-- is 15
--b is 13```

Note: ++a and a++, both are increment operators, however, both are slightly different.

In ++a, the value of the variable is incremented first and then It is used in the program. In a++, the value of the variable is assigned first and then It is incremented. Similarly happens for the decrement operator.

B) Binary Operators: These operators operate or work with two operands. For example: Addition(+), Subtraction(-), etc.

Note: The Modulo operator(%) operator should only be used with integers.

Example:

## C++

 `// CPP Program to demonstrate the Binary Operators``#include ``using` `namespace` `std;` `int` `main()``{``    ``int` `a = 8, b = 3;` `    ``// Addition operator``    ``cout << ``"a + b = "` `<< (a + b) << endl;``  ` `    ``// Subtraction operator``    ``cout << ``"a - b = "` `<< (a - b) << endl;``  ` `    ``// Multiplication operator``    ``cout << ``"a * b = "` `<< (a * b) << endl;``  ` `    ``// Division operator``    ``cout << ``"a / b = "` `<< (a / b) << endl;``  ` `    ``// Modulo operator``    ``cout << ``"a % b = "` `<< (a % b) << endl;` `    ``return` `0;``}`

Output

```a + b = 11
a - b = 5
a * b = 24
a / b = 2
a % b = 2```

### 2) Relational Operators

These operators are used for the comparison of the values of two operands. For example, ‘>’ checks if one operand is greater than the other operand or not, etc. The result returns a Boolean value, i.e., true or false.

Example:

## C++

 `// CPP Program to demonstrate the Relational Operators``#include ``using` `namespace` `std;` `int` `main()``{``    ``int` `a = 6, b = 4;` `    ``// Equal to operator``    ``cout << ``"a == b is "` `<< (a == b) << endl;``  ` `    ``// Greater than operator``    ``cout << ``"a > b is "` `<< (a > b) << endl;``  ` `    ``// Greater than or Equal to operator``    ``cout << ``"a >= b is "` `<< (a >= b) << endl;``  ` `    ``//  Lesser than operator``    ``cout << ``"a < b is "` `<< (a < b) << endl;``  ` `    ``// Lesser than or Equal to operator``    ``cout << ``"a <= b is "` `<< (a <= b) << endl;``  ` `    ``// true``    ``cout << ``"a != b is "` `<< (a != b) << endl;` `    ``return` `0;``}`

Output

```a == b is 0
a > b is 1
a >= b is 1
a < b is 0
a <= b is 0
a != b is 1```

### 3) Logical Operators

These operators are used to combine two or more conditions or constraints or to complement the evaluation of the original condition in consideration. The result returns a Boolean value, i.e., true or false.

Example:

## C++

 `// CPP Program to demonstrate the Logical Operators``#include ``using` `namespace` `std;` `int` `main()``{``    ``int` `a = 6, b = 4;` `    ``// Logical AND operator``    ``cout << ``"a && b is "` `<< (a && b) << endl;``  ` `    ``// Logical OR operator``    ``cout << ``"a ! b is "` `<< (a > b) << endl;``  ` `    ``// Logical NOT operator``    ``cout << ``"!b is "` `<< (!b) << endl;` `    ``return` `0;``}`

Output

```a && b is 1
a ! b is 1
!b is 0```

### 4) Bitwise Operators

These operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands. The mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit-level for faster processing.

Note: Only char and int data types  can be used with Bitwise Operators.

Example:

## C++

 `// CPP Program to demonstrate the Relational Operators``#include ``using` `namespace` `std;` `int` `main()``{``    ``int` `a = 6, b = 4;` `    ``// Equal to operator``    ``cout << ``"a == b is "` `<< (a == b) << endl;``  ` `    ``// Greater than operator``    ``cout << ``"a > b is "` `<< (a > b) << endl;``  ` `    ``// Greater than or Equal to operator``    ``cout << ``"a >= b is "` `<< (a >= b) << endl;``  ` `    ``//  Lesser than operator``    ``cout << ``"a < b is "` `<< (a < b) << endl;``  ` `    ``// Lesser than or Equal to operator``    ``cout << ``"a <= b is "` `<< (a <= b) << endl;``  ` `    ``// true``    ``cout << ``"a != b is "` `<< (a != b) << endl;` `    ``return` `0;``}`

Output

```a & b = 4
a | b = 6
a ^ b = 2
a>>1 = 3
a<<1 = 12
~(a) = -7```

### 5) Assignment Operators

These operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator 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 raise an error.

Example:

## C++

 `// CPP Program to demonstrate the Assignment Operators``#include ``using` `namespace` `std;` `int` `main()``{``    ``int` `a = 6, b = 4;` `    ``// Assignment Operator``    ``cout << ``"a = "` `<< a << endl;``  ` `    ``//  Add and Assignment Operator``    ``cout << ``"a += b is "` `<< (a += b) << endl;``  ` `    ``// Subtract and Assignment Operator``    ``cout << ``"a -= b is "` `<< (a -= b) << endl;``  ` `    ``//  Multiply and Assignment Operator``    ``cout << ``"a *= b is "` `<< (a *= b) << endl;``  ` `    ``//  Divide and Assignment Operator``    ``cout << ``"a /= b is "` `<< (a /= b) << endl;` `    ``return` `0;``}`

Output

```a = 6
a += b is 10
a -= b is 6
a *= b is 24
a /= b is 6```

### 6) Ternary or Conditional Operators(?:)

This operator returns the value based on the condition.

`Expression1? Expression2: Expression3`

The ternary operator ? determines the answer on the basis of the evaluation of Expression1. If it is true, then Expression2 gets evaluated and is used as the answer for the expression. If Expression1 is false, then Expression3 gets evaluated and is used as the answer for the expression.

This operator takes three operands, therefore it is known as a Ternary Operator.

Example:

## C++

 `// CPP Program to demonstrate the Conditional Operators``#include ``using` `namespace` `std;` `int` `main()``{``    ``int` `a = 3, b = 4;` `    ``// Conditional Operator``    ``int` `result = (a < b) ? b : a;``    ``cout << ``"The greatest number is "` `<< result << endl;` `    ``return` `0;``}`

Output

`The greatest number is 4`

7) There are some other common operators available in C++ besides the operators discussed above. Following is a list of these operators discussed in detail:

A) sizeof Operator: This unary operator is used to compute the size of its operand or variable.

`sizeof(char); // returns 1`

B) Comma Operator(,): This binary operator (represented by the token) is used to evaluate its first operand and discards the result, it then evaluates the second operand and returns this value (and type). It is used to combine various expressions together.

```int a = 6;
int b = (a+1, a-2, a+5); // b = 10```

C)  -> Operator: This operator is used to access the variables of classes or structures.

`cout<<emp->first_name;`

D) Cast Operator: This unary operator is used to convert one data type into another.

```float a = 11.567;
int c = (int) a; // returns 11```

E) Dot Operator(.): This operator is used to accesses members of structure variables or class objects in C++.

`cout<<emp.first_name;`

F) & Operator: This is a pointer operator and is used to represent the memory address of an operand.

G) * Operator: This  is an Indirection Operator

## C++

 `// CPP Program to demonstrate the & and * Operators``#include ``using` `namespace` `std;` `int` `main()``{``    ``int` `a = 6;``    ``int``* b;``    ``int` `c;``    ``//  & Operator``    ``b = &a;` `    ``// * Operator``    ``c = *b;``    ``cout << ``" a = "` `<< a << endl;``    ``cout << ``" b = "` `<< b << endl;``    ``cout << ``" c = "` `<< c << endl;` `    ``return` `0;``}`

Output

``` a = 6
b = 0x7ffe2064d048
c = 6```

H) << Operator: This is the output operator. It prints the output value.

I) >> Operator: This is the input operator. It gets the input value.

```int a;
cin>>a;
cout<<a;```

### Operator Precedence Chart

My Personal Notes arrow_drop_up