函數(shù)strtod,strtol
|
strtod(將字符串轉(zhuǎn)換成浮點數(shù)) |
|
|
相關(guān)函數(shù) |
atoi,atol,strtod,strtol,strtoul |
|
表頭文件 |
#include<stdlib.h> |
|
定義函數(shù) |
double strtod(const char *nptr,char **endptr); |
|
函數(shù)說明 |
strtod()會掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負(fù)符號才開始做轉(zhuǎn)換,到出現(xiàn)非數(shù)字或字符串結(jié)束時('\0')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若endptr不為NULL,則會將遇到不合條件而終止的nptr中的字符指針由endptr傳回。參數(shù)nptr字符串可包含正負(fù)號、小數(shù)點或E(e)來表示指數(shù)部分。如123.456或123e-2。 |
|
返回值 |
返回轉(zhuǎn)換后的浮點型數(shù)。 |
|
附加說明 |
參考atof()。 |
|
范例 |
/*將字符串a,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)換成長整型數(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()會將參數(shù)nptr字符串根據(jù)參數(shù)base來轉(zhuǎn)換成長整型數(shù)。參數(shù)base范圍從2至36,或0。參數(shù)base代表采用的進(jìn)制方式,如base值為10則采用10進(jìn)制,若base值為16則采用16進(jìn)制等。當(dāng)base值為0時則是采用10進(jìn)制做轉(zhuǎn)換,但遇到如'0x'前置字符則會使用16進(jìn)制做轉(zhuǎn)換。一開始strtol()會掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)字或正負(fù)符號才開始做轉(zhuǎn)換,再遇到非數(shù)字或字符串結(jié)束時('\0')結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若參數(shù)endptr不為NULL,則會將遇到不合條件而終止的nptr中的字符指針由endptr返回。 |
|
返回值 |
返回轉(zhuǎn)換后的長整型數(shù),否則返回ERANGE并將錯誤代碼存入errno中。 |
|
附加說明 |
ERANGE指定的轉(zhuǎn)換字符串超出合法范圍。 |
|
范例 |
/* 將字符串a,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 |
|