OneBite.Dev - Coding blog in a bite size

convert variable from int to float in C

Code snippet on how to convert variable from int to float in C

int myInt = 5;
float myFloat;
myFloat = (float) myInt;

This code snippet shows how to convert an integer variable called “myInt” to a float variable called “myFloat” in the C programming language. The integer variable is initialized by setting its value to 5. The float variable is declared, but not initialized. The last line of code converts the integer value to float and assigns it to the float variable. The line starts with a special keyword, “cast”, and the type of the desired output in parentheses, in this case float. The integer value is given as the argument after the parentheses. By casting, the integer value is converted to float and stored in the float variable.

c