Type Conversion
Type
conversion is done when the expression has variables of different data types
§Type conversion is used to convert a type of a variable into another data types.
§There are two types of Type conversion in Computer programming..
§Implicit is a type conversion in which is done automatically during program execution according
to hierarchy
of data types as shown in the figure.
Long double
⬇
Double
⬇
Float
⬇
Unsigned Long int
⬇
Long int
⬇
Unsigned int
⬇
Char
§Examples float f; float salary=15000.0
float
x; int I; int sal
Int y=3; f=I; sal=salar y
higher data type is converted into the value of lower data type
lower data types.
§This typecasting is under the programmer’s control and not
under the compiler’s control.
The integer variant of %f is =3
Int y=3; f=I; sal=salar y
Typecasting
§It is a type of explicit /forced conversion in which value ofhigher data type is converted into the value of lower data type
lower data types.
§This typecasting is under the programmer’s control and not
under the compiler’s control.
Example
#include<stdio.h>
int main()
{
float f,int i;
printf(“\nEnter the floating point number: “);
scanf(“%f” ,&f);
i=(int)f;
printf(“\n %d” ,f, i);
return 0;
}
Output : Enter the floating point number:3.76The integer variant of %f is =3
Long
double