Pointer to a null-terminated string representing a floating-point number
Read more about parameters of atof in parameters section
The atoffunction in C language Converts a string to a floating-point number.
The function discards any whitespace characters until the first non-whitespace character is found, then takes as many characters as possible to form a valid floating-point number representation and converts them to a double value. The rest of the string is ignored. While in general atof function acts very similar to atoi and atol, there is one crucial difference:the fact that it recognizes and takes care of decimal points and exponents.
To Summarize the Workflow:
1.
Takes a string (like "-12.34e5") as input.
2.
Scans the string, ignoring leading whitespace.
3.
If the string starts with a + or - sign, it interprets the number as positive or negative accordingly.
4.
Then processes the digits.
5.
Handles exponents (if present).
6.
Recognizes decimal points as part of the number.
7.
Stops at the first invalid character (like a letter or symbol not part of a valid floating-point number).
8.
Returns the floating-point value of the processed number (e.g., -1234000.0).
9.
If the string doesn’t start with a valid number, it returns 0.0.
Read more about return type and value ofatof function in return section.
The atoffunction takes 1
parameter:
•
const char * `str`: Pointer to a null-terminated string representing a floating-point number
Converts the initial portion of a string to a `double` value, interpreting the content as a floating-point number.
The atof function return value :
Returns the converted double value
If no valid conversion could be performed, it returns 0
0
Output
This example demonstrates the basic usage of the `atof` function with different input strings, including a positive number, a negative number with leading whitespace, and an invalid input.