convert string to float c
To convert a string to a float in C, you can use the atof()
function. Here's an example:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "3.14";
float num = atof(str);
printf("The float value is: %f\n", num);
return 0;
}
In this example, the atof()
function is used to convert the string "3.14"
to a float value. The resulting float value is then printed using printf()
.
Please note that the atof()
function returns a double
value, but it can be assigned to a float
variable since float
is a smaller data type than double
.
I hope this helps! Let me know if you have any further questions.