Name

strtod, strtof, strtold

Synopsis

Converts a string into a floating-point number

#include <stdlib.h>
doublestrtod( const char * restrict s, char ** restrict endptr );
float  strtof( const char * restrict s, char ** restrict endptr );   (C99)
long double strtold( const char * restrict s, char ** restrict endptr  ); (C99)

The strtod() function attempts to interpret the string addressed by its first pointer argument, s, as a floating-point numeric value, and returns the result with the type double. strtof() and strold() are similar, but return float and long double respectively. Leading whitespace characters are ignored, and the string converted ends with the last character that can be interpreted as part of a floating-point numeral. The second parameter, endptr, is a pointer to a pointer. If its argument value is not a null pointer, then the function stores a pointer to the first character that is not part of the numeral converted in the location addressed by the endptr argument. (The locations that the function reads from and writes to using its restricted pointer parameters must not overlap.) If no conversion is possible, the function returns 0.

If the resulting value exceeds the range of the function’s type, then the return value is positive or negative HUGE_VAL (or HUGE_VALF or HUGE_VALL, for the float and long double variants). On an overflow, the errno variable is set to the value of ERANGE (“range error”). If the conversion produces an underflow, the magnitude of the return value ...

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.