開発環境
- OS X Lion - Apple (UNIX) (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)の6章(条件式と制御文)、6.8(どこにでも代入できることによる副作用)、設問 6-1を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
設問 6-1.
ifステートメントの条件式で「=」ではなく「==」に修正すればいい。
コード
sample.c
#include <stdio.h> char line[80]; int balance_owed; int main(int argc, char *argv[]) { printf("Enter number of dollars owed: "); fgets(line, sizeof(line), stdin); sscanf(line, "%d", &balance_owed); if (balance_owed == 0) { printf("You owe nothing.\n"); } else { printf("You owe %d dollars.\n", balance_owed); } return (0); }
入出力結果(Terminal)
$ cc -g -o sample sample.c $ ./sample Enter number of dollars owed: 12 You owe 12 dollars. $ ./sample Enter number of dollars owed: 0 You owe nothing. $
0 コメント:
コメントを投稿