C examples for stdlib.h:strtol
function
<cstdlib> <stdlib.h>
Convert string to long integer of the specified base to a long int value.
long int strtol (const char* str, char** endptr, int base);
Parameter | Description |
---|---|
str | C-string representation of an integral number. |
endptr | type char* set by the function to the next character in str after the numerical value. |
base | Numerical base (radix) |
On success, the function returns the converted integral number as a long int value.
For non valid conversion, a zero value is returned (0L).
For out of the range values, the function returns LONG_MAX or LONG_MIN (defined in <climits>), and errno is set to ERANGE.
#include <stdio.h> #include <stdlib.h> int main ()// www. j ava2s . co m { char str[] = "2020 60c0c0 -1010101010101010101010 0x6f1234"; char * pEnd; long int li1, li2, li3, li4; li1 = strtol (str,&pEnd,10); li2 = strtol (pEnd,&pEnd,16); li3 = strtol (pEnd,&pEnd,2); li4 = strtol (pEnd,NULL,0); printf ("%ld, %ld, %ld %ld.\n", li1, li2, li3, li4); return 0; }