https://www.codegrepper.com/search.php?q=convert%20string%20to%20float%20c
Converting a string to a float in C
To convert a string to a float in C, you can use the atof()
function from the C standard library. This function takes a string as input and returns the corresponding floating-point value.
Here's an example of how you can use the atof()
function:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "3.14";
float num = atof(str);
printf("The converted float value is: %f\n", num);
return 0;
}
In this example, the string "3.14"
is converted to a float using the atof()
function, and the result is stored in the variable num
. The converted float value is then printed using printf()
.
Please note that the atof()
function returns 0.0 if the input string cannot be converted to a float.
I hope this helps! Let me know if you have any further questions.