名前空間
変種
操作

論理演算子

From cppreference.com
< c‎ | language

論理演算子は、オペランドに標準的なブール代数の演算を適用します。

Operator 演算子名 結果
! 論理的否定 !a a の論理否定
&& 論理積 a && b ab の論理積
|| 論理和 a || b ab の論理和

目次

[編集] 論理的否定

論理否定式は次の形式をとります。

!

ここで、

- あらゆる スカラー型 の式

論理否定演算子の型は int です。その値は、 がゼロと等しくない値に評価される場合は 0、ゼロと等しい値に評価される場合は 1 となります。(したがって、!E(0==E) と同じです)

#include <stdbool.h>
#include <stdio.h>
#include <ctype.h>
int main(void)
{
    bool b = !(2+2 == 4); // not true
    printf("!(2+2==4) = %s\n", b ? "true" : "false");
 
    int n = isspace('a'); // non-zero if 'a' is a space, zero otherwise
    int x = !!n; // "bang-bang", common C idiom for mapping integers to [0,1]
                 // (all non-zero values become 1)
    char *a[2] = {"non-space", "space"};
    puts(a[x]); // now x can be safely used as an index to array of 2 strings
}

出力

!(2+2==4) = false
non-space

[編集] 論理積

論理積式は次の形式をとります。

lhs && rhs

ここで、

lhs - あらゆるスカラー型の式
rhs - あらゆるスカラー型の式。これは、lhs がゼロと等しくないと評価された場合にのみ評価されます。

論理積演算子の型は int で、lhsrhs の両方がゼロと等しくないと評価された場合は値 1 を持ちます。それ以外の場合(lhs または rhs のいずれか、または両方がゼロと等しいと評価された場合)は値 0 を持ちます。

lhs の評価後には シーケンスポイント があります。lhs の結果がゼロと等しいと評価された場合、rhs はまったく評価されません(いわゆる *短絡評価*)。

#include <stdbool.h>
#include <stdio.h>
int main(void)
{
    bool b = 2+2==4 && 2*2==4; // b == true
 
    1 > 2 && puts("this won't print");
 
    char *p = "abc";
    if(p && *p) // common C idiom: if p is not null
                // AND if p does not point at the end of the string
    {           // (note that thanks to short-circuit evaluation, this
                //  will not attempt to dereference a null pointer)
    // ...      // ... then do some string processing
    }
}

[編集] 論理和

論理和式は次の形式をとります。

lhs || rhs

ここで、

lhs - あらゆるスカラー型の式
rhs - あらゆるスカラー型の式。これは、lhs がゼロと等しいと評価された場合にのみ評価されます。

論理和演算子の型は int で、lhs または rhs のいずれかがゼロと等しくないと評価された場合は値 1 を持ちます。それ以外の場合(lhsrhs の両方がゼロと等しいと評価された場合)は値 0 を持ちます。

lhs の評価後には シーケンスポイント があります。lhs の結果がゼロと等しくないと評価された場合、rhs はまったく評価されません(いわゆる *短絡評価*)。

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
{
    bool b = 2+2 == 4 || 2+2 == 5; // true
    printf("true or false = %s\n", b ? "true" : "false");
 
    // logical OR can be used simialar to perl's "or die", as long as rhs has scalar type
    fopen("test.txt", "r") || printf("could not open test.txt: %s\n", strerror(errno));
}

実行結果の例

true or false = true
could not open test.txt: No such file or directory

[編集] 参考文献

  • C11標準 (ISO/IEC 9899:2011)
  • 6.5.3.3 単項算術演算子 (p: 89)
  • 6.5.13 論理積演算子 (p: 99)
  • 6.5.14 論理和演算子 (p: 99)
  • C99標準 (ISO/IEC 9899:1999)
  • 6.5.3.3 単項算術演算子 (p: 79)
  • 6.5.13 論理積演算子 (p: 89)
  • 6.5.14 論理和演算子 (p: 89)
  • C89/C90標準 (ISO/IEC 9899:1990)
  • 3.3.3.3 単項算術演算子
  • 3.3.13 論理積演算子
  • 3.3.14 論理和演算子

[編集] 関連項目

演算子の優先順位

共通の演算子
代入 インクリメント
デクリメント
算術 論理 比較 メンバ
アクセス
その他

a = b
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b

a(...)
a, b
(type) a
a ? b : c
sizeof


_Alignof
(C11 以降)
(C23まで)

alignof
(C23以降)

[編集] 関連

C++ ドキュメント論理演算子 の場合)
English 日本語 中文(简体) 中文(繁體)