strtol(3C)
_________________________________________________________________
strtol function
Convert a string to a long integer.
_________________________________________________________________
Calling Sequence
long strtol(), number;
char *string;
char **pointer;
int base;
number = strtol(string, pointer, base);
where string is a pointer to an array of characters,
beginning with optional whitespace characters and an
optional sign (+ or -). A string of digits follows.
Conversion stops when the function encounters the
first character that isn't legal for the base. If
the base is greater than 10, the alphabetic
characters A - Z and a - z are treated as digits 10
- 35.
pointer is either a pointer to a character pointer
or (char **) 0. If you pass the pointer, the
character pointer that it points to will be updated
to point to the character in string immediately
after the digit string.
base is an integer which indicates the base to
convert the digit string in. If base is positive,
and less than or equal to 36, strtol uses it as the
base to convert string to. If it is 16, an optional
leading 0x or 0X is ignored. If the base is 0,
strtol uses the C rules for converting integer
constants:
* A leading 0x or 0X means the base is 16.
* A leading 0 means the base is 8.
* In all other cases, the base is 10.
Description
The strtol function converts strings into integral values. It is
a more general function than atol and atoi. It can convert in
any base in the range of 2 to 36. It will also return the
pointer to the next character in the string after the conversion.
The strtol function is compatible with the UNIX system V function
of the same name.
DG/UX 4.00 Page 1
Licensed material--property of copyright holder(s)
strtol(3C)
Note the following:
Expression Is Equivalent to:
atol(string) strtol(string, (char **) 0, 10)
atoi(string) (int) strtol(string, (char **) 0, 10)
Returns
The strtol function returns the converted number as a long
integer. On illegal input, it returns 0 with no diagnostic. It
ignores integer overflow.
Related Functions
See also the atoi, atol, sscanf, and strtod functions.
Example
/* Program test for the strtol() function */
#include <stdio.h>
main(argc, argv)
int argc;
char *argv[];
{
long strtol();
int i = 1;
while (i < argc - 1) {
printf(" %s radix %s is: %ld.\n",
argv[i], argv[i + 1], strtol( argv[i],
(char **)0, atoi(argv[i + 1])));
i += 2;
}
}
A call to the program test with
XEQ test 10, 2, A, 12, 100, 22
generates the following:
10 radix 2 is: 2.
A radix 12 is: 10.
100 radix 22 is: 484.
DG/UX 4.00 Page 2
Licensed material--property of copyright holder(s)