To write a function in C that parses a float number from a string, including support for scientific notation (e.g., 1.23e4 or -0.456E-2), you can utilize the standard C standard library function strtof (or strtod for double values).
Here's an example implementation in C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
float parse_float(const char *str, int *error) {
char *endptr;
*error = 0;
// Ensure the input string is non-NULL
if (str == NULL) {
*error = 1; // Error: null input
return 0.0f;
}
// Use strtof to parse the float number
errno = 0;
float result = strtof(str, &endptr);
// Check if the string was valid (endptr must advance)
if (endptr == str) {
*error = 2; // Error: no conversion
return 0.0f;
}
// Check if we reached the end of the string (optional, if you want strict parsing)
while (isspace((unsigned char)*endptr)) {
endptr++;
}
if (*endptr != '\0') {
*error = 3; // Error: extra characters after the valid float
return 0.0f;
}
// Handle out-of-range errors
if (errno == ERANGE) {
*error = 4; // Error: out of range
return 0.0f; // Or you can return the overflowed value
}
// Return the parsed value
return result;
}
int main() {
const char *test_strings[] = {
"3.14",
"-0.123",
"1.23e4",
"-0.456E-2",
"1.0e308", // Float overflow
"not_a_number",
NULL, // Invalid case: NULL string
"3.14abc" // Invalid trailing characters
};
for (int i = 0; i < sizeof(test_strings) / sizeof(test_strings[0]); i++) {
int error;
float value = parse_float(test_strings[i], &error);
if (error != 0) {
printf("Input: %-10s -> Error: %d\n",
test_strings[i] ? test_strings[i] : "NULL", error);
} else {
printf("Input: %-10s -> Parsed float: %f\n",
test_strings[i], value);
}
}
return 0;
}
Explanation:
-
strtof Function Usage:
The strtof function is used to convert a string to a float. It interprets a variety of formats, including decimal numbers and scientific notation. It sets an endptr to point to the first unprocessed character.
-
Error Handling:
- Check if the input string is
NULL.
- Check if
endptr didn’t advance, indicating the input was not a number.
- Check if there are extra characters after the valid float number.
- Handle range errors using
errno.
-
Output:
The function returns the parsed float. It also sets an error code, so you can take appropriate action.
Error Codes:
1: Input string was NULL.
2: No conversion could be performed (e.g., string was "abc").
3: Unexpected characters trailing a valid float (e.g., "3.14abc").
4: Overflow or underflow occurred due to out-of-range numbers.
Test Outputs:
For example, if you run the code, you would get the following results:
Input: 3.14 -> Parsed float: 3.140000
Input: -0.123 -> Parsed float: -0.123000
Input: 1.23e4 -> Parsed float: 12300.000000
Input: -0.456E-2 -> Parsed float: -0.004560
Input: 1.0e308 -> Error: 4
Input: not_a_number -> Error: 2
Input: NULL -> Error: 1
Input: 3.14abc -> Error: 3
This implementation is robust and covers a variety of edge cases. Let me know if you have any further questions!