Safely convert strings to numbers with strtoX functions
suggest changestrtol, strtoul, and strtod have been in the library since C89. C99 added strtoll, strtoull, strtof, and strtold. The names have the form strtoX, where X selects the result type.
double strtod(char const *p, char **endptr);
long double strtold(char const *p, char **endptr);
Set errno to zero before the call. If the value overflows and the default rounding is in effect, strtod returns HUGE_VAL or -HUGE_VAL, according to the sign, and stores ERANGE in errno. Underflow is not the same test: the result then has a magnitude no larger than the smallest normalized positive value, and whether errno becomes ERANGE is implementation-defined. HUGE_VAL is declared in <math.h>; errno and ERANGE come from <errno.h>.
errno = 0;
double ret = strtod(argv[1], 0);
if ((ret == HUGE_VAL || ret == -HUGE_VAL) && errno == ERANGE)
return; /* overflow */
That return belongs in a function. If the subject sequence contains no number, strtod returns 0.0 and does not report an error through errno.
When endptr is not a null pointer, the function stores the address of the first unconverted character through it. Passing NULL (or 0) ignores that result.
char *check = 0;
errno = 0;
double ret = strtod(argv[1], &check);
if (argv[1] == check)
return; /* no conversion was performed */
else if (*check != '\0')
return; /* trailing junk after the number */
else if ((ret == HUGE_VAL || ret == -HUGE_VAL) && errno == ERANGE)
return; /* overflow */
argv[1] == check means nothing was converted. A conversion can still leave trailing characters; *check != '\0' detects them. Whitespace after the number is not consumed, so it counts as trailing text.
The wider integer conversions are analogous. A leading minus sign is accepted; for the unsigned functions the converted value is negated in the unsigned type, so "-1" becomes the maximum value of the result type rather than a failed conversion.
long strtol(char const *p, char **endptr, int base);
long long strtoll(char const *p, char **endptr, int base);
unsigned long strtoul(char const *p, char **endptr, int base);
unsigned long long strtoull(char const *p, char **endptr, int base);
base is the numeric base, from 2 to 36. The value 0 selects the base the way an integer constant does: a 0x or 0X prefix is hexadecimal, a leading 0 is octal, and any other form is decimal.
long a = strtol("101", 0, 2 ); /* a = 5L */
long b = strtol("101", 0, 8 ); /* b = 65L */
long c = strtol("101", 0, 10); /* c = 101L */
long d = strtol("101", 0, 16); /* d = 257L */
long e = strtol("101", 0, 0 ); /* e = 101L */
long f = strtol("0101", 0, 0 ); /* f = 65L */
long g = strtol("0x101", 0, 0 ); /* g = 257L */
A command-line argument can be converted the same way. argc is at least 1 when the program name is present, so the number is missing when argc < 2. strtoull can represent every size_t value, but a successful call can still be out of range for size_t, and an out-of-range subject sequence sets errno to ERANGE and returns ULLONG_MAX. Check errno, endptr, and the range before storing the result.
int main(int argc, char *argv[]) {
if (argc < 2)
return EXIT_FAILURE; /* No number given. */
unsigned long long converted = strtoull(argv[1], 0, 0);
/* Reject no conversion, trailing junk, ERANGE, and values above SIZE_MAX. */
return EXIT_SUCCESS;
}
With base 0, the same program accepts octal, decimal, and hexadecimal arguments.