18 Jan C – Typecasting
Type casting in C Language converts one type to another. In C language, we use the cast operator for type casting:
(type_name)value
Let us now see some examples of Type Conversion and convert one datatype to another.
Convert int to float in C
We typecast and convert int to float type:
#include <stdio.h>
int main() {
int a = 13, b = 3;
float f = (float) a/ b;
printf("Float = %f\n", f);
return 0;
}
Output
Float = 4.333333
Convert int to double in C
We typecast and convert int to double type:
#include <stdio.h>
int main() {
int a = 20, b = 7;
double f = (double) a/ b;
printf("Double = %f\n", f);
return 0;
}
Output
Double = 2.857143
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments