Mixed
Mode Expression and Type Conversions
When the variables and constants of different types
are mixed in an expression ,they are all converted to the same type .
The compiler converts all operands up to the type of
the largest operand , which is called type promotion .
Generally , the lower type of the operator is promoted to higher type operand and the
result will be of the higher type .
The ordering the data type is :
char < int < long < float <double .
Also , an unsigned value out ranks the corresponding
signed type .
Conversion Rules :
The following are the rules applicable to the
arithmetic operations between two operators having different data types .
· If
char and short int values are used as operands , the char operand is
automatically elevated to int .
· If
float and double values are used as operands
, the float operand is automatically elevated to double .
· If
int and float values are used as operands , the int operand is automatically
elevated to float .
· If
float and double values are used as operands ,the float is automatically elevated
to double .
· If
long and unsigned int are used as operands , both the operands are
automatically elevated to unsigned long.
Type Casting :
To convert the value of an expression to a different
type ,the expression must be preceded by the name of the desired data type ,
enclosed in parenthesis ; (data type) expression .
This type of conversion is known as type casting . The
operators with in C are grouped hierarchically according
to their precedence (i.e, order of evaluation).
Operations with higher precedence are carried out
before operations having a lower precedence . The natural order of evaluation
can be altered through the use of paranthesis.
The arithmetic operators * , / and % fall in to
precedence group , + and – falls in to another . The first group has a higher
precedence than the second one .
The order in which consecutive operations with in
the same precedence group are carried out is known as associately .with in each
of the precedence groups described above , the associativity is from left to
right.
Example Program:
Output :
Thank you …
Please Show your support by subscribe to my blog below ...
0 comments: