Type Conversion
C converts automatically when types differ (an int becomes a double in a calculation). To force a conversion, put the type in parentheses before the value: (double) a. Converting a decimal to an int drops the fraction.
Example
int a = 7, b = 2;
printf("%d\n", a / b);
printf("%.2f\n", (double) a / b);
double d = 9.99;
int n = (int) d;
printf("%d\n", n);Result
3 3.50 9
Exercise
Convert to double before dividing.
() a / b
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.