CHAPTER 10 LIBRARY FUNCTIONS
User
’
s Manual U15556EJ1V0UM
197
strtol,
strtoul
Utility Functions
EXPLANATION
strtol
The
strtol
function decomposes the string pointed by pointer
nptr
into the following three parts.
(1) String of white-space characters that may be empty (to be specified by
isspace
)
(2) Integer representation by the base determined by the value of
“
base
”
(3) String of one or more characters that cannot be recognized (including null terminators)
The
strtol
function converts part (2) of the string into a long integer and returns this integer value.
A base of 0 indicates that the base should be determined from the leading digits of the string. A leading 0x or
0X indicates a hexadecimal number; a leading 0 indicates an octal number; otherwise, the number is
interpreted as decimal. (In this case, the number may be signed.)
If the base is 2 to 36, the set of letters from a to z or A to Z which can be part of a number (and which may be
signed) with any of these bases are taken to represent 10 to 35. A leading 0x or 0X is ignored if the base is
16.
If
endptr
is not a null pointer, a pointer to part (3) of the string is stored in the object pointed to by
endptr
.
If the correct value causes an overflow, the function returns
LONG_MAX
(2147483647) for the positive
overflow or
LONG_MIN
(
2147483648) for the negative overflow depending on the sign and sets
errno
to
ERANGE (2).
If the string in (2) is empty or the first non-white-space character of the string (2) is not appropriate for an
integer with the given base, the function performs no conversion and returns 0. In this case, the value of the
string
nptr
is stored in the object pointed to by
endptr
(if it is not a null string). This holds true with the bases
0 and 2 to 36.
strtoul
The
strtoul
function decomposes the string pointed by pointer
nptr
into the following three parts.
(1) String of white-space characters that may be empty (to be specified by
isspace
)
(2) Integer representation by the base determined by the value of
base
(3) String of one or more characters that cannot be recognized (including null terminators)
The
strtoul
function converts part (2) of the string into a unsigned long integer and returns this unsigned
long integer value.
A base of 0 indicates that the base should be determined from the leading digits of the string. A leading 0x or
0X indicates a hexadecimal number; a leading 0 indicates an octal number; otherwise, the number is
interpreted as decimal.
If the base is 2 to 36, the set of letters from a to z or A to Z which can be part of a number (and which may be
signed) with any of these bases are taken to represent 10 to 35. A leading 0x or 0X is ignored if the base is
16.
If
endptr
is not a null pointer, a pointer to part (3) of the string is stored in the object pointed to by
endptr
.