转载链接:
http://blog.chinaunix.net/u2/76292/showart_1845022.html
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <ctype.h>
4
5 long my_atol(const char *nptr)
6 {
7 int c; /* current char */
8 long total; /* current total */
9 int sign; /* if '-', then negative, otherwise positive */
10
11 /* skip whitespace */
12 while ( isspace((int)(unsigned char)*nptr) )
13 {
14 ++nptr;
15 }
16
17 c = (int)(unsigned char)*nptr++;
18 sign = c; /* save sign indication */
19 if (c == '-' || c == '+')
20 {
21 c = (int)(unsigned char)*nptr++; /* skip sign */
22 }
23
24 total = 0;
25
26 while (isdigit(c))
27 {
28 total = 10 * total + (c - '0'); /* accumulate digit */
29 c = (int)(unsigned char)*nptr++; /* get next char */
30 }
31
32 if (sign == '-')
33 {
34 return -total;
35 }
36 else
37 {
38 return total; /* return result, negated if necessary */
39 }
40 }
41
42 int main(int argc, char* argv[])
43 {
44 if( argc < 2 )
45 {
46 return -1;
47 }
48
49 int i = my_atol(argv[1]);
50 printf("[%s]====>[%d]\n", argv[1], i);
51
52 int j = atoi(argv[1]);
53 printf("[%s]====>[%d]\n", argv[1], j);
54
55 return 0;
56 }
57
http://www.cppblog.com/Files/bujiwu/atoi.rar