Chapter Contents |
Previous |
Next |
strtod, strtof, strtold |
Portability: | ISO/ANSI C conforming, UNIX compatible |
SYNOPSIS | |
DESCRIPTION | |
RETURN VALUE | |
DIAGNOSTICS | |
EXAMPLE | |
RELATED FUNCTIONS | |
SEE ALSO |
SYNOPSIS |
#include <stdlib.h> double strtod(const char *str, char **end); float strtof(const char *str, char **end); long double strtold(const char *str, char **end);
DESCRIPTION |
strtod
, strtof
, and strtold
, expect
a floating-point number in C syntax, with these specifications:
F
or L
) is allowed
INF
or INFINITY
, ignoring case
NAN
or
NAN(character sequence)
,
ignoring case in the NAN
part. See the nan
function description for information about character sequence.
If
the end
value is not NULL
, *end
is modified to address
the first character of the string that is not consistent with the floating-point
syntax above. However, if no initial segment of the string can be interpreted
as a floating-point number, str
is assigned
to *end
.
RETURN VALUE |
strtod
, strtof
, and strtold
functions
return the floating-point value (double
, float
, and long
double
) represented
by the character string up to the first
unrecognized character. If no initial segment of the string can be interpreted
as a floating-point number, 0.0 is returned.
DIAGNOSTICS |
If the floating-point value is outside the range of valid
floating-point numbers, errno
is set to ERANGE.
In this case, ±HUGE_VAL
(defined in
<math.h
>) is returned if the correct value
is too large, or 0.0 if the correct value is too close to 0.
EXAMPLE |
#include <stdlib.h> #include <ctype.h> #include <stdio.h> main() { double number; char *input, *stopchar; char string[20]; puts("Enter a string to be converted to double:"); input = gets(string); /* Skip space characters. */ while(isspace(*input)) ++input; /* Convert from character to double. */ number = strtod(input, &stopchar); /* Determine if string is valid. */ if (stopchar == input) printf("Invalid float number: %s\n", input); /* Check for characters afterwards. */ else if (*stopchar && !isspace(*stopchar)) printf("Extra characters after value ignored: %s\n", stopchar); printf("The entered string was converted to: %g\n", number); }
RELATED FUNCTIONS |
nan
, strtol
SEE ALSO |
"String Utility Functions" in Chapter 2, "Function Categories."
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2004 by SAS Institute Inc., Cary, NC, USA. All rights reserved.