From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/language%2Foperator_logical.html | 111 +++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 devdocs/c/language%2Foperator_logical.html (limited to 'devdocs/c/language%2Foperator_logical.html') diff --git a/devdocs/c/language%2Foperator_logical.html b/devdocs/c/language%2Foperator_logical.html new file mode 100644 index 00000000..bdc70465 --- /dev/null +++ b/devdocs/c/language%2Foperator_logical.html @@ -0,0 +1,111 @@ +

Logical operators

Logical operators apply standard boolean algebra operations to their operands.

+ + + + + +
Operator Operator name Example Result
! logical NOT !a the logical negation of a
&& logical AND a && b the logical AND of a and b
|| logical OR a || b the logical OR of a and b

Logical NOT

The logical NOT expression has the form

+ + +
! expression

where

+ + +
expression - an expression of any scalar type

The logical NOT operator has type int. Its value is ​0​ if expression evaluates to a value that compares unequal to zero. Its value is 1 if expression evaluates to a value that compares equal to zero. (so !E is the same as (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'); // zero if 'a' is a space, nonzero otherwise
+    int x = !!n; // "bang-bang", common C idiom for mapping integers to [0,1]
+                 // (all non-zero values become 1)
+    char *a[2] = {"nonspace", "space"};
+    printf("%s\n", a[x]); // now x can be safely used as an index to array of 2 ints
+}

Output:

+
!(2+2==4) = false
+nonspace

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 if lhs does not compare equal to ​0​

The logical-AND operator has type int and the value 1 if both lhs and rhs compare unequal to zero. It has the value ​0​ otherwise (if either lhs or rhs or both compare equal to zero).

+

There is a sequence point after the evaluation of lhs. If the result of lhs compares equal to zero, then rhs is not evaluated at all (so-called short-circuit evaluation)

+
#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
+    }
+}

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 if lhs compares equal to ​0​

The logical-OR operator has type int and the value 1 if either lhs or rhs compare unequal to zero. It has value ​0​ otherwise (if both lhs and rhs compare equal to zero).

+

There is a sequence point after the evaluation of lhs. If the result of lhs compares unequal to zero, then rhs is not evaluated at all (so-called short-circuit evaluation)

+
#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));
+}

Possible output:

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

References

See Also

Operator precedence

+ + + +
Common operators
assignment increment
decrement
arithmetic logical comparison member
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)

+

See also

+
C++ documentation for Logical operators
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/language/operator_logical +

+
-- cgit v1.2.3