int to float c
#include <stdio.h>
int main() {
int integerNum = 10;
float floatNum;
floatNum = (float)integerNum;
printf("Integer: %d\n", integerNum);
printf("Float: %f\n", floatNum);
return 0;
}
Explanation:
- Include the necessary header file
stdio.h
. - Define the
main()
function. - Declare an integer variable
integerNum
and initialize it with the value10
. - Declare a floating-point variable
floatNum
without initialization. - Convert the
integerNum
variable to a float using typecasting(float)integerNum
. This converts the value ofintegerNum
from an integer to a float. - Print the original integer value using
printf
with the%d
format specifier. - Print the converted float value using
printf
with the%f
format specifier. return 0;
signifies the end of themain()
function and the successful execution of the program.