生活随笔
收集整理的这篇文章主要介绍了
atoi简析
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
原文链接
atoi()函数的功能:将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回(返回转换后的整型数)。
atoi()函数实现的代码:
int my_atoi(char* pstr) { int Ret_Integer = 0; int Integer_sign = 1; if(pstr == NULL) { printf("Pointer is NULL\n"); return 0; } while(isspace(*pstr) == 0) { pstr++; } if(*pstr == '-') { Integer_sign = -1; } if(*pstr == '-' || *pstr == '+') { pstr++; } while(*pstr >= '0' && *pstr <= '9') { Ret_Integer = Ret_Integer * 10 + *pstr - '0'; pstr++; } Ret_Integer = Integer_sign * Ret_Integer; return Ret_Integer; } 现在贴出运行my_atoi()的结果,定义的主函数为:int main ()
int main() { char a[] = "-100"; char b[] = "456"; int c = 0; int my_atoi(char*); c = atoi(a) + atoi(b); printf("atoi(a)=%d\n",atoi(a)); printf("atoi(b)=%d\n",atoi(b)); printf("c = %d\n",c); return 0; } 运行结果:
转载于:https://www.cnblogs.com/liangliangdetianxia/p/4165037.html
总结
以上是生活随笔为你收集整理的atoi简析的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。