看了下楼主的《剑指Offer——名企面试官精讲典型编程题》
第7章,第一个实例简直就是面试官装B的一个实例。
说什么atoi 返回0的时候也可能是错误,会设置一个全局变量。
根本就是瞎扯。或者说最近的libc里面根本没有提。害的我找了一圈
###############man 3 atoi :############
DESCRIPTION
The atoi() function converts the initial portion of the string
pointed to by nptr to int. The behaviour is the same as
strtol(nptr, (char **)NULL, 10);
except that atoi() does not detect errors.
###############atoi.c###################
00001 /*
00002 * This file is shared between libc and the kernel, so don't put
anything
00003 * in here that won't work in both contexts.
00004 */
00005
00006 #ifdef _KERNEL
00007 #include
00008 #include
00009 #else
00010 #include
00011 #include
00012 #endif
00013
00014 /*
00015 * Standard C function: parse a string that represents a decimal
integer.
00016 * Leading whitespace is allowed. Trailing gunk is allowed too. Doesn'
t
00017 * really report syntax errors in any useful way.
00018 */
00019
00020 int
00021 atoi(const char *s)
00022 {
00023 static const char digits[] = "0123456789"; /* legal digits in
order */
00024 unsigned val=0; /* value we're accumulating */
00025 int neg=0; /* set to true if we see a minus sign
*/
00026
00027 /* skip whitespace */
00028 while (*s==' ' || *s=='\t') {
00029 s++;
00030 }
00031
00032 /* check for sign */
00033 if (*s=='-') {
00034 neg=1;
00035 s++;
00036 }
00037 else if (*s=='+') {
00038 s++;
00039 }
00040
00041 /* process each digit */
00042 while (*s) {
00043 const char *where;
00044 unsigned digit;
00045
00046 /* look for the digit in the list of digits */
00047 where = strchr(digits, *s);
00048 if (where==NULL) {
00049 /* not found; not a digit, so stop */
00050 break;
00051 }
00052
00053 /* get the index into the digit list, which is the
value */
00054 digit = (where - digits);
00055
00056 /* could (should?) check for overflow here */
00057
00058 /* shift the number over and add in the new digit */
00059 val = val*10 + digit;
00060
00061 /* look at the next character */
00062 s++;
00063 }
00064
00065 /* handle negative numbers */
00066 if (neg) {
00067 return -val;
00068 }
00069
00070 /* done */
00071 return val;
00072 }