C言語のdouble型について
auです。
今回は、int型やchar型などと同じデータ型の"double型"について調べました。
double型とは
double型のサイズは8バイトです。
#include <stdio.h> int main(void) { printf("double型: %d\n", sizeof(double)); printf("int型: %d\n", sizeof(int)); printf("char型: %d\n", sizeof(char)); return 0; } // 実行結果 double型: 8 int型: 4 char型: 1
最大値1.797693e+308
最小値は2.225074e-308
"printf"などで表記する場合の変換指定子は"%lf"(long float)になります。
#include <stdio.h> int main(void) { double d = 1234567; printf("%lf\n", d); return 0; } // 実行結果 1234567.000000
int型の変換指定子"%d"でやってしまった場合
#include <stdio.h> int main(void) { double d = 1234567; printf("%d\n", d); return 0; } // gccコンパイル時 practice16.c:5:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=] printf("%d\n", d); ^ // 実行結果 -972293288
よくわからない数字がでてきます。