This program reads an integer, a float, and a character, then displays them with printf() using the appropriate format specifiers, demonstrating type conversion between int and float.
C Program
#include <stdio.h>
int main() {
int i;
float f;
char c;
printf("Enter an integer: ");
scanf("%d", &i);
printf("Enter a float: ");
scanf("%f", &f);
printf("Enter a character: ");
scanf(" %c", &c);
printf("\nInteger value: %d\n", i);
printf("Float value: %.2f\n", f);
printf("Character value: %c\n", c);
printf("\nInteger converted to float: %.2f\n", (float) i);
printf("Float converted to integer (truncated): %d\n", (int) f);
return 0;
}
Explanation
%d,%f, and%ctellprintf()/scanf()how to interpret each variable’s bytes; using the wrong specifier gives garbage output.- The leading space in
" %c"skips any leftover whitespace/newline in the input buffer so the character read isn’t accidentally the Enter key from the previous input. (float) iexplicitly (explicit conversion) converts the integer to a float for display.(int) ftruncates the float’s decimal part when converting to an integer — it does not round.
Sample Output
Enter an integer: 7
Enter a float: 3.75
Enter a character: A
Integer value: 7
Float value: 3.75
Character value: A
Integer converted to float: 7.00
Float converted to integer (truncated): 3