Character array conversion to an integral value of type long int.
#include <stdlib.h> long int strtol(const char *nptr,char **endptr, int base);
nptr
A pointer to a nul-terminated character string to convert.
endptr
A pointer to the position in nptr that could not be converted.
base
A numberic base between 2 and 36.
The strtol() function converts a character array, pointed to by nptr, expected to represent an integer expressed in radix base, to an integer value of type long int. If the sequence pointed to by nptr is a minus sign, the value resulting from the conversion is negated in the return value.
The base argument in strtol() specifies the base used for conversion. It must have a value between 2 and 36, or 0. The letters a (or A) through z (or Z) are used for the values 10 through 35; only letters and digits representing values less than base are permitted. If base is 0, then strtol() converts the character array based on its format. Character arrays beginning with '0' are assumed to be octal, number strings beginning with '0x' or '0X' are assumed to be hexadecimal. All other number strings are assumed to be decimal.
If the endptr argument is not a null pointer, it is assigned a pointer to a position within the character array pointed to by nptr . This position marks the first character that is not convertible to a long int value.
In other than the "C" locale, additional locale-specific subject sequence forms may be accepted. This function skips leading white space.
strtol() returns an integer value of type long int. If the converted value is less than LONG_MIN, strtol() returns LONG_MIN and sets errno to ERANGE. If the converted value is greater than LONG_MAX, strtol() returns LONG_MAX and sets errno to ERANGE. The LONG_MIN and LONG_MAX macros are defined in limits.h.
#include <stdlib.h> #include <stdio.h> int main(void) { long int i; unsigned long int j; long long int lli; unsigned long long ull; static char si[] = "4733777"; static char sb[] = "0x10*****"; static char sc[] = "66E00M???"; static char sd[] = "Q0N50Mabcd"; char *endptr; i = strtol(si, &endptr, 10); printf("%ld remainder of string = |%s|\n", i, endptr); i = strtol(si, &endptr, 8); printf("%ld remainder of string = |%s|\n", i, endptr); j = strtoul(sb, &endptr, 0); printf("%lu remainder of string = |%s|\n", j, endptr); j = strtoul(sb, &endptr, 16); printf("%lu remainder of string = |%s|\n", j, endptr); lli = strtoll(sc, &endptr, 36); printf("%lld remainder of string = |%s|\n", lli, endptr); ull = strtoull(sd, &endptr, 27); printf("%llu remainder of string = |%s|\n", ull, endptr); return 0; } Output: 4733 remainder of string = | 777| 2523 remainder of string = | 777| 16 remainder of string = |*****| 16 remainder of string = |*****| 373527958 remainder of string = |???| 373527958 remainder of string = | abcd|