Convert strings to a double-precision (strtod), long-double-precision (_strtold), long-integer (strtol), or unsigned long-integer (strtoul) value.
#include <stdlib.h>
double strtod( const char *nptr, char **endptr );
long strtol( const char *nptr, char **endptr, int base );
long double _strtold( const char *nptr, char **endptr );
unsigned long strtoul( const char *nptr, char **endptr, int base );
nptr | String to convert | |
endptr | Pointer to character that stops scan | |
base | Number base to use |
The strtod, _strtold, strtol, and strtoul functions convert a character string to a double-precision value, a long-double value, a long-integer value, or an unsigned long-integer value, respectively. The input string is a sequence of characters that can be interpreted as a numerical value of the specified type.
These functions stop reading the string at the first character they cannot recognize as part of a number. This may be the null character ('\0') at the end of the string. With strtol or strtoul, this terminating character can also be the first numeric character greater than or equal to base. If endptr is not NULL, a pointer to the character that stopped the scan is stored at the location pointed to by endptr. If no conversion could be performed (no valid digits were found or an invalid base was specified), the value of nptr is stored at the location pointed to by endptr.
The strtod and _strtold functions expect nptr to point to a string with the following form:
[[whitespace]] [[sign]] [[digits]] [[.digits]] [[ {d | D| e | E}[[sign]]digits]]
A whitespace consists of space and tab characters, which are ignored; sign is either plus (+) or minus (–); and digits are one or more decimal digits. If no digits appear before the decimal point, at least one must appear after the decimal point. The decimal digits can be followed by an exponent, which consists of an introductory letter (b, D, e, or E) and an optionally signed decimal integer.
The first character that does not fit this form stops the scan.
The strtol function expects nptr to point to a string with the following form:
[[whitespace]] [[sign]] [[0]] [[{ x | X }]] [[digits]]
The strtoul function expects nptr to point to a string having this form:
[[whitespace]] [[{ + | -}]] [[0]] [[{ x | X}]] [[digits]]
If base is between 2 and 36, then it is used as the base of the number. If base is 0, the initial characters of the string pointed to by nptr are used to determine the base. If the first character is 0 and the second character is not 'x' or 'X', then the string is interpreted as an octal integer; otherwise, it is interpreted as a decimal number. If the first character is '0' and the second character is 'x' or 'X', then the string is interpreted as a hexadecimal integer. If the first character is '1' through '9', then the string is interpreted as a decimal integer. The letters 'a' through 'z' (or 'A' through 'Z') are assigned the values 10 through 35; only letters whose assigned values are less than base are permitted.
The strtoul function allows a plus (+) or minus (–) sign prefix; a leading minus sign indicates that the return value is negated.
The strtod and _strtold functions return the value of the floating-point number, except when the representation would cause an overflow, in which case they return ±HUGE_VAL. The functions return 0 if no conversion could be performed or an underflow occurred.
The strtol function returns the value represented in the string, except when the representation would cause an overflow, in which case it returns LONG_MAX or LONG_MIN. The function returns 0 if no conversion could be performed.
The strtoul function returns the converted value, if any. If no conversion can be performed, the function returns 0. The function returns ULONG_MAX on overflow.
In all four functions, errno is set to ERANGE if overflow or underflow occurs.
strtod, strtol
Standards:ANSI, UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
_strtold
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:None
strtoul
Standards:ANSI
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
/* STRTOD.C: This program uses strtod to convert a string to a
* double-precision value; strtol to convert a string to long
* integer values; and strtoul to convert a string to unsigned
* long-integer values.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char *string, *stopstring;
double x;
long l;
int base;
unsigned long ul;
string = "3.1415926This stopped it";
x = strtod( string, &stopstring );
printf( "string = %s\n", string );
printf(" strtod = %f\n", x );
printf(" Stopped scan at: %s\n\n", stopstring );
string = "-10110134932This stopped it";
l = strtol( string, &stopstring, 10 );
printf( "string = %s\n", string );
printf(" strtol = %ld\n", l );
printf(" Stopped scan at: %s\n\n", stopstring );
string = "10110134932";
printf( "string = %s\n", string );
/* Convert string using base 2, 4, and 8: */
for( base = 2; base <= 8; base *= 2 )
{
/* Convert the string: */
ul = strtoul( string, &stopstring, base );
printf( " strtol = %ld (base %d)\n", ul, base );
printf( " Stopped scan at: %s\n", stopstring );
}
}
string = 3.1415926This stopped it
strtod = 3.141593
Stopped scan at: This stopped it
string = -10110134932This stopped it
strtol = -2147483647
Stopped scan at: This stopped it
string = 10110134932
strtol = 45 (base 2)
Stopped scan at: 34932
strtol = 4423 (base 4)
Stopped scan at: 4932
strtol = 2134108 (base 8)
Stopped scan at: 932