-
atof
鎖定
atof()是C 語言標準庫中的一個字符串處理函數,功能是把字符串轉換成浮點數,所使用的頭文件為。該函數名是 “ascii to floating point numbers” 的縮寫。語法格式為:double atof(const char *nptr)。
- 外文名
- atof()
- 釋 義
- 函數名
- 功 能
- 把字符串轉換成浮點數
- 環 境
- 語言標準庫
- 語法格式
- double atof(const char *nptr)
- 全 稱
- ascii to floating point numbers
atof程序舉例
atof程序示例一
#include<stdlib.h> #include<stdio.h> int main() { double d; char str[] = "123.456"; d=atof(str); printf("string=%sdouble=%lf\n",str,d); return 0; }
atof程序示例二
編寫一個程序,該程序接收浮點數,用atof將其轉換為內部表示形式,之後打印該值。
[1]
#include <stdio.h> #include <math.h> main(int argc, char **argv) { double value; if(arge < 2) { prinf("Usage: %s <ualue>\n", argv[o]); } else { value=atof(argv[1]); printf("Value entered=%g\n", value); } }
atof基本介紹
atof的功能:(將字串轉換成浮點型數)
所屬庫名:<math.h> and <stdlib.h>
頭文件: #include <stdlib.h>
定義函數:double atof(const char *nptr);
函數説明:atof()會掃描參數nptr字符串,跳過前面的空格字符,直到遇上數字或正負符號才開始做轉換,而再遇到非數字或字符串結束時('\0')才結束轉換,並將結果返回。參數nptr字符串可包含正負號、小數點或E(e)來表示指數部分,如123.456或123e-2。
[2]
返回值:返回轉換後的浮點型數。
附加説明:atof()與使用strtod(nptr,(char**)NULL)結果相同。
範例:將字符串a 與字符串b轉換成數字後相加。
#include<stdlib.h> int main() { char*a="-100.23"; char*b="200e-2"; double c; c=atof(a)+atof(b); printf(“c=%.2lf\n”,c); return 0; }
結果為 c=-98.23