2013年10月2日水曜日

開発環境

C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)の6章(条件式と制御文)、6.8(どこにでも代入できることによる副作用)、設問 6-1を解いてみる。

その他参考書籍

設問 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 コメント:

コメントを投稿