Q19. What is an operator? List and explain the types of operators in C++.
Answer:
Operator
An operator is.a single or double symbol that gives instruction to the compiler or interpreter to perform certain operations on its associated data items called operands (such as constants, variables, array elements and function references).
Example
(i) 6+5
The above expression has a single symbol ‘+’ associated with two constants 5 and 6. This operator instructs the compiler to perform addition operation on the constants.
(ii) int a = 10;
a --;
In the above example, the second statement has double symbol -- called post decrement operator. It signifies that the value of the operator must be decreased by one.
Types of Operators
C++ support different types of operators as discussed below:
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
- Increment and decrement operators
- Conditional operators
- Bitwise operators
- Special operators.
Arithmetic
Operator
|
Meaning
|
+
|
Addition or unary plus
|
-
|
Subtraction or unary minus
|
*
|
Multiplication
|
/
|
Division
|
%
|
Modulo division
|
- For arithmetic operations, operands must be numeric values.
- Here, modulo division produces remainder of integer division.
Arithmetic operations are classified as
(i) Integer arithmetic
(ii) Real arithmetic
(iii) Mixed mode arithmetic.
(i) Integer Arithmetic
Both the operands must be integer in an integer arithmetic. It always yields.an integer value.
Syntax
exp1 Integer_arithmetic exp2
Example
a + b, a - b, a * b, a / b, a % b etc.
(ii) Real Arithmetic
Both the operands must be real in real arithmetic. It yields real values as result. It cannot be applied to % operator.
Syntax
exp1 real_arithmetic exp2
Example
float a = 20.0, b = 3.0;
a + b = 20.0 + 3.0 = 23.000000
a - b=20.0 - 3.0 = 17.000000
a * b = 20.0 * 3.0 = 60.000000
a / b = 20.0/3.0 = 6.6666667
a% b = 20.0 % 3.0 (invalid expression)
(iii) Mixed Mode Arithmetic
When one operand is integer and other is real it is known as mixed mode arithmetic. Here, the result will always be real.
Syntax
exp1 mixedmode_arithmetic exp2.
Example
int a = 20.0, b = 3;
a + b = 20.0 + 3 = 23.0
a - b = 20.0 - 3 = 17.0
a*b = 20.0 * 3 = 60.0
a/b = 20.0/3 = 6.666667
a % b = 20.0 % 3 (invalid expression)
2. Relational Operators
These operators are used to compare two quantities.
Relational Operator
|
Action
|
<
|
is less than
|
<=
|
is less than or equal
to
|
>
|
is greater than
|
>=
|
is greater than or
equal to
|
==
|
is equal to
|
!=
|
is not equal to
|
Syntax
exp1 relational_operator exp2.
Example
Expression
|
Result
|
35
< 5
|
0
(false)
|
35
> 5
|
1
(true)
|
35
== 10
|
0
(false)
|
35
!= 10
|
1
(true)
|
(10
+ 5) == (3*5)
i.e.,
15 == 15
|
1
(true)
|
3. Logical Operators
Logical expressions combine two or more relational. expressions. Logical operators are used to test more than one condition and make decision.
Logical Operator
|
Action
|
&&
|
Logical AND
|
||
|
Logical OR
|
!
|
Logical NOT
|
(i)Logical AND
The result of the logical AND expression will be true only when both the relational expressions are true.
Syntax
exp1 && exp2
Examples
Consider a = 10, b = 5, c = 15
1. i = (a > b) && (b < c);
The value of i in this expression will be 1 i.e., true.
2. i = (a < b) && (b < c)
The value of i is 0 i.e., false.
(ii) Logical OR
The result of logical OR expression will be false only when both relational expressions are false.
Syntax
expl | | exp2
Example
1. i=(a < b) || (b < c)
Here the value of i is 1 i.e., true.
2. i = (a < b) || (b > ¢)
Here the value of i is 0 i.e., false.
(iii) Logical NOT
The result of the expression will be true, if the expression is false and vice versa.
Syntax
exp1 ! exp2
Example
x=20
i = !(x == 20)
The value of i will be 0. This is because x == 20 is true (value 1) and ! true (!1=0) is false (i.e. 0).
4, Assignment Operators
These operators are used to assign result of an expression to a variable. ,
Syntax
variable op = exp;
or
variable = variable op exp;
Example
a += 10; or a = a+10;
Assignment Operator
|
Action
|
a = a+1 or a += 1
|
Adds 1 to a and assigns the values to a
|
a = a-1 or a -= 1
|
Decrements a by 1 and assigns it to a
|
a = a/(b+5) or a/=(b+5)
|
Divides a by b+5 and assigns result to a
|
a = a*(b+5) or a *= b+5
|
Multiplies b+5 with a and assigns result to a
|
5. Increment and Decrement Operators
These operators are represented as ‘++’ and "--". The operator ‘++’ increments operand by 1 and ‘--’ decrements operand by 1. The unary operators with their associated actions are as follows,
Operator
|
Action
|
a++
|
Post increment
|
++a
|
Pre increment
|
a--
|
Post decrement
|
--a
|
Pre decrement
|
Syntax
Increment or decrement operator exp
Examples
1. int a = 8;
y = ++a;
The value of y will be 9.
2. int a = 8;
y = a++
The value of y will be 8.
3. int a = 5;
y = a--
The value of y will be 5.
4. int a = 5:
y = --a
The value of y will be 4.
6. Conditional Operators
It is also known as a ternary operator.
Syntax
exp1?exp2: exp3;
Where exp1, exp2 and exp3 are the expressions.
Operation of Conditional Operator
The value of exp1 is evaluated first. If it is true, value of exp2 is evaluated, otherwise exp3 is evaluated.
Example
1. int a = 5, b = 10, c = 15;
y = (a>b) ? b: c;
In the above statement, the expression a> b is evaluated, since it is false value of c will be assigned to y. So, value of y will be 15.
y = (a<b) ? b:c;
Here, a<b is true, so value of b is assigned to y. So, value of y will be 10.
2 Greatest of three numbers using conditional operator is as follows,
x=(a>b)? (a>c)? (b>c)? a:b: c;
Here, the greatest of three numbers i.e.,.15 is stored in x.
7. Bitwise Operators
Bitwise operators are similar to that of logical operators except that they work on binary bits. The variables are internally converted to binary numbers and then bitwise operators are applied on individual bits.
These operators work with char and int datatypes. They cannot be used with floating point numbers.
Bitwise Operator
|
Action
|
&
|
bitwise AND
|
|
|
bitwise OR
|
^
|
bitwise exclusive OR
|
<<
|
shift left
|
>>
|
shift right
|
~
|
ones complement
|
Syntax
exp1 Bitwise_operator exp2
Example
if a=4, b= 6;
The equivalent binary value of a is 0000 0100
The equivalent binary value of b is 0000 0110
(a) The-value of a&b is 00000100
(b) The value of a|b is 00000110
(c) The value of a^b is 00000010
(d) a<<2.
This operator left shifts the binary bits twice.
a =00010000
(e) a>>2
This operator right shifts the binary bits twice.
a =00000001
(f) 5>>2
The value of 5 >> 2 = 01000001 = 65
(g) 5<<3
The value of 5 <<3 = 00101000 = 40
8. Special Operators
Some of the special operators that are available in 'C' are
(i) Comma operator
(ii) Size of operator
(iii) Member selection operators (. and →).
(i) Comma Operator
The comma operator ',' has the lowest priority among all the operators available in ‘C’. This operator is used to separate the related expressions. The expressions separated by comma operator need not be included within the parenthesis.
Syntax
expl, exp2;
Example
c = (a = 5, b = 10, a+b);
This expression first assigns 5 to.a, then assigns 10 to b and finally assigns (5 + 10) to c.
(ii) Sizeof Operator.
It returns number of bytes the operand occupies. Operand may be a variable, a constant, or a datatype
qualifier.
Syntax
sizeof(operand);
Example
y = sizeof(int); here the value of y will be 2 (bytes)
z = sizeof(a); here the value of z depends on datatype of a.
(iii) Member Selection Operators
These operators are used to access members of structures and unions.
Syntax
exp1 member_selection_operator exp2.
Example
var.member1; // When var is a structure variable.
var→members2; // When var is a pointer to structure variable.
Beside these operators, C++ also supports some additional operators. These operators include the following,
1. Insertion operator: The symbolic notation of this operator is <<
2. Extraction operator: It is represented as >>
3. Scope access or resolution operator: It is represented as ::
4. Pointer to member declaration: It is represented as ::*
5. Deference pointers to pointers to class members: It is represented as a*
6. Deference pointers to class members: It is represented as *
7. Memory release operator: It is represented by the keyword "delete"
8. Memory allocation operator: This operator is represented by the keyword "new"
exp1 Bitwise_operator exp2
Example
if a=4, b= 6;
The equivalent binary value of a is 0000 0100
The equivalent binary value of b is 0000 0110
(a) The-value of a&b is 00000100
(b) The value of a|b is 00000110
(c) The value of a^b is 00000010
(d) a<<2.
This operator left shifts the binary bits twice.
a =00010000
(e) a>>2
This operator right shifts the binary bits twice.
a =00000001
(f) 5>>2
The value of 5 >> 2 = 01000001 = 65
(g) 5<<3
The value of 5 <<3 = 00101000 = 40
8. Special Operators
Some of the special operators that are available in 'C' are
(i) Comma operator
(ii) Size of operator
(iii) Member selection operators (. and →).
(i) Comma Operator
The comma operator ',' has the lowest priority among all the operators available in ‘C’. This operator is used to separate the related expressions. The expressions separated by comma operator need not be included within the parenthesis.
Syntax
expl, exp2;
Example
c = (a = 5, b = 10, a+b);
This expression first assigns 5 to.a, then assigns 10 to b and finally assigns (5 + 10) to c.
(ii) Sizeof Operator.
It returns number of bytes the operand occupies. Operand may be a variable, a constant, or a datatype
qualifier.
Syntax
sizeof(operand);
Example
y = sizeof(int); here the value of y will be 2 (bytes)
z = sizeof(a); here the value of z depends on datatype of a.
(iii) Member Selection Operators
These operators are used to access members of structures and unions.
Syntax
exp1 member_selection_operator exp2.
Example
var.member1; // When var is a structure variable.
var→members2; // When var is a pointer to structure variable.
Beside these operators, C++ also supports some additional operators. These operators include the following,
1. Insertion operator: The symbolic notation of this operator is <<
2. Extraction operator: It is represented as >>
3. Scope access or resolution operator: It is represented as ::
4. Pointer to member declaration: It is represented as ::*
5. Deference pointers to pointers to class members: It is represented as a*
6. Deference pointers to class members: It is represented as *
7. Memory release operator: It is represented by the keyword "delete"
8. Memory allocation operator: This operator is represented by the keyword "new"