Guardian Native C Library Calls Reference Manual (G06.29+, H06.08+, J06.03+)
strtol(3) Guardian Native C Library Calls Reference Manual
• If the base value is between 2 and 36, the subject string can be a sequence of digits and
letters a or A to z or Z that are used to represent an integer in the specified base. Alpha-
betic characters represent digits with an equivalent decimal value from 10 (for the letter
A) to 35 (for the letter Z). The subject string can only have digits with a value less than
base and alphabetic characters with equivalent values less than base. For example, when
the value of the base parameter is 20, only the following value assignments are con-
verted:
Character 0123456789ABCDEFGHIJ
abcdefghij
Value 012345678910111213141516171819
The subject string can optionally be preceded by a + (plus sign) or - (minus sign), but cannot
include an integer suffix (such as L). If the subject string is preceded by a - (minus sign), the con-
verted integer value has a negative value. If the value of base is 16, the characters 0x or 0X may
optionally precede the sequence of letters or digits, following the sign, if present.
The character string is parsed to skip the initial space characters (as determined by the isspace()
function). Any nonspace character is the starting of a potential subject string that may form an
integer in the base specified by the base parameter. The subject sequence is defined to be the
longest initial substring that is of the expected form of long integer. Any character that does not
satisfy this expected form begins the final sequence of unrecognized characters. The strtol()
function sets the location pointed to by the endptr parameter to point to this final sequence of
unrecognized characters except when endptr is a null pointer.
The LC_CTYPE category of the locale controls what characters are treated as spaces but does
not effect the interpretation of characters as part of the subject string. The characters in the sub-
ject string are always treated as if the locale was the C/POSIX locale.
In the TNS environment, the strtol() function supports the C/POSIX locale only. In the native
environment, all locales are supported.
EXAMPLES
The following example converts a character string to a signed long integer.
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <errno.h>
#define LENGTH 40
main()
{
char String[LENGTH], *endptr;
long int retval;
(void)setlocale(LC_ALL, "");
if (fgets(String, LENGTH, stdin) != NULL) {
errno = 0;
retval = strtol ( String, &endptr, 0 );
if (retval == 0 && (errno != 0
|| String == endptr)) {
/* No conversion could be performed */
printf("No conversion performed\n");
} else if (errno !=0 && (retval == LONG_MAX
|| retval == LONG_MIN)) {
/* Error handling */
6−134 Hewlett-Packard Company 527192-018