Type casting:This is used to convert one data type to another. Particularly, the conversion is done from low level to high level. Here level means total bytes occupied in memory. So the conversion is done from int to float or float to double or int to double and not vice versa.
It's of two types.
1)Implicit casting::- In this , computer itself converts one data type to another that is called auto conversion.
// program to show type casting concept
#include<stdio.h>
#include<conio.h>
void main()
{
int a=3,b=2;
float c;
c=a/b;
printf("%f",c);
}
Its output is 1.00. It is incorrect.
So, it should be avoided.
2)Explicit casting:
In this conversion, we forcefully tell computer to convert one data type to another.It gives us right answer. To use this, we put word float in front of both numbers.
Example,
// program to show type casting concept
#include<stdio.h>
#include<conio.h>
void main()
{
int a=3,b=2;
float c;
c=(float) a/b;
printf("%f",c);
}
Its output is 1.5. It is correct.
Here, the computer, first converts both values(numerator and denominator ) into float then goes for division.
So, it should be used.
It's of two types.
1)Implicit casting::- In this , computer itself converts one data type to another that is called auto conversion.
// program to show type casting concept
#include<stdio.h>
#include<conio.h>
void main()
{
int a=3,b=2;
float c;
c=a/b;
printf("%f",c);
}
Its output is 1.00. It is incorrect.
So, it should be avoided.
2)Explicit casting:
In this conversion, we forcefully tell computer to convert one data type to another.It gives us right answer. To use this, we put word float in front of both numbers.
Example,
// program to show type casting concept
#include<stdio.h>
#include<conio.h>
void main()
{
int a=3,b=2;
float c;
c=(float) a/b;
printf("%f",c);
}
Its output is 1.5. It is correct.
Here, the computer, first converts both values(numerator and denominator ) into float then goes for division.
So, it should be used.