atoi
頭文件<stdlib.h>
int atoi(const char *str);
用於轉譯str所指低字節字符歘那種整數值。捨棄任何空白符,直至找到首個非空白符,並接受儘可能所的字符以組成合法的整數表示,轉化爲整數值;
參數:str 指向要轉譯的空終止字符串的指針;
返回值:返回對應str內容的整數值。
若轉換的值落在對應返回類型範圍外,則返回值未定義。若無法進行轉換,則返回0;
示例
#include<stdio.h>
#include<stdlib.h>
int main(void){
printf("%i\n",atoi("-123jjjk");
printf("%i\n",atoi("0");
printf("%i\n",atoi("12341234");
printf("%i\n",atoi("123411234234"));//越界?
printf("%i\n",atoi("asdfasdfasd");
return 0;
}
-123
0
12341234
-1142817350
0
itoa
它是aoti逆函數,用於將數字轉換字符串
void itoa(int n,char s[]){
int i,sign;
sign = n;
i = 0;
if((sign = n) < 0)
n = -n;//將n轉換爲正數
do{
s[i++] = abs(n % 10) + '0';
} while(n /= 10);
if(sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
void reverse(char s[]){
int c,i,j;
for(i=0,j=strlen(s)-1;i<j;i++,j--){
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
feof
函數: 如果文件結束,則返回非0值,否則返回0,文件結束符只能被clearerr()清除。