Syntax
#include <stdlib.h> long int strtol(const char *nptr, char **endptr, int base);Description
strtol converts a character string to a long-integer value. The parameter nptr points to a sequence of characters that can be interpreted as a numerical value of type long int. This function stops reading the string at the first character that it cannot recognize as part of a number. This character can be the null character (\0) at the end of the string. The ending character can also be the first numeric character greater than or equal to the base.
When you use the strtol function, nptr should point to a string with
the following form: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ ³
³ >>ÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÂÄÄÄÂÄÄÂÄÄÄÄÂÄÄÂÄÄÄÄÄÄÄÄÂÄÄ><
³
³ ÀÄwhite-spaceÄÙ ÃÄ+Ä´ ÃÄ0ÄÄ´ ÀÄdigitsÄÙ ³
³ ÀÄÁÄÙ ÃÄ0xÄ´ ³
³ ÀÄ0XÄÙ ³
³ ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
If base is in the range of 2 through 36, it becomes the base of the number. If base is 0, the prefix determines the base (8, 16, or 10): the prefix 0 means base 8 (octal); the prefix 0x or 0X means base 16 (hexadecimal); using any other digit without a prefix means decimal.
strtol returns the value represented in the string, except when the representation causes an overflow. For an overflow, it returns LONG_MAX or LONG_MIN, according to the sign of the value and errno is set to ERANGE. If base is not a valid number, strtol sets errno to EDOM.
errno is set to ERANGE for the exceptional cases, depending on the base of the value. If the string pointed to by nptr does not have the expected form, no conversion is performed and the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.
This example converts the strings to a long value. It prints out the converted value and the substring that stopped the conversion.
#include <stdlib.h> #include <stdio.h> int main(void) { char *string,*stopstring; long l; int bs; string = "10110134932"; printf("string = %s\n", string); for (bs = 2; bs <= 8; bs *= 2) { l = strtol(string, &stopstring, bs); printf(" strtol = %ld (base %d)\n", l, bs); printf(" Stopped scan at %s\n\n", stopstring); } return 0; /**************************************************************************** The output should be: string = 10110134932 strtol = 45 (base 2) Stopped scan at 34932 strtol = 4423 (base 4) Stopped scan at 4932 strtol = 2134108 (base 8) Stopped scan at 932 ****************************************************************************/ }
Related Information