diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/language%2Foperator_logical.html | |
new repository
Diffstat (limited to 'devdocs/c/language%2Foperator_logical.html')
| -rw-r--r-- | devdocs/c/language%2Foperator_logical.html | 111 |
1 files changed, 111 insertions, 0 deletions
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 @@ + <h1 id="firstHeading" class="firstHeading">Logical operators</h1> <p>Logical operators apply standard boolean algebra operations to their operands.</p> +<table class="wikitable"> <tr style="text-align:center"> <th> Operator </th> <th> Operator name </th> <th> Example </th> <th> Result </th> +</tr> <tr style="text-align:center"> <td> <code>!</code> </td> <td> logical NOT </td> <td> <code>!a</code> </td> <td> the logical negation of <b>a</b> </td> +</tr> <tr style="text-align:center"> <td> <code>&&</code> </td> <td> logical AND </td> <td> <code>a && b</code> </td> <td> the logical AND of <b>a</b> and <b>b</b> </td> +</tr> <tr style="text-align:center"> <td> <code>||</code> </td> <td> logical OR </td> <td> <code>a || b</code> </td> <td> the logical OR of <b>a</b> and <b>b</b> </td> +</tr> +</table> <h3 id="Logical_NOT"> Logical NOT</h3> <p>The logical NOT expression has the form</p> +<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <code>!</code> <span class="t-spar">expression</span> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td> +</tr> +</table> <p>where</p> +<table class="t-par-begin"> <tr class="t-par"> <td> <span class="t-spar">expression</span> </td> <td> - </td> <td> an expression of any <a href="type#Type_groups" title="c/language/type">scalar type</a> </td> +</tr> +</table> <p>The logical NOT operator has type <code>int</code>. Its value is <code>0</code> if <span class="t-spar">expression</span> evaluates to a value that compares unequal to zero. Its value is <code>1</code> if <span class="t-spar">expression</span> evaluates to a value that compares equal to zero. (so <code>!E</code> is the same as <code>(0==E)</code>)</p> +<div class="t-example"> <div class="c source-c"><pre data-language="c">#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 +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">!(2+2==4) = false +nonspace</pre></div> </div> <h3 id="Logical_AND"> Logical AND</h3> <p>The logical AND expression has the form</p> +<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <span class="t-spar">lhs</span> <code>&&</code> <span class="t-spar">rhs</span> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td> +</tr> +</table> <p>where</p> +<table class="t-par-begin"> <tr class="t-par"> <td> <span class="t-spar">lhs</span> </td> <td> - </td> <td> an expression of any scalar type </td> +</tr> <tr class="t-par"> <td> <span class="t-spar">rhs</span> </td> <td> - </td> <td> an expression of any scalar type, which is only evaluated if <span class="t-spar">lhs</span> does not compare equal to <code>0</code> </td> +</tr> +</table> <p>The logical-AND operator has type <code>int</code> and the value <code>1</code> if both <span class="t-spar">lhs</span> and <span class="t-spar">rhs</span> compare unequal to zero. It has the value <code>0</code> otherwise (if either <span class="t-spar">lhs</span> or <span class="t-spar">rhs</span> or both compare equal to zero).</p> +<p>There is a <a href="eval_order" title="c/language/eval order">sequence point</a> after the evaluation of <span class="t-spar">lhs</span>. If the result of <span class="t-spar">lhs</span> compares equal to zero, then <span class="t-spar">rhs</span> is not evaluated at all (so-called <i>short-circuit evaluation</i>)</p> +<div class="t-example"> <div class="c source-c"><pre data-language="c">#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 + } +}</pre></div> </div> <h3 id="Logical_OR"> Logical OR</h3> <p>The logical OR expression has the form</p> +<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <span class="t-spar">lhs</span> <code>||</code> <span class="t-spar">rhs</span> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td> +</tr> +</table> <p>where</p> +<table class="t-par-begin"> <tr class="t-par"> <td> <span class="t-spar">lhs</span> </td> <td> - </td> <td> an expression of any scalar type </td> +</tr> <tr class="t-par"> <td> <span class="t-spar">rhs</span> </td> <td> - </td> <td> an expression of any scalar type, which is only evaluated if <span class="t-spar">lhs</span> compares equal to <code>0</code> </td> +</tr> +</table> <p>The logical-OR operator has type <code>int</code> and the value <code>1</code> if either <span class="t-spar">lhs</span> or <span class="t-spar">rhs</span> compare unequal to zero. It has value <code>0</code> otherwise (if both <span class="t-spar">lhs</span> and <span class="t-spar">rhs</span> compare equal to zero).</p> +<p>There is a <a href="eval_order" title="c/language/eval order">sequence point</a> after the evaluation of <span class="t-spar">lhs</span>. If the result of <span class="t-spar">lhs</span> compares unequal to zero, then <span class="t-spar">rhs</span> is not evaluated at all (so-called <i>short-circuit evaluation</i>)</p> +<div class="t-example"> <div class="c source-c"><pre data-language="c">#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)); +}</pre></div> <p>Possible output:</p> +<div class="text source-text"><pre data-language="c">true or false = true +could not open test.txt: No such file or directory</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 6.5.3.3 Unary arithmetic operators (p: 89) </li> +<li> 6.5.13 Logical AND operator (p: 99) </li> +<li> 6.5.14 Logical OR operator (p: 99) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul> +<li> 6.5.3.3 Unary arithmetic operators (p: 79) </li> +<li> 6.5.13 Logical AND operator (p: 89) </li> +<li> 6.5.14 Logical OR operator (p: 89) </li> +</ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul> +<li> 3.3.3.3 Unary arithmetic operators </li> +<li> 3.3.13 Logical AND operator </li> +<li> 3.3.14 Logical OR operator </li> +</ul> +</ul> <h3 id="See_Also"> See Also</h3> <p><a href="operator_precedence" title="c/language/operator precedence"> Operator precedence</a></p> +<table class="wikitable"> <tr style="text-align:center"> <th colspan="7"> Common operators </th> +</tr> <tr style="text-align:center"> <td> <a href="operator_assignment" title="c/language/operator assignment"> assignment</a> </td> <td> <a href="operator_incdec" title="c/language/operator incdec"> increment<br>decrement</a> </td> <td> <a href="operator_arithmetic" title="c/language/operator arithmetic"> arithmetic</a> </td> <td> <strong class="selflink"> logical</strong> </td> <td> <a href="operator_comparison" title="c/language/operator comparison"> comparison</a> </td> <td> <a href="operator_member_access" title="c/language/operator member access"> member<br>access</a> </td> <td> <a href="operator_other" title="c/language/operator other"> other</a> </td> +</tr> <tr style="text-align:center"> <td> <p><code>a = b a += b a -= b a *= b a /= b a %= b a &= b a |= b a ^= b a <<= b a >>= b</code></p> +</td> <td> <p><code>++a --a a++ a--</code></p> +</td> <td> <p><code>+a -a a + b a - b a * b a / b a % b ~a a & b a | b a ^ b a << b a >> b</code></p> +</td> <td> <p><code>!a a && b a || b</code></p> +</td> <td> <p><code>a == b a != b a < b a > b a <= b a >= b</code></p> +</td> <td> <p><code>a[b] *a &a a->b a.b</code></p> +</td> <td> <p><code>a(...) a, b (type) a a ? b : c sizeof</code><br><br> <code>_Alignof</code><br><span class="t-mark-rev t-since-c11">(since C11)</span></p> +</td> +</tr> </table> <h3 id="See_also_2"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/operator_logical" title="cpp/language/operator logical">C++ documentation</a></span> for <span class=""><span>Logical operators</span></span> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/language/operator_logical" class="_attribution-link">https://en.cppreference.com/w/c/language/operator_logical</a> + </p> +</div> |
