2012年6月26日火曜日

開発環境

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

設問10-2.

MAXの定義で余分な=があって値が10ではなく=10となり、forループで変数counterを10で初期化するのではなく、値を設定していない変数counterとcounter ==10と評価することになってしまうから。

修正

コード(TextWrangler)

#include <stdio.h>

#define MAX 10

int main()
{
  int counter;
  
  for(counter =MAX; counter > 0; --counter){
    printf("Hi there\n");
  }
  return (0);
}

入出力結果(Terminal)

$ cc -g -o sample sample.c
$ ./sample
Hi there
Hi there
Hi there
Hi there
Hi there
Hi there
Hi there
Hi there
Hi there
Hi there
$

0 コメント:

コメントを投稿