生活随笔
收集整理的這篇文章主要介紹了
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简析的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。