atof in c: The function atof converts a string to a floating-point number(double). The function skips all white-space characters at the beginning of the string until the first non-whitespace character is found. Then it converts the subsequent characters into a floating point number until it encounters a character which is not a number.
Function prototype of atof
double atof(const char *str);
- str : A pointer to a string beginning with the representation of a floating-point number.
Return value of atof
The function returns the converted floating point number as a double value otherwise it returns zero (0.0), If no valid conversion could be performed.
C program using atof function
The following program shows the use of atof function for string to floating point number conversion.
#include <stdio.h> #include <stdlib.h> int main(){ char string[100]; double value; printf("Enter a string\n"); scanf("%s", &string); /* String to floating point number conversion */ value = atof(string); printf("Floating point value %lf\n", value); return 0; }
Output
Enter a string 1234.2651 Floating point value 1234.265100
Enter a string 1234.33ABCD Floating point value 1234.330000
Enter a string 123ABCD Floating point value 123.000000
Enter a string ABCDEF Floating point value 0.000000