Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

strtof function in C

  • Last Updated : 22 Jul, 2019

Parses the C-string str(assumed) interpreting its content as a floating-point number (according to the current locale ) and returns its value as a float. If endptr(end pointer) is not a null pointer, the function also sets the value of endptr to point to the first character after the number.


Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.
strtof(const char* str, char **endptr)
str : String object with the representation of floating point number
endptr : Reference to an already allocated object of type char*, 
whose value is set by the function to the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.
Return Value : On success, the function returns the
 converted floating-point number as a value of type float.

// C code to convert string having
// floating point as its content
// using strtof function
#include <stdio.h>
#include <stdlib.h> // Header file containing strtof function
int main()
    // Character array to be parsed
    char array[] = "365.25 7.0";
    // Character end pointer
    char* pend;
    // f1 variable to store float value
    float f1 = strtof(array, &pend);
    // f2 variable to store float value
    float f2 = strtof(pend, NULL);
    // Printing parsed float values of f1 and f2
    printf("%.2f\n%.2f\n", f1, f2);
    // Performing operation on the values returned
    printf(" One year has %.2f weeks \n", f1 / f2);
    return 0;


One year has 52.18 weeks

This article is contributed by Mohak Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Recommended Articles
Page :