strtol(3C) strtol(3C)
NAME
strtol, strtoul, atol, atoi, strtoll, strtoull, atoll - convert string
to integer
SYNOPSIS
#include <stdlib.h>
long int strtol(const char *str, char **endptr, int base);
unsigned long int strtoul(const char *str, char **endptr, int base);
long int atol(const char *str);
int atoi(const char *str);
long long strtoll(const char *str, char **ptr, int base);
unsigned long long strtoull(const char *str, char **ptr, int base);
long long atoll(const char *str);
DESCRIPTION
strtol() returns as a long int the value represented by the character
string pointed to by str. The string is scanned up to the first char-
acter inconsistent with the base. Leading "white-space" characters [as
defined by isspace() in ctype(3C)] are ignored.
If the value of endptr is not (char **)NULL, a pointer to the charac-
ter terminating the scan is returned in the location pointed to by
endptr. If no integer can be formed, that location is set to str, and
zero is returned.
If base is positive (and not greater than 36), it is used as the base
for conversion. After an optional leading sign, leading zeros are
ignored; if base is 16, the characters "0x" or "0X" are also ignored.
If base is zero, the string itself determines the base as follows:
After an optional leading sign a leading zero indicates octal conver-
sion, and a leading "0x" or "0X" hexadecimal conversion. Otherwise,
decimal conversion is used.
Truncation from long to int can, of course, take place upon assignment
or by an explicit cast.
If the value represented by str would cause overflow, LONGMAX or
LONGMIN is returned (according to the sign of the value), and errno
is set to the value ERANGE.
strtoul() is similar to strtol() except that strtol() returns as an
unsigned long int the value represented by str. If the value repre-
sented by str would cause overflow, ULONGMAX is returned, and errno
is set to the value ERANGE.
Page 1 Reliant UNIX 5.44 Printed 11/98
strtol(3C) strtol(3C)
strtoll() is similar to strtol() except that strtoll() returns as a
long long int the value represented by str. If the value represented
by str would cause overflow, LONGLONGMAX or LONGLONGMIN is returned
(according to the sign of the value), and errno is set to the value,
ERANGE.
strtoull() is similar to strtoll() except that strtoull() returns as
an unsigned long long int the value represented by str. If the value
represented by str would cause overflow, ULONGLONGMAX is returned,
and errno is set to the value, ERANGE.
Except for behavior on error, atol(str) is equivalent to:
strtol(str, (char **)NULL, 10).
Except for behavior on error, atoi(str) is equivalent to:
(int) strtol(str, (char **)NULL, 10).
Except for behavior on error, atoll(str) is equivalent to:
strtoll(str, (char **)NULL, 10).
RESULT
If strtol() is given a base greater than 36, it returns 0 and sets
errno to EINVAL.
ERRORS
The following error code descriptions are function-specific. You will
find a general description in introprm2(2) or in errno(5).
The strtol() function will fail if:
ERANGE The value to be returned is not representable.
The strtol() function may fail if:
EINVAL The value of base is not supported.
The strtoul() function will fail if:
EINVAL The value of base is not supported.
ERANGE The value to be returned is not representable.
The strtoul() function may fail if:
EINVAL No conversion could be performed.
Page 2 Reliant UNIX 5.44 Printed 11/98
strtol(3C) strtol(3C)
NOTES
Because no return value is reserved to indicate an error, an applica-
tion wishing to check for error situations should set errno to 0, then
call the function, then check errno. If it is non-zero, an error must
have occurred.
strtol() no longer accepts values greater than LONGMAX as valid
input. Use strtol() instead.
As the result is undefined for a non-representable return value for
atol() and atoi(), you should use strtol() instead of these functions.
SEE ALSO
ctype(3C), strtod(3C), scanf(3S), stdlib(5).
Page 3 Reliant UNIX 5.44 Printed 11/98