| Functions | |||||||||||||||||||||||||||||||||||||||||
| Character manipulation | |||||||||||||||||||||||||||||||||||||||||
| Conversions to and from numeric formats | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String manipulation | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String examination | |||||||||||||||||||||||||||||||||||||||||
| Memory manipulation | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
| Miscellaneous | |||||||||||||||||||||||||||||||||||||||||
(C11)(C11) | |||||||||||||||||||||||||||||||||||||||||
Defined in header <stdlib.h> | ||
long strtol(constchar* str,char** str_end,int base); | (until C99) | |
long strtol(constchar*restrict str,char**restrict str_end,int base); | (since C99) | |
longlong strtoll(constchar*restrict str,char**restrict str_end,int base); | (since C99) | |
Interprets an integer value in a byte string pointed to bystr.
Discards any whitespace characters (as identified by callingisspace) until the first non-whitespace character is found, then takes as many characters as possible to form a validbase-n (where n=base) integer number representation and converts them to an integer value. The valid integer value consists of the following parts:
0) indicating octal base (applies only when the base is8 or0)0x or0X) indicating hexadecimal base (applies only when the base is16 or0)The set of valid values for base is{0, 2, 3, ..., 36}. The set of valid digits for base-2 integers is{0, 1}, for base-3 integers is{0, 1, 2}, and so on. For bases larger than10, valid digits include alphabetic characters, starting fromAa for base-11 integer, toZz for base-36 integer. The case of the characters is ignored.
Additional numeric formats may be accepted by the currently installed Clocale.
If the value ofbase is0, the numeric base is auto-detected: if the prefix is0, the base is octal, if the prefix is0x or0X, the base is hexadecimal, otherwise the base is decimal.
If the minus sign was part of the input sequence, the numeric value calculated from the sequence of digits is negated as if byunary minus in the result type.
The functions set the pointer pointed to bystr_end to point to the character past the last numeric character interpreted. Ifstr_end is a null pointer, it is ignored.
If thestr is empty or does not have the expected form, no conversion is performed, and (ifstr_end is not a null pointer) the value ofstr is stored in the object pointed to bystr_end.
Contents |
| str | - | pointer to the null-terminated byte string to be interpreted |
| str_end | - | pointer to a pointer to character |
| base | - | base of the interpreted integer value |
#include <errno.h>#include <limits.h>#include <stdbool.h>#include <stdio.h>#include <stdlib.h> int main(void){// parsing with error handlingconstchar* p="10 200000000000000000000000000000 30 -40 junk";printf("Parsing '%s':\n", p); for(;;){// errno can be set to any non-zero value by a library function call// regardless of whether there was an error, so it needs to be cleared// in order to check the error set by strtolerrno=0;char* end;constlong i= strtol(p,&end,10);if(p== end)break; const bool range_error=errno==ERANGE;printf("Extracted '%.*s', strtol returned %ld.",(int)(end-p), p, i); p= end; if(range_error)printf("\n --> Range error occurred."); putchar('\n');} printf("Unextracted leftover: '%s'\n\n", p); // parsing without error handlingprintf("\"1010\" in binary --> %ld\n", strtol("1010",NULL,2));printf("\"12\" in octal --> %ld\n", strtol("12",NULL,8));printf("\"A\" in hex --> %ld\n", strtol("A",NULL,16));printf("\"junk\" in base-36 --> %ld\n", strtol("junk",NULL,36));printf("\"012\" in auto-detected base --> %ld\n", strtol("012",NULL,0));printf("\"0xA\" in auto-detected base --> %ld\n", strtol("0xA",NULL,0));printf("\"junk\" in auto-detected base --> %ld\n", strtol("junk",NULL,0));}
Possible output:
Parsing '10 200000000000000000000000000000 30 -40 junk':Extracted '10', strtol returned 10.Extracted ' 200000000000000000000000000000', strtol returned 9223372036854775807. --> Range error occurred.Extracted ' 30', strtol returned 30.Extracted ' -40', strtol returned -40.Unextracted leftover: ' junk' "1010" in binary --> 10"12" in octal --> 10"A" in hex --> 10"junk" in base-36 --> 926192"012" in auto-detected base --> 10"0xA" in auto-detected base --> 10"junk" in auto-detected base --> 0
(C99) | converts a byte string to an integer value (function)[edit] |
(C99) | converts a byte string to an unsigned integer value (function)[edit] |
(C95)(C99) | converts a wide string to an integer value (function)[edit] |
(C95)(C99) | converts a wide string to an unsigned integer value (function)[edit] |
C++ documentation forstrtol,strtoll | |