開発環境
- OS X Lion - Apple (UNIX) (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)の4章(基本的な宣言および式)、4.10(浮動小数点数型の除算と整数型の除算)、設問4-4を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
設問 4-4.
resultは浮動小数点数型だけど、printfの第1引数のフォーマット文字列で整数型の%dになっているから。浮動小数点数型の%f変換に修正すればいい。
コード
sample.c
#include <stdio.h> float result; int main(int argc, char *argv[]) { result = 7.0 / 22.0; printf("The result is %d\n", result); printf("The result is %f\n", result); return (0); }
入出力結果(Terminal)
$ cc -g -o sample sample.c sample.c:8:28: warning: format specifies type 'int' but the argument has type 'double' [-Wformat] printf("The result is %d\n", result); ~^ ~~~~~~ %f 1 warning generated. $ ./sample The result is 1852033808 The result is 0.318182 $
0 コメント:
コメントを投稿