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:
1 2 3 |
(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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> int main() { int a = 13, b = 3; float f = (float) a/ b; printf("Float = %f\n", f); return 0; } |
Output
1 2 3 |
Float = 4.333333 |
Convert int to double in C
We typecast and convert int to double type:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> int main() { int a = 20, b = 7; double f = (double) a/ b; printf("Double = %f\n", f); return 0; } |
Output
1 2 3 |
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