2014年2月5日水曜日

開発環境

独習C 第4版(ハーバート・シルト (著)、 柏原 正三 (監修)トップスタジオ (翻訳) 、翔泳社)の第2章(制御文 - その1)、2.7(関係演算子と論理演算子)、2.7(練習問題), 3.を解いてみる。

その他参考書籍

2.7(練習問題), 3.

sample.c

#include <stdio.h>

int main(int argc, char *argv[])
{
    /* 2つの式の評価結果は異なる。 */
    /* aは真 */
    printf("a: ");
    if (0 && 1 || 1) {
        printf("真\n");
    } else {
        printf("偽\n");
    }
    /* aの式は(0 && 1) || 1と同じ */
    if ((0 && 1) || 1) {
        printf("真\n");
    } else {
        printf("偽\n");
    }
    
    /* bは偽 */
    printf("b: ");
    if (0 && (1 || 1)) {
        printf("真\n");
    } else {
        printf("偽\n");
    }
    
    return (0);
}

入出力結果(Terminal)

$ cc -g -o sample sample.c && ./sample
sample.c:8:11: warning: '&&' within '||' [-Wlogical-op-parentheses]
    if (0 && 1 || 1) {
        ~~^~~~ ~~
sample.c:8:11: note: place parentheses around the '&&' expression to silence
      this warning
    if (0 && 1 || 1) {
          ^
        (     )
1 warning generated.
a: 真
真
b: 偽
$

0 コメント:

コメントを投稿