how to convert string to float in c code example

Example 1: convert string to float c

char myString = "6.88";
float x = atof(myString);
//x is now 6.88

Example 2: atof in c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
   float val;
   char str[20];
   
   strcpy(str, "98993489");
   val = atof(str);//returns the string as a floating point number
   printf("String value = %s, Float value = %f\n", str, val);

   strcpy(str, "tutorialspoint.com");
   val = atof(str);
   printf("String value = %s, Float value = %f\n", str, val);

   return(0);
}

Tags:

C Example