Guardian Native C Library Calls Reference Manual (G06.28+, H06.04+)

strtol(3) Guardian Native C Library Calls Reference Manual
converted:
Character 0123456789ABCDEFGHIJ
abcdefghij
Value 012345678910111213141516171819
The subject string can optionally be preceded by a + (plus sign) or - (minus sign), but cannot
include an integer sufx (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 specied by the base parameter. The subject sequence is dened 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 nal sequence of unrecognized characters. The strtol()
function sets the location pointed to by the endptr parameter to point to this nal 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 */
} else {
/* retval contains long integer */
printf("Integer in decimal is %d\n", retval);
}
}
}
684 Hewlett-Packard Company 527192-005