Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

strtod, strtof, strtol - Convert a string into a floating-point number

&pagelevel(4)&pagelevel

Definition    

#include <stdlib.h>

double strtod(const char *s, char **p);
float strtof(const char *s, char **p);
long double strtold(const char *s, char **p);

These functions convert the string to which s points into a floating-point number of type double. The string to be converted may be structured as follows:

[{ tab |'BLANK'}...][+|-][ digit ...][.][ digit ...][{E|e}[+|-] digit ...]
or
[{ tab |'BLANK'}...][+|-]0{X|x}[ hexdigit ...][.][ hexdigit ...][{P|p}[+|-] digit ...]

Any control character for white space may be used for tab (see definition under isspace).

strtod also recognizes strings that start with a digit but end with any character.
In such cases, strtod first truncates the numeric part and converts it to a floating-point value.

strtod additionally provides a pointer (*p) to the first non-convertible character in string s via the second argument p of type char **. If no conversion is possible at all, *p is set to the start address of string s.
However, this occurs only if p is not passed as a NULL pointer.

If p is a NULL pointer, strtod is executed like the atof function:

strtod(s, (char **)NULL) and strtod(s, NULL) are both equivalent to atof(s).

Return val.

Floating-point number of type double, float or long double



for strings which are structured as described above and represent a numeric value within the permissible floating-point range.


0

for strings that do not conform to the syntax described above or do not begin with convertible characters.


+/-HUGE_VAL
+/-HUGE_VALF
+/-HUGE_VALL

depending on the function type and the sign of x, for strings whose numeric value lies outside the permissible floating-point range.
In addition, errno is set to ERANGE (result too large).

Note

The decimal point character (period or comma) in the string to be converted is determined by the locale (category LC_NUMERIC). The default setting is a period.

Example

The following program converts a string passed during the call (Enter Options) into its corresponding floating-point number and outputs the first non-convertible character, if any.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
               /* Numbers are passed as strings!!
                  A conversion is necessary if the
                  numeric value is required */
{
  char *p;
  printf("floating : %f\n", strtod(argv[1], &p));
  putchar(*p);
  return 0;
}

See also

atof, atoi, atol, strtol, strtoul