atoi 與 itoa 函式的內部實現

2021-05-27 07:14:33 字數 678 閱讀 5352

c語言提供了幾個標準庫函式,可以將任意型別(整型、長整型、浮點型等)的數字轉換為字串。以下是用itoa()函式將整數轉 換為字串的乙個例子:

atoi     把字串轉換成整型數

itoa     把一整數轉換為字串

#include "stdio.h"

#include "ctype.h"

#include "stdlib.h"

/*converts a character string into an int or long

將乙個字串轉化為整數

*/int my_atoi(char s)

/*converts an int or long into a character string

將乙個整數轉化為字串

*/void my_itoa(int n,char s)

while((n/=10)>0); //迴圈相除

if(sign<0)

s[i++]='-';

s[i]='\0';

for(j=i-1;j>=0;j--) //生成的數字是逆序的,所以要逆序輸出

printf("%c",s[j]);

}void main()

atoi 與 itoa 函式的內部實現

c語言提供了幾個標準庫函式,可以將任意型別 整型 長整型 浮點型等 的數字轉換為字串。以下是用itoa 函式將整數轉 換為字串的乙個例子 atoi 把字串轉換成整型數 itoa 把一整數轉換為字串 cpp view plain copy include stdio.h include ctype.h...

itoa 函式與atoi 函式

目錄 1 itoa 函式 整型轉字元 2 atoi 函式 字元轉整型 以下是用itoa 函式將整數轉換為字串的乙個例子 include include void main void itoa 函式有3個引數 第乙個引數是要轉換的數字,第二個引數是要寫入轉換結果的目標字串,第三個引數是轉移數字時所用 ...

atoi 與 itoa 函式用法

itoa 函式的原型為 char itoa int value,char string,int radix itoa 函式有3個引數 第乙個引數是要轉換的數字,第二個引數是要寫入轉換結果的目標字串,第三個引數是轉換數字時所用的基數。在例中,轉換基數為10。10 十進位制 2 二進位制.itoa並不是...