|
atof(將字符串轉(zhuǎn)換成浮點(diǎn)型數(shù)) |
相關(guān)函數(shù) |
atoi,atol,strtod,strtol,strtoul
|
表頭文件 |
#include <stdlib.h>
|
定義函數(shù) |
double atof(const char *nptr);
|
函數(shù)說明 |
atof()會(huì)掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換,而再遇到非數(shù)字或字符串結(jié)束時(shí)('\0')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。參數(shù)nptr字符串可包含正負(fù)號(hào)、小數(shù)點(diǎn)或E(e)來表示指數(shù)部分,如123.456或123e-2。
|
返回值 |
返回轉(zhuǎn)換后的浮點(diǎn)型數(shù)。
|
附加說明 |
atof()與使用strtod(nptr,(char**)NULL)結(jié)果相同。
|
范例 |
/* 將字符串a(chǎn) 與字符串b轉(zhuǎn)換成數(shù)字后相加*/ #include<stdlib.h> main() { char *a=”-100.23”; char *b=”200e-2”; float c; c=atof(a)+atof(b); printf(“c=%.2f\n”,c); }
|
執(zhí)行 |
c=-98.23
|
|
|
atoi(將字符串轉(zhuǎn)換成整型數(shù)) |
相關(guān)函數(shù) |
atof,atol,atrtod,strtol,strtoul
|
表頭文件 |
#include<stdlib.h>
|
定義函數(shù) |
int atoi(const char *nptr);
|
函數(shù)說明 |
atoi()會(huì)掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換,而再遇到非數(shù)字或字符串結(jié)束時(shí)('\0')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。
|
返回值 |
返回轉(zhuǎn)換后的整型數(shù)。
|
附加說明 |
atoi()與使用strtol(nptr,(char**)NULL,10);結(jié)果相同。
|
范例 |
/* 將字符串a(chǎn) 與字符串b轉(zhuǎn)換成數(shù)字后相加*/ #include<stdlib.h> mian() { char a[]=”-100”; char b[]=”456”; int c; c=atoi(a)+atoi(b); printf(c=%d\n”,c); }
|
執(zhí)行 |
c=356
|
|
|
atol(將字符串轉(zhuǎn)換成長(zhǎng)整型數(shù)) |
相關(guān)函數(shù) |
atof,atoi,strtod,strtol,strtoul
|
表頭文件 |
#include<stdlib.h>
|
定義函數(shù) |
long atol(const char *nptr);
|
函數(shù)說明 |
atol()會(huì)掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換,而再遇到非數(shù)字或字符串結(jié)束時(shí)('\0')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。
|
返回值 |
返回轉(zhuǎn)換后的長(zhǎng)整型數(shù)。
|
附加說明 |
atol()與使用strtol(nptr,(char**)NULL,10);結(jié)果相同。
|
范例 |
/*將字符串a(chǎn)與字符串b轉(zhuǎn)換成數(shù)字后相加*/ #include<stdlib.h> main() { char a[]=”1000000000”; char b[]=” 234567890”; long c; c=atol(a)+atol(b); printf(“c=%d\n”,c); }
|
執(zhí)行 |
c=1234567890
|
|
|
gcvt(將浮點(diǎn)型數(shù)轉(zhuǎn)換為字符串,取四舍五入) |
相關(guān)函數(shù) |
ecvt,fcvt,sprintf
|
表頭文件 |
#include<stdlib.h>
|
定義函數(shù) |
char *gcvt(double number,size_t ndigits,char *buf);
|
函數(shù)說明 |
gcvt()用來將參數(shù)number轉(zhuǎn)換成ASCII碼字符串,
參數(shù)ndigits表示顯示的位數(shù)。gcvt()與ecvt()和fcvt()不同的地方在于,gcvt()所轉(zhuǎn)換后的字符串包含小數(shù)點(diǎn)或正負(fù)符號(hào)。若轉(zhuǎn)
換成功,轉(zhuǎn)換后的字符串會(huì)放在參數(shù)buf指針?biāo)傅目臻g。
|
返回值 |
返回一字符串指針,此地址即為buf指針。
|
附加說明 |
|
范例 |
#include<stdlib.h> main() { double a=123.45; double b=-1234.56; char *ptr; int decpt,sign; gcvt(a,5,ptr); printf(“a value=%s\n”,ptr); ptr=gcvt(b,6,ptr); printf(“b value=%s\n”,ptr); }
|
執(zhí)行 |
a value=123.45 b value=-1234.56
|
|
|
strtod(將字符串轉(zhuǎn)換成浮點(diǎn)數(shù)) |
相關(guān)函數(shù) |
atoi,atol,strtod,strtol,strtoul
|
表頭文件 |
#include<stdlib.h>
|
定義函數(shù) |
double strtod(const char *nptr,char **endptr);
|
函數(shù)說明 |
strtod()會(huì)掃描參數(shù)nptr字符串,跳過前面的空格字
符,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換,到出現(xiàn)非數(shù)字或字符串結(jié)束時(shí)('\0')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若endptr不為NULL,則會(huì)將遇到不
合條件而終止的nptr中的字符指針由endptr傳回。參數(shù)nptr字符串可包含正負(fù)號(hào)、小數(shù)點(diǎn)或E(e)來表示指數(shù)部分。如123.456或123e
-2。
|
返回值 |
返回轉(zhuǎn)換后的浮點(diǎn)型數(shù)。
|
附加說明 |
參考atof()。
|
范例 |
/*將字符串a(chǎn),b,c 分別采用10,2,16 進(jìn)制轉(zhuǎn)換成數(shù)字*/ #include<stdlib.h> mian() { char a[]=”1000000000”; char b[]=”1000000000”; char c[]=”ffff”; printf(“a=%d\n”,strtod(a,NULL,10)); printf(“b=%d\n”,strtod(b,NULL,2)); printf(“c=%d\n”,strtod(c,NULL,16)); }
|
執(zhí)行 |
a=1000000000 b=512 c=65535
|
|
|
strtol(將字符串轉(zhuǎn)換成長(zhǎng)整型數(shù)) |
相關(guān)函數(shù) |
atof,atoi,atol,strtod,strtoul
|
表頭文件 |
#include<stdlib.h>
|
定義函數(shù) |
long int strtol(const char *nptr,char **endptr,int base);
|
函數(shù)說明 |
strtol()會(huì)將參數(shù)nptr字符串根據(jù)參數(shù)base來轉(zhuǎn)換
成長(zhǎng)整型數(shù)。參數(shù)base范圍從2至36,或0。參數(shù)base代表采用的進(jìn)制方式,如base值為10則采用10進(jìn)制,若base值為16則采用16進(jìn)制
等。當(dāng)base值為0時(shí)則是采用10進(jìn)制做轉(zhuǎn)換,但遇到如'0x'前置字符則會(huì)使用16進(jìn)制做轉(zhuǎn)換。一開始strtol()會(huì)掃描參數(shù)nptr字符串,跳
過前面的空格字符,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換,再遇到非數(shù)字或字符串結(jié)束時(shí)('\0')結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若參數(shù)endptr不為
NULL,則會(huì)將遇到不合條件而終止的nptr中的字符指針由endptr返回。
|
返回值 |
返回轉(zhuǎn)換后的長(zhǎng)整型數(shù),否則返回ERANGE并將錯(cuò)誤代碼存入errno中。
|
附加說明 |
ERANGE指定的轉(zhuǎn)換字符串超出合法范圍。
|
范例 |
/* 將字符串a(chǎn),b,c 分別采用10,2,16進(jìn)制轉(zhuǎn)換成數(shù)字*/ #include<stdlib.h> main() { char a[]=”1000000000”; char b[]=”1000000000”; char c[]=”ffff”; printf(“a=%d\n”,strtol(a,NULL,10)); printf(“b=%d\n”,strtol(b,NULL,2)); printf(“c=%d\n”,strtol(c,NULL,16)); }
|
執(zhí)行 |
a=1000000000 b=512 c=65535
|
|
|
strtoul(將字符串轉(zhuǎn)換成無符號(hào)長(zhǎng)整型數(shù)) |
相關(guān)函數(shù) |
atof,atoi,atol,strtod,strtol
|
表頭文件 |
#include<stdlib.h>
|
定義函數(shù) |
unsigned long int strtoul(const char *nptr,char **endptr,int base);
|
函數(shù)說明 |
strtoul()會(huì)將參數(shù)nptr字符串根據(jù)參數(shù)base來轉(zhuǎn)
換成無符號(hào)的長(zhǎng)整型數(shù)。參數(shù)base范圍從2至36,或0。參數(shù)base代表采用的進(jìn)制方式,如base值為10則采用10進(jìn)制,若base值為16則采
用16進(jìn)制數(shù)等。當(dāng)base值為0時(shí)則是采用10進(jìn)制做轉(zhuǎn)換,但遇到如'0x'前置字符則會(huì)使用16進(jìn)制做轉(zhuǎn)換。一開始strtoul()會(huì)掃描參數(shù)
nptr字符串,跳過前面的空格字符串,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換,再遇到非數(shù)字或字符串結(jié)束時(shí)('\0')結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若參數(shù)
endptr不為NULL,則會(huì)將遇到不合條件而終止的nptr中的字符指針由endptr返回。
|
返回值 |
返回轉(zhuǎn)換后的長(zhǎng)整型數(shù),否則返回ERANGE并將錯(cuò)誤代碼存入errno中。
|
附加說明 |
ERANGE指定的轉(zhuǎn)換字符串超出合法范圍。
|
范例 |
參考strtol()
|
|
|
toascii(將整型數(shù)轉(zhuǎn)換成合法的ASCII 碼字符) |
相關(guān)函數(shù) |
isascii,toupper,tolower
|
表頭文件 |
#include<ctype.h>
|
定義函數(shù) |
int toascii(int c)
|
函數(shù)說明 |
toascii()會(huì)將參數(shù)c轉(zhuǎn)換成7位的unsigned char值,第八位則會(huì)被清除,此字符即會(huì)被轉(zhuǎn)成ASCII碼字符。
|
返回值 |
將轉(zhuǎn)換成功的ASCII碼字符值返回。
|
范例 |
#include<stdlib.h> main() { int a=217; char b; printf(“before toascii () : a value =%d(%c)\n”,a,a); b=toascii(a); printf(“after toascii() : a value =%d(%c)\n”,b,b); }
|
執(zhí)行 |
before toascii() : a value =217() after toascii() : a value =89(Y)
|
|
|
tolower(將大寫字母轉(zhuǎn)換成小寫字母) |
相關(guān)函數(shù) |
isalpha,toupper
|
表頭文件 |
#include<stdlib.h>
|
定義函數(shù) |
int tolower(int c);
|
函數(shù)說明 |
若參數(shù)c為大寫字母則將該對(duì)應(yīng)的小寫字母返回。
|
返回值 |
返回轉(zhuǎn)換后的小寫字母,若不須轉(zhuǎn)換則將參數(shù)c值返回。
|
附加說明 |
|
范例 |
/* 將s字符串內(nèi)的大寫字母轉(zhuǎn)換成小寫字母*/ #include<ctype.h> main() { char s[]=”aBcDeFgH12345;!#$”; int i; printf(“before tolower() : %s\n”,s); for(i=0;I<sizeof(s);i++) s[i]=tolower(s[i]); printf(“after tolower() : %s\n”,s); }
|
執(zhí)行 |
before tolower() : aBcDeFgH12345;!#$ after tolower() : abcdefgh12345;!#$
|
|
|
toupper(將小寫字母轉(zhuǎn)換成大寫字母) |
相關(guān)函數(shù) |
isalpha,tolower
|
表頭文件 |
#include<ctype.h>
|
定義函數(shù) |
int toupper(int c);
|
函數(shù)說明 |
若參數(shù)c為小寫字母則將該對(duì)映的大寫字母返回。
|
返回值 |
返回轉(zhuǎn)換后的大寫字母,若不須轉(zhuǎn)換則將參數(shù)c值返回。
|
附加說明 |
|
范例 |
/* 將s字符串內(nèi)的小寫字母轉(zhuǎn)換成大寫字母*/ #include<ctype.h> main() { char s[]=”aBcDeFgH12345;!#$”; int i; printf(“before toupper() : %s\n”,s); for(i=0;I<sizeof(s);i++) s[i]=toupper(s[i]); printf(“after toupper() : %s\n”,s); }
|
執(zhí)行 |
before toupper() : aBcDeFgH12345;!#$ after toupper() : ABCDEFGH12345;!#$ |