function
<cstdlib>
strtollong int strtol (const char* str, char** endptr, int base);
Convert string to long integer
Parses the C-string str interpreting its content as an integral number of the specified base, which is returned as along int
value. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax that depends on the base parameter, and interprets them as a numerical value. Finally, a pointer to the first character following the integer representation in str is stored in the object pointed by endptr.
If the value of base is zero, the syntax expected is similar to that of integer constants, which is formed by a succession of:
+
or -
)"0"
or "0x"/"0X"
respectively)'0'
and up to 'z'
/'Z'
for radix 36). The sequence may optionally be preceded by a sign (either +
or -
) and, if base is 16, an optional "0x"
or "0X"
prefix.
If the first sequence of non-whitespace characters in str is not a valid integral number as defined above, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
For locales other than the "C"
locale, additional subject sequence forms may be accepted.
char*
, whose value is set by the function to the next character in str after the numerical value.
0
, the base used is determined by the format in the sequence (see above).
long int
value.
0L
).
long int
, the function returns LONG_MAX or LONG_MIN (defined in <climits>), and errno is set to ERANGE.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* strtol example */
#include <stdio.h> /* printf */
#include <stdlib.h> /* strtol */
int main ()
{
char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
char * pEnd;
long int li1, li2, li3, li4;
li1 = strtol (szNumbers,&pEnd,10);
li2 = strtol (pEnd,&pEnd,16);
li3 = strtol (pEnd,&pEnd,2);
li4 = strtol (pEnd,NULL,0);
printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4);
return 0;
}
The decimal equivalents are: 2001, 6340800, -3624224 and 7340031
If str does not point to a valid C-string, or if endptr does not point to a valid pointer object, it causes undefined behavior.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4