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.
Also see, Literals in C, Fibonacci Series in C++ and, Unary operator overloading in c++.
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.