当前位置: 代码迷 >> 综合 >> 字符串转整型(C语言)
  详细解决方案

字符串转整型(C语言)

热度:67   发布时间:2024-01-16 21:07:54.0

字符串转整型(C语言)

second60 20180529

#include <stdio.h>
int str2int(const char* str)
{int temp = 0;const char* p = str;if(str == NULL) return 0;if(*str == '-' || *str == '+'){str ++;}while( *str != 0){if( *str < '0' || *str > '9'){break;}temp = temp*10 +(*str -'0');str ++;}if(*p == '-'){temp = -temp;}return temp;
}

int main(int argc,char **argv)
{int n = 0;char str[7] = "-12345";n = str2int(str);printf("n=%d\n",n);getchar();return 1;
}
  相关解决方案