生活随笔
收集整理的這篇文章主要介紹了
atoi简析
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文鏈接
atoi()函數(shù)的功能:將字符串轉(zhuǎn)換成整型數(shù);atoi()會(huì)掃描參數(shù)nptr字符串,跳過(guò)前面的空格字符,直到遇上數(shù)字或正負(fù)號(hào)才開(kāi)始做轉(zhuǎn)換,而再遇到非數(shù)字或字符串時(shí)('\0')才結(jié)束轉(zhuǎn)化,并將結(jié)果返回(返回轉(zhuǎn)換后的整型數(shù))。
??? atoi()函數(shù)實(shí)現(xiàn)的代碼:
??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;??}?? ????現(xiàn)在貼出運(yùn)行my_atoi()的結(jié)果,定義的主函數(shù)為: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;??} ? ??? 運(yùn)行結(jié)果:
轉(zhuǎn)載于:https://www.cnblogs.com/liangliangdetianxia/p/4165037.html
總結(jié)
以上是生活随笔為你收集整理的atoi简析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。