1.
Introduction
2.
Types of operators
2.1.
Arithmetic operators
2.1.1.
Unary operators
2.1.2.
Binary operators
2.2.
Relational operators
2.3.
Logical operators
2.4.
Bitwise operators
2.5.
Assignment operators
2.6.
Other operators
2.6.1.
sizeof operator
2.6.2.
Comma operator
2.6.3.
Conditional operator
3.
4.
Key Takeaways
Last Updated: Mar 27, 2024

# Introduction

## Introduction

They are the foundation of any programming language, and they can be defined as symbols that help the programmer to perform specific mathematical operations and logical computations on operands. For example, ‘*’ is an operator which is used for multiplication, as shown below

z= x * y;

In this, ‘*’ is an operator which is known as the multiplication operator, and ‘x’ and ‘y’ are operands. The function of the multiplication operator is to tell the compiler to multiply both the operands and store the result in ‘z’.

Any programming language is incomplete without the use of operators.

## Types of operators

In C++, there are many built-in operators which can be classified into six types:

### Arithmetic operators

These are the operators which are used to perform mathematical/arithmetic operations on the operands. Arithmetic operators are of two types :

#### Unary operators

• Unary operators are operators that work with a single operand. For example: Increment operator (++) and decrement operator (--).
• Example
`````` int val=5;
++val: // val=6 now``````

#### Binary operators

• Binary operators are operators that work with two operands. For example: Addition (+), Subtraction(-), Multiplication(*), Division(/).
• Example
``````Int x=1;
Int y=2;
cout<<2-1; // 1``````

### Relational operators

These operators establish a relationship between the operands, they are used for comparing the values of the two operands. For example, checking if one operand is greater than the other operand or not, or checking if two operands are equal or not, etc.

### Logical operators

These operators are used to combine two or more conditions or constraints. They are also used to complement the evaluation of the original condition under consideration. The result of these operators is either true or false. For example: (2<10) && (5!=7) ;// true

### Bitwise operators

These operators are used to perform bit-level operations on the operands.

The operators are first converted to bit level, and then operations are performed on the operands. Results of arithmetic operations can be achieved faster if done at bit level. They work only with integral data types and not with floating-point values.

For example:

`````` int a=5; // a = 00000101
cout<< (-a); // a=11111010``````

### Assignment operators

These are used to assign values to variables, the variable is placed onto the left side of the operator while the value is placed on the right side of the operator. The value on the right side of the operator must be of the same data type as the variable which is on the left side of the operator, otherwise, the compiler will give an error.

### Other operators

There are some other operators which are available in C++ that are used to perform some specific tasks. Some of them are:

#### sizeof operator

• It is widely used in the C++ language.
• It is a compiler-time unary operator. It is used to determine the size of a data type.
• The result of sizeof is of an unsigned integral type, which is denoted by size_t
``Syntax: sizeof( variable );``

#### Comma operator

• The comma operator triggers the performance of a sequence of operations.
• It is a binary operator that evaluates its first operand and then discards the result, it then evaluates the second operand and then returns this value (and type).
• It has the lowest precedence out of all.

#### Conditional operator

• In this, an expression/condition is first evaluated and then acts based on the result of the evaluation.
• Syntax : Condition? Expression-1 ? Expression-2;
• In this first, we will evaluate the condition given, if the condition is true, then Expression-1 is executed, and if the condition is false, then Expression-2 is executed.
• We can replace if..else statements with these conditional operators.

1. Is ++a and a++ the same?
++a and a++ are not the same, ++a is increment then use whereas a++ is use and then increment.

2. What will be the value of :
``````Int a=5;
a+=4;
cout<<a;``````

In +=, we first add the value of the left operand to the value of the right operand and then assign the value so a will 5+4 = 9, therefore 9 will be printed.

3. is ‘=’ and ‘==’ the same?
No, ‘=’ and ‘==’ are not the same,’=’ is an assignment operator which basically assigns the value which is on the right to the variable on the left. Whereas ‘==’ is a relational operator which checks whether the LHS is equal to RHS or not.

## Key Takeaways

In this blog, we have covered the following things:

• First, we studied about what are operators and what is their use
• Then we studied about the various operators which are available in C++ along with their uses.

Operators are the fundamental basis of C++ and many other programming languages, these languages are incomplete without operators. Many coding problems require the knowledge and use of operators like expression evaluation in stack about which you can study on this.

Happy Coding!!

Live masterclass