2013年10月31日木曜日

開発環境

C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅡ部(単純なプログラミング)の10章(Cプリプロセッサ)、10.1(#define文)、設問 10-1を解いてみる。

その他参考書籍

設問 10-1.

問題のコードでは、printfでの展開が以下のようになるので47と回答してしまう。

FIRST_PART + LAST_PART * FIRST_PART + LAST_PART
7 + 5 * 7 + 5 (= 47)

144と回答するように丸括弧を使って修正。

コード

sample.c

#include <stdio.h>

#define FIRST_PART 7
#define LAST_PART 5
#define ALL_PARTS (FIRST_PART + LAST_PART)

int main()
{
    printf("The square of all the parts is %d\n", ALL_PARTS * ALL_PARTS);
    return (0);
}

makefile

CC=cc
CFLAGS=-g

sample: sample.c
 $(CC) $(CFLAGS) -o sample sample.c

clean:
 rm -f sample

入出力結果(Terminal)

$ make
cc -g -o sample sample.c
$ ./sample
The square of all the parts is 144
$

0 コメント:

コメントを投稿