Home / C Tutorial / Automatic type conversion Expression In Programming Language

Automatic type conversion Expression In Programming Language

If the operands are of different types the lower type is automatically converted to the higher typebefore the operation proceeds
The result is of the higher type
Given below is the sequence of rules that are applied by evaluating expressions.
Operator 1 Operator 2 Result
Long Double any Long Double
Double any Double
Float any Float
Unsigned Long Int any Unsigned Long In
Long Int any Long Int
Unsigned Int any Unsigned Int
Final result of an expression to the type of the variable on the left of the assignment signed before assigning the value to it.
However, the following changes are introduced during the final assignment:
1. Float to Int causes truncation of the fractional part.
2. Double to float causes rounding of digits.
3. Long int to int causes dropping of the excess higher order bits
Type Casting:
Casting a value is a forcing a type conversion in a way that is different from the automatic conversion and this process is called type cast.
This should be clear from the following examples:
An example of automatic conversion:
Output of automatic conversion:
The answer turns out to be 1.000000 and not 1.5 this is because, 6 and 4 are both integers, and hence 6/4 yields an integer, 1.
This 1 when stored in a is converted to 1.000000. But what if you don’t want the question to be truncated. One solution is to make either x or y as a float.
The general form of casting is:
(type_desired) expression;
where type_desired : standard C data types and
expression: constant, variables or an expression.
An example of type casting:
Output of type casting example:
Type Definition using typedef
C allows us to create data types via the typedef statement.
The format of typedef statement is:
typedef data type new_type_name;
Example:
typedef int units;
units bat1,bat2; /*this statement is equivalent to int bat1,bat2*/