Professional Documents
Culture Documents
Type Conversion:
In these types of operations data type of one operand is converted into data type
of another operand. This is known as type conversion. The different types of
type conversion are:
Implicit type conversions are done by the compiler while the explicit type
conversions are user defined conversions.
Automatic Conversions:
Automatic unary conversions:
All operands of type char and short will be converted to int before any
operation. Some compilers convert all float operands to double before any
operation.
Automatic binary conversions:
Rule 1: Whenever there are two operands of different data types the operand
with a lower rank will be converted to the type of higher rank operand. This is
called promotion of data type.
This done with the help of cast operator. The cast operator is a unary operator
that is used for converting an expression to a particular data type temporarily.
The expression can be any constant or variable
The syntax of cast operator is-
Now the value of z will come out be 6.66. This happens because the
cast operator (float) temporarily converted the int variable x into float
type and so floating-point arithmetic took place and fraction part was
not lost.
Precedence And Associativity of Operators:
Examples:
a = 8, b = 4, c = 2, d = 1, e = 5 , f = 20
a = 8, b = 5, c = 8, d = 3, e = 65, f = 10, g = 2, h = 5, k = 2
Order of Evaluation of Operands:
C does not specify the order in which the operands of an operator are evaluated.
For example, consider the expression,
if the first operand (++x) is evaluated first then y will be assigned value 11, while
if second operand (- -x) is evaluated first then y will be assigned the value 9. Since
C is silent on such controversies, the answers may vary on different compilers so
it is better to avoid such type of expressions.
There are four exceptional operators where C clearly specifies the order of
evaluation of operands.
These operators are logical AND ( && ), logical .OR (||), conditional (? : ) and
comma operator( , ).
In all these cases the operand on the left side is evaluated first. In the case of
logical AND and logical OR operators, sometimes there is no need to evaluate
second operand. In && operator, if the first operand evaluates to false(zero), then
second operand is not evaluated and in the case of || operator if the first operand
evaluates to true (nonzero) then second operand is not evaluated.