😸

[C] atof 関数

に公開

機能

文字列を浮動小数点数(double型)に変換する関数[1]

使用例

123.45 と表示する

\n は改行を表す エスケープシーケンス[2]

コード

printf("%f\n", atof("123.45"));
全文
#include <stdio.h>
#include <stdlib.h>
void main() {
    printf("%f\n", atof("123.45"));
}

実行結果

123.450000

文字列変数 "0.789" を変換して表示する

コード

char str[] = "0.789";
printf("%f\n", atof(str));
全文
#include <stdio.h>
#include <stdlib.h>
void main() {
    char str[] = "0.789";
    printf("%f\n", atof(str));
}

実行結果

0.789000
脚注
  1. 文字列("12.34"など)を、プログラム内で計算できるdouble型値に変換する。 ↩︎

  2. 改行やタブなど、画面に表示されない制御文字のこと。 ↩︎

Discussion