strtol -- convert string to integer


cc . . . -lc

#include  <stdlib.h>
#include  <limits.h>        /* ANSI-only */

long strtol (str, ptr, base) char *str, **ptr; int base;


The strtol function returns as a long integer the value represented by the character string pointed to by str. The string is scanned up to the first character inconsistent with the base. Leading ``white-space'' characters (as defined by isspace in ctype(S)) are ignored.

If the value of ptr is not (char **)NULL, a pointer to the character terminating the scan is returned in the location pointed to by ptr. 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, and ``0x'' or ``0X'' is ignored if base is 16.

If base is zero, the string itself determines the base. After an optional leading sign a leading zero indicates octal conversion, and a leading ``0x'' or ``0X'' hexadecimal conversion. Otherwise, decimal conversion is used.

Truncation from long to int can take place upon assignment or by an explicit cast.

Upon successful completion, strtol returns the converted value, if any. If the correct value causes overflow, and you are compiling with the cc -ansi flag, errno is set to ERANGE and LONG_MIN is returned if the value is negative, and LONG_MAX is returned if the value is positive. (LONG_MIN and LONG_MAX are defined in limits.h.) For all other conformance standards, the value returned is either a positive or negative long integer and ERANGE is returned in errno. For all other errors, zero is returned and errno indicates the error. EINVAL is returned when the value of base is greater than 36 or less than 2.

See also

atof(S), ctype(S), scanf(S), strtod(S)

Standards conformance

strtol is conformant with:

X/Open Portability Guide, Issue 3, 1989 ;
and ANSI X3.159-1989 Programming Language -- C .

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003