This program demonstrates implicit and explicit type conversion by dividing two integers to obtain a floating-point result.
C Program
#include <stdio.h>
int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
int implicit_result = a / b;
float explicit_result = (float) a / (float) b;
printf("Integer division (implicit truncation): %d\n", implicit_result);
printf("Floating-point division (explicit conversion): %.2f\n", explicit_result);
return 0;
}
Explanation
- When both operands of
/areint, C performs integer division and silently truncates any decimal part — this is the “implicit” behavior since no conversion is requested but the result type still loses precision. - Casting either operand to
floatusing(float) aforces explicit type conversion: C then promotes the whole expression to floating-point arithmetic before dividing, preserving the fractional part. - Only one operand needs to be cast for the whole expression to become floating-point, but casting both makes the intent clearer.
Sample Output
Enter two integers: 7 2
Integer division (implicit truncation): 3
Floating-point division (explicit conversion): 3.50