1// definition: long int strtol (const char* str, char** endptr, int base);
2
3/* strtol example */
4#include <stdio.h> /* printf */
5#include <stdlib.h> /* strtol */
6
7int main ()
8{
9 char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
10 char * pEnd;
11 long int li1, li2, li3, li4;
12 li1 = strtol (szNumbers,&pEnd,10);
13 li2 = strtol (pEnd,&pEnd,16);
14 li3 = strtol (pEnd,&pEnd,2);
15 li4 = strtol (pEnd,NULL,0);
16 printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4);
17 return 0;
18}
1#include <stdio.h>
2#include <stdlib.h>
3
4int main () {
5 char str[30] = "2030300 This is test";
6 char *ptr;
7 long ret;
8
9 ret = strtol(str, &ptr, 10);
10 printf("The number(unsigned long integer) is %ld\n", ret);
11 printf("String part is |%s|", ptr);
12
13 return(0);
14}
1# **strtol** | string to long integer | c library function
2# EX.
3# include <stdlib.h> // the library required to use strtol
4
5int main() {
6 const char str[25] = " 2020 was garbage.";
7 char *endptr;
8 int base = 10;
9 long answer;
10
11 answer = strtol(str, &endptr, base);
12 printf("The converted long integer is %ld\n", answer);
13 return 0;
14}
15
16# ignores any whitespace at the beginning of the string and
17# converts the next characters into a long integer.
18# Stops when it comes across the first non-integer character.
19
20long strtol(const char *str, char **endptr, int base)
21
22# str − string to be converted.
23
24# endptr − reference to an object of type char*, whose value is set
25# to the next character in str after the numerical value.
26
27# base − This is the base, which must be between 2 and 36 inclusive,
28# or be the special value 0.