Data Types
Casts
A cast asks C to treat a value as another type for a specific expression.
Casts
casts.c
#include <stdio.h>
int main(void) {
int total = ;
int count = 2;
double average = (double)total / count;
printf("average=%.1f\n", average);
return 0;
}
#include <stdio.h>
int main(void) {
int total = ;
int count = 2;
double average = (double)total / count;
printf("average=%.1f\n", average);
return 0;
}
#include <stdio.h>
int main(void) {
int total = ;
int count = 2;
double average = (double)total / count;
printf("average=%.1f\n", average);
return 0;
}
cast
`(double)total` converts `total` before the division runs.
integer division
Without a cast, dividing two integers keeps only the integer part.