strtof() Function
The strtof()
function is declared in the header file <stdlib.h>
.
The strtof()
function converts a C-string into a floating point number based on the current locale. It processes the string by discarding leading whitespace and then parsing the sequence of characters that form a valid floating point literal. This conversion supports decimal and hexadecimal formats, as well as special values like INF and NAN.
Syntax of strtof()
float strtof(const char* str, char** endptr);
Parameters
Parameter | Description |
---|---|
str | A C-string starting with the representation of a floating point number. |
endptr | A pointer to a pointer that is set to the character immediately following the parsed number. This parameter can be NULL if not needed. |
The function skips any leading whitespace characters and then interprets the valid portion of the string as a floating point literal. It supports both decimal and hexadecimal formats as well as special values such as INF, INFINITY, and NAN. If the conversion fails, strtof()
returns 0.0F.
Return Value
On success, strtof()
returns the converted floating point number as a value of type float
. If no valid conversion is performed, it returns 0.0F. In case the converted value is out of the representable range, it returns HUGE_VALF (with the appropriate sign) and sets errno
to ERANGE. Similarly, for underflow, it returns a value no greater than the smallest normalized positive number, and some implementations may also set errno
to ERANGE.
Examples for strtof()
Example 1: Basic String Conversion
This example demonstrates a basic conversion of a string representing a floating point number into a float.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *str = " 3.14159";
char *end;
float value = strtof(str, &end);
printf("Converted value: %f\n", value);
printf("Remaining string: %s\n", end);
return 0;
}
Explanation:
- The string contains leading whitespace followed by a valid floating point number.
strtof()
converts the string to a float and sets theend
pointer to the end of the numeric part.- The program prints the converted value and the remaining string (if any).
Program Output:
Converted value: 3.141590
Remaining string:
Example 2: Handling Invalid Input
This example shows the behavior of strtof()
when the string does not start with a valid floating point number.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *str = "abc123";
char *end;
float value = strtof(str, &end);
printf("Converted value: %f\n", value);
printf("Remaining string: %s\n", end);
return 0;
}
Explanation:
- The string starts with non-numeric characters.
- No conversion is performed, so
strtof()
returns 0.0F. - The
end
pointer remains at the start of the string, indicating the absence of a valid number.
Program Output:
Converted value: 0.000000
Remaining string: abc123
Example 3: Converting a Hexadecimal Floating-Point Number
This example demonstrates how to convert a hexadecimal floating point number using strtof()
.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *str = " -0x1.8p3"; // Represents -12.0 in decimal
char *end;
float value = strtof(str, &end);
printf("Converted value: %f\n", value);
printf("Remaining string: %s\n", end);
return 0;
}
Explanation:
- The string includes a hexadecimal floating point literal with a negative sign.
strtof()
correctly interprets the hexadecimal format and converts it to its float equivalent.- The converted value and any remaining characters are printed.
Program Output:
Converted value: -12.000000
Remaining string:
Example 4: Detecting Overflow and Underflow
This example illustrates how strtof()
behaves when the input string represents a number that is out of the representable range, causing overflow or underflow.
Program
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
int main() {
const char *overflowStr = "1e40"; // Likely to cause overflow
const char *underflowStr = "1e-50"; // Underflow value
char *end;
errno = 0;
float overflowVal = strtof(overflowStr, &end);
if (errno == ERANGE) {
printf("Overflow occurred: %f\n", overflowVal);
} else {
printf("Converted overflow value: %f\n", overflowVal);
}
errno = 0;
float underflowVal = strtof(underflowStr, &end);
if (errno == ERANGE) {
printf("Underflow occurred: %f\n", underflowVal);
} else {
printf("Converted underflow value: %f\n", underflowVal);
}
return 0;
}
Explanation:
- The program converts strings representing very large and very small numbers.
- If the converted number is out of range,
strtof()
returns HUGE_VALF (or a value close to the minimum positive value in case of underflow) and setserrno
to ERANGE. - The program checks
errno
to detect overflow or underflow and prints an appropriate message.
Program Output:
Overflow occurred: inf
Converted underflow value: 0.000000