C strtof() Function | Convert String to Float with Error Checking

Introduction

The strtof() function in C is a standard library function that converts a string to a float. It is part of the C standard library (stdlib.h). This function is more versatile and robust than atof() because it allows for error checking and supports more complex input strings.

strtof() Function Syntax

The syntax for the strtof() function is as follows:

float strtof(const char *str, char **endptr);

Parameters:

  • str: A C string that contains the representation of a floating-point number.
  • endptr: A pointer to a character pointer. If endptr is not NULL, strtof() stores the address of the first invalid character in *endptr.

Returns:

  • The function returns the converted float value. If no valid conversion could be performed, it returns 0.0f and sets endptr to str.

Examples

Converting a Simple String to Float

To demonstrate how to use strtof() to convert a string to a float, we will write a simple program.

Example

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

int main() {
    const char *str = "123.456";
    char *endptr;
    float num;

    // Convert string to float
    num = strtof(str, &endptr);

    // Print the converted value
    printf("The converted value is: %f\n", num);

    // Print the remaining part of the string
    if (*endptr != '\0') {
        printf("Remaining part of the string: %s\n", endptr);
    }

    return 0;
}

Output:

The converted value is: 123.456001

Handling Invalid Input

This example shows how strtof() behaves with invalid input.

Example

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

int main() {
    const char *str = "abc123.456";
    char *endptr;
    float num;

    // Convert string to float
    num = strtof(str, &endptr);

    // Print the converted value
    printf("The converted value is: %f\n", num);

    // Print the remaining part of the string
    if (*endptr != '\0') {
        printf("Remaining part of the string: %s\n", endptr);
    }

    return 0;
}

Output:

The converted value is: 0.000000
Remaining part of the string: abc123.456

Real-World Use Case

Converting User Input to Float with Error Checking

In real-world applications, the strtof() function can be used to convert user input, provided as a string, into a float with proper error checking.

Example

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

int main() {
    char input[100];
    char *endptr;
    float value;

    // Prompt the user for input
    printf("Enter a floating-point number: ");
    fgets(input, sizeof(input), stdin);

    // Convert input to float
    value = strtof(input, &endptr);

    // Check for errors
    if (endptr == input) {
        printf("No valid conversion could be performed.\n");
    } else {
        // Print the converted value
        printf("You entered: %f\n", value);

        // Print the remaining part of the string
        if (*endptr != '\0' && *endptr != '\n') {
            printf("Remaining part of the string: %s\n", endptr);
        }
    }

    return 0;
}

Output (example user input "45.67abc"):

Enter a floating-point number: 45.67abc
You entered: 45.670002
Remaining part of the string: abc

Conclusion

The strtof() function is useful for converting strings that represent floating-point numbers into float values. It is essential for precise and reliable numerical data conversion, providing error handling and the ability to parse more complex number formats.

Comments