Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

ctype(3C)

scanf(3S)

strtod(3C)

STRTOL(3C)                           SysV                           STRTOL(3C)



NAME
     strtol, strtoul, atol, atoi - convert string to integer

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

     unsigned long int strtoul(str, ptr, base)
     const char *str;
     char **ptr;
     int base;

     long int atol (str)
     const char *str;

     int atoi (str)
     const char *str;

DESCRIPTION
     strtol (strtoul) returns as a long integer (unsigned 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(3C)) 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 thusly: 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, of course, take place upon assignment or
     by an explicit cast.

     Except for the behavior on error, atol(str) is equivalent to

          strtol(str, (char **)NULL, 10)

     and atoi(str) is equivalent to

          (int) strtol(str, (char **)NULL, 10)

SEE ALSO
     ctype(3C), scanf(3S), strtod(3C).

DIAGNOSTICS
     The behavior of atoi and atol is undefined if the value of the result
     cannot be represented.  The value of errno is not affected.

     strtol and strtoul return the converted value, if any.  If no conversion
     could be performed, they return zero.  If the correct value is outside
     the range of representable values, strtol and strtoul set errno to
     ERANGE.  In this case, strtol returns LONG_MAX or LONG_MIN (according to
     the sign of the value) and strtoul returns ULONG_MAX.

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026