Type
conversions:
- Type conversions depend on the specified operator and the type the type of the operand or operators.
- Type conversions are performed in the following cases:
- When a value of one type is assigned to a variable of a different type or an operator converts the type of its operand or operands before performing an operation.
- When a value of one type is explicitly cast to a different type?
- When a value is passed as an argument to a function or when a type is returned from a function.
Implicit
conversion between integer and float
In order to effectively develop ‘C’ programs,
implicit conversion of floating point and integer values are required in ‘C’. Following
are the rules which govern this conversion:
An arithmetic operation between a ‘float’ and
‘float’ always results in a ‘float’.
Example: float a=5.0/2.0 gives you the result as
2.500000.
In an arithmetic operation between an ‘int’ and
‘float’ the integer is first promote to ‘float’ and then the operation is
carried out. So the result will always be a ‘float’.
Example: float a=5.0/2 gives you the result as
2.500000 because the 2 in the denominator is promoted to float.
On assigning ‘float’ to an integer
(using=operator) the ‘float’ is demoted to an integer.
Example: int a=30.54; this actually stored as 30
and the .54 will be ignored or truncated.
On assigning ‘int’ to ‘float’, it is promoted to a
‘float’
Example: float a=13; this is stored as 13.000000.
Typecasting: Facilitating explicit conversion
Typecasting is a mechanism used to convert a value
from one data type into another.
Example for Typecasting:
f=(float) i/j;
In this statement, by placing a float inside
parentheses, you are forcibly converting the numerator (i) into a float.
Operator Precedence:
Consider an arithmetic expression that has two or
more operators i.e., a+b*c
To understand, how the above arithmetic expression
executed, we need to know the priority or operator precedence, in which the
operations in an expression are performed. The evaluation of a given arithmetic
expression follows certain precedence, known as Operator Precedence.
The table given here shows the precedence of
commonly used arithmetic operators.
Priority
|
Operators
|
Description
|
I
|
* / %
|
Multiplication, Division and Modulo
division
|
II
|
+ -
|
Addition and Subtraction
|
III
|
=
|
Assignment
|
X= 3 * 16 % 11 / 4;
In this expression, the three operators (*, % and
/) belong to the same precedence level, i.e., level is, -1. This expression is
evaluated as
X = 48 % 11/4
|
After multiplication (3*16)
|
X= 4/4
|
After modular division (48%11)
|
X = 1
|
After division (4/4)
|
In this case, the compiler has evaluated the
operators on a first come first served basis.
This brings in a new concept called, operator association.
Operator Association:
Associativity is applied when we have more than
one operator of the same precedence level in an expression.
X = 3 * 16 % 11 /4;
In the above expression, the operators * and
%share the operand 16 and similarly % and / share the operand 11. In such
cases, C used left-to-right association rule, this means that the operator to
the left of the operand is evaluated first and then the one on the right.
Operators
|
Associativity
|
*
/ %
|
Left to right
|
+ -
|
Left to right
|
=
|
Right to left
|
In the case of an assignment operator, association
is from right-to-left, which means that the value on the right of the operator
is finally assigned to the variable on the left and not the other way round.