标签:带符号字符串转换成整数
/*编写一个函数,将一个数字字符串转换成该字符串对应的数字(包括正整数、负整数) 例如:"12" 返回12 "-123" 返回-123 函数原型:int my_atoi(char *str)*/ #include <stdio.h> int my_atoi(char const *str) { int sum = 0; int p; int n = 0; if( *str == '-' ) { n = 1; str++; } while( *str != '\0' ) { if( *( str ) >= '0' && *( str ) <= '9' ) { p = ( int )( *str ) - 48; sum = sum * 10 + p; str++; } } if( n == 1 ) { sum = - sum; } return sum; } int main() { char *p = "-123"; printf("转换后为:%d\n",my_atoi(p)); return 0; } <img src="http://img.blog.csdn.net/20150402125124836?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><img src="http://img.blog.csdn.net/20150402125224316?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
【c语言】编写一个函数,将一个数字字符串转换成该字符串对应的数字(包括正整数、负整数)
标签:带符号字符串转换成整数
原文地址:http://blog.csdn.net/zhaoyaqian552/article/details/44830635