In this article, we are going to learn about operators in C++
Operators are defined as a symbol that helps us to perform certain mathematical and logical computations on operands.
c = a + b;
Here, ‘+’ is the operator known as the addition operator and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’.
Different types of operators in C++
There are 6 built-in operators in C++:-
- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
- Assignment operators
- Other operators
1. Arithmetic operators:
These are used to perform mathematical operations on operands.
For example: (+,-,*,/,%,++,–).
These are of two types:
a.) Unary operators
b.) Binary operators
a.) Unary operators:-
operators that work with a single operand are called unary operators.
For example:- Increment(++) and Decrement(–) operators
int val = 6;
++val; // 7
int val = 6; ++val; // 7
b.) Binary operators:-
operators that operate or work with two operands are binary operators.
int a = 7;
int b = 8;
a + b; // 15
int a = 7; int b = 8; a + b; // 15
2. Relational operators:-
These are used to compare two operands’ values. To check whether the operands are equal or not i.e one operand is equal to another operand or not.
Some of the relational operands are
1.) Equal (==)
2.) Greater than or equal(>=)
3.) Less than or equal(<=)
a>b; // operator to check if a is greater than b
int a=7; int b=5; a>b; // operator to check if a is greater than b
3. Logical operators:-
Logical operators are used to combining two or more conditions. The result of the operation of the logical operator is a Boolean value either true or false.
Some of the logical operators are:-
1.) Logical AND (&&)
2.) Logical OR (||)
3.) Logical NOT(!)
(7!=8) && (7>8); // false
(7!=8) || (7>8); // false
(7!=8); //true (7!=8) || (7>8); // false (7!=8) && (7>8); // false
4. Bitwise operators:-
The bitwise operators are used to perform bit-level operations on the operands. The operators are converted into bit-level then the calculation is performed on the operands.
There are six types of Bitwise operators
1.) Bitwise AND operator (&)
2.) Bitwise OR operator(|)
3.) Bitwise XOR operator(^)
4.) Bitwise COMPLEMENT operator(~)
5.) Bitwise SHIFT LEFT operator(<<)
6.) Bitwise SHIFT RIGHT operator(>>)
These operations are necessary because the Arithmetic-Logic Unit(ALU) in the computer’s CPU carries out arithmetic operations at the bit level.
NOTE:- Bitwise operators are only used with char and int data types.
int a=5, b=9; //a = 5(00000101), b = 9(00001001)
(a^b); // 00001100
(~a); // 11111010
int a=5, b=9; //a = 5(00000101), b = 9(00001001) (a^b); // 00001100 (~a); // 11111010
5. Assignment operators:-
These are used to assign a value to the variable. The left side operand of the assignment operator is a variable and the right side 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.
There are a few assignment operators:-
1.Simple assignment operator (=):-
It assigns a value on the right to a variable on the left.
a = 10;
b = 20;
a = 10; b = 20;
There are other types of assignment operators used with other arithmetic and bitwise operators known as short-hand operators.
2. Add and assignment operator(+=)
It first adds the value of the left operand to the value of the right operand and then assigns the result to the variable on the left.
a += b; // a = a + b
If a = 7 then
a += 8 = 15;
a += b; // a = a + b
3. Subtract and assignment operator(-=):
It first subtracts the value of the right operand from the value of the left operand and then assigns the result to the variable on the left.
a -= b; //a = a-b
If a = 5 then
a -= 3 = 2;
a -= b; //a = a-b
4. Multiply and assignment operator(*=):
It first multiplies the value of the left operand with the value of the right operand and then assigns the result to the variable on the left.
a* = b; //a = a*b
If a = 4 then
a *= 4 = 16;
a *= b; //a = a*b
5. Division and assignment operator(/=):
It first divides the value of the left operand with
the value of the right operand and then assigns
the result to the variable on the left.
a /= b; //a = a/b
If a = 10 then
a /= 2 = 5;
a /= b; //a = a/b
6. Other operators:-
Apart from the above operators, there are some other operators available in C or C++ used to perform some specific tasks. Some of them are discussed here:
1.)Size of operator:-
Size is much used in the C/C++ programming language.
· It is a compile-time unary operator which can be used to compute the size of its operand.
· Basically, the size of the operator is used to compute the size of the variable.
The comma operator is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value.
· The comma operator has the lowest precedence of any C operator.
· Comma acts as both operator and separator.
· The conditional operator is of the form Expression1? Expression2: Expression3.
· Here, Expression1 is the condition to be evaluated. If the condition is True then we will execute and return the result of Expression2 otherwise if the condition is false then we will execute and return the result of Expression3.
· We may replace the use of if-else statements with conditional operators.
4.)dot(.) and arrow(->) operators:-
Member operators are used to referencing individual members of classes, structures, and unions.
· The dot operator is applied to the actual object.
· The arrow operator is used with a pointer to an object.
Casting operators convert one data type to another.
· For example, int(7.2000) would return 7.
· A cast is a special operator that forces one data type to be converted into another.
· The most general cast supported by most of the C++ compilers is as follows – [(type)expression].
· Pointer operator & returns the address of a variable.
· For example &a; will give the actual address of the variable.
· Pointer operator * is a pointer to a variable.
· For example *var; will pointer to a variable var.