Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Logical operators

      From cppreference.com
      <c‎ |language
       
       
       
       

      Logical operators apply standard boolean algebra operations to their operands.

      Operator Operator name Example Result
      ! logical NOT!a the logical negation ofa
      && logical ANDa&& b the logical AND ofa andb
      || logical ORa|| b the logical OR ofa andb

      Contents

      [edit]Logical NOT

      The logical NOT expression has the form

      !expression

      where

      expression - an expression of anyscalar type

      The logical NOT operator has typeint. Its value is0 ifexpression evaluates to a value that compares unequal to zero. Its value is1 ifexpression evaluates to a value that compares equal to zero. (so!E is the same as(0==E))

      Run this code
      #include <stdbool.h>#include <stdio.h>#include <ctype.h>int main(void){    bool b=!(2+2==4);// not trueprintf("!(2+2==4) = %s\n", b?"true":"false"); int n=isspace('a');// non-zero if 'a' is a space, zero otherwiseint 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}

      Output:

      !(2+2==4) = falsenon-space

      [edit]Logical AND

      The logical AND expression has the form

      lhs&&rhs

      where

      lhs - an expression of any scalar type
      rhs - an expression of any scalar type, which is only evaluated iflhs does not compare equal to0

      The logical-AND operator has typeint and the value1 if bothlhs andrhs compare unequal to zero. It has the value0 otherwise (if eitherlhs orrhs or both compare equal to zero).

      There is asequence point after the evaluation oflhs. If the result oflhs compares equal to zero, thenrhs is not evaluated at all (so-calledshort-circuit evaluation)

      Run this code
      #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}}

      [edit]Logical OR

      The logical OR expression has the form

      lhs||rhs

      where

      lhs - an expression of any scalar type
      rhs - an expression of any scalar type, which is only evaluated iflhs compares equal to0

      The logical-OR operator has typeint and the value1 if eitherlhs orrhs compare unequal to zero. It has value0 otherwise (if bothlhs andrhs compare equal to zero).

      There is asequence point after the evaluation oflhs. If the result oflhs compares unequal to zero, thenrhs is not evaluated at all (so-calledshort-circuit evaluation)

      Run this code
      #include <stdbool.h>#include <stdio.h>#include <string.h>#include <errno.h>int main(void){    bool b=2+2==4||2+2==5;// trueprintf("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 typefopen("test.txt","r")||printf("could not open test.txt: %s\n",strerror(errno));}

      Possible output:

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

      [edit]References

      • C11 standard (ISO/IEC 9899:2011):
      • 6.5.3.3 Unary arithmetic operators (p: 89)
      • 6.5.13 Logical AND operator (p: 99)
      • 6.5.14 Logical OR operator (p: 99)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.5.3.3 Unary arithmetic operators (p: 79)
      • 6.5.13 Logical AND operator (p: 89)
      • 6.5.14 Logical OR operator (p: 89)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.3.3.3 Unary arithmetic operators
      • 3.3.13 Logical AND operator
      • 3.3.14 Logical OR operator

      [edit]See Also

      Operator precedence

      Common operators
      assignmentincrement
      decrement
      arithmeticlogicalcomparisonmember
      access
      other

      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
      (since C11)
      (until C23)

      alignof
      (since C23)

      [edit]See also

      C++ documentation forLogical operators
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/operator_logical&oldid=171168"

      [8]ページ先頭

      ©2009-2025 Movatter.jp