1 /* 2 *atoi() 3 * 4 * To convert a string to integer 5 * 6 */ 7 #include8 #include 9 #define IS_DIGIT(_arg_) ({char _ch_ = _arg_ ;(_ch_ >= '0'&&_ch_ <= '9');})10 int my_atoi(const char *str);11 12 int main(int argc, const char *argv[])13 {14 int value;15 value = my_atoi("-123456");16 printf("value:%d\n",value);17 18 return 0;19 }20 21 int my_atoi(const char *str)22 {23 int integer = 0;24 const char *p = str;25 int flag = 1;26 switch(*p)27 {28 case '+':29 p++;30 break;31 case '-':32 flag = -1;33 p++;34 break;35 default:36 break;37 }38 39 for(;'\0' != *p && IS_DIGIT(*p);p++)40 {41 integer *= 10;42 integer += *p - '0';43 }44 45 return flag > 0 ? integer : -integer;46 47 }