summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Foperator_incdec.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/language%2Foperator_incdec.html
new repository
Diffstat (limited to 'devdocs/c/language%2Foperator_incdec.html')
-rw-r--r--devdocs/c/language%2Foperator_incdec.html98
1 files changed, 98 insertions, 0 deletions
diff --git a/devdocs/c/language%2Foperator_incdec.html b/devdocs/c/language%2Foperator_incdec.html
new file mode 100644
index 00000000..37060cb3
--- /dev/null
+++ b/devdocs/c/language%2Foperator_incdec.html
@@ -0,0 +1,98 @@
+ <h1 id="firstHeading" class="firstHeading">Increment/decrement operators</h1> <p>Increment/decrement operators are unary operators that increment/decrement the value of a variable by 1.</p>
+<p>They can have postfix form:</p>
+<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <span class="t-spar">expr</span> <code>++</code> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <span class="t-spar">expr</span> <code>--</code> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td>
+</tr>
+</table> <p>As well as the prefix form:</p>
+<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <code>++</code> <span class="t-spar">expr</span> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <code>--</code> <span class="t-spar">expr</span> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td>
+</tr>
+</table> <p>The operand <span class="t-spar">expr</span> of both prefix and postfix increment or decrement must be a <a href="value_category" title="c/language/value category">modifiable lvalue</a> of <a href="type" title="c/language/type">integer type</a> (including <code>_Bool</code> and enums), real floating type, or a pointer type. It may be cvr-qualified, unqualified, or <a href="atomic" title="c/language/atomic">atomic</a>.</p>
+<p>The result of the postfix increment and decrement operators is the value of <span class="t-spar">expr</span>.</p>
+<p>The result of the prefix increment operator is the result of adding the value <code>1</code> to the value of <span class="t-spar">expr</span>: the expression <code>++e</code> is equivalent to <code>e += 1</code>. The result of the prefix decrement operator is the result of subtracting the value <code>1</code> from the value of <span class="t-spar">expr</span>: the expression <code>--e</code> is equivalent to <code>e -= 1</code>.</p>
+<p>Increment operators initiate the side-effect of adding the value <code>1</code> of appropriate type to the operand. Decrement operators initiate the side-effect of subtracting the value <code>1</code> of appropriate type from the operand. As with any other side-effects, these operations complete at or before the next <a href="eval_order" title="c/language/eval order">sequence point</a>.</p>
+<div class="c source-c"><pre data-language="c">int a = 1;
+int b = a++; // stores 1+a (which is 2) to a
+ // returns the old value of a (which is 1)
+ // After this line, b == 1 and a == 2
+a = 1;
+int c = ++a; // stores 1+a (which is 2) to a
+ // returns 1+a (which is 2)
+ // after this line, c == 2 and a == 2</pre></div> <table class="t-rev-begin"> <tr class="t-rev t-since-c11">
+<td> <p>Post-increment or post-decrement on any <a href="atomic" title="c/language/atomic">atomic variable</a> is an atomic read-modify-write operation with memory order <code><a href="../atomic/memory_order" title="c/atomic/memory order">memory_order_seq_cst</a></code>.</p>
+</td> <td><span class="t-mark-rev t-since-c11">(since C11)</span></td>
+</tr> </table> <p>See <a href="operator_arithmetic" title="c/language/operator arithmetic">arithmetic operators</a> for limitations on pointer arithmetic, as well as for implicit conversions applied to the operands.</p>
+<h3 id="Notes"> Notes</h3> <p>Because of the side-effects involved, increment and decrement operators must be used with care to avoid undefined behavior due to violations of <a href="eval_order" title="c/language/eval order">sequencing rules</a>.</p>
+<p>Increment/decrement operators are not defined for complex or imaginary types: the usual definition of adding/subtracting the real number 1 would have no effect on imaginary types, and making it add/subtract <code>i</code> for imaginaries but <code>1</code> for complex numbers would have made it handle <code>0+yi</code> different from <code>yi</code>.</p>
+<p>Unlike C++ (and some implementations of C), the increment/decrement expressions are never themselves lvalues: <code>&amp;++a</code> is invalid.</p>
+<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
+#include &lt;stdlib.h&gt;
+
+int main(void)
+{
+ int a = 1;
+ int b = 1;
+
+ printf("original values: a == %d, b == %d\n", a, b);
+ printf("result of postfix operators: a++ == %d, b-- == %d\n", a++, b--);
+ printf("after postfix operators applied: a == %d, b == %d\n", a, b);
+ printf("\n");
+
+ // Reset a and b.
+ a = 1;
+ b = 1;
+
+ printf("original values: a == %d, b == %d\n", a, b);
+ printf("result of prefix operators: ++a == %d, --b == %d\n", ++a, --b);
+ printf("after prefix operators applied: a == %d, b == %d\n", a, b);
+}</pre></div> <p>Output:</p>
+<div class="text source-text"><pre data-language="c">original values: a == 1, b == 1
+result of postfix operators: a++ == 1, b-- == 1
+after postfix operators applied: a == 2, b == 0
+
+original values: a == 1, b == 1
+result of prefix operators: ++a == 2, --b == 0
+after prefix operators applied: a == 2, b == 0</pre></div> </div> <h3 id="References"> References</h3> <ul>
+<li> C23 standard (ISO/IEC 9899:2023): </li>
+<ul>
+<li> 6.5.2.4 Postfix increment and decrement operators (p: TBD) </li>
+<li> 6.5.3.1 Prefix increment and decrement operators (p: TBD) </li>
+</ul>
+<li> C17 standard (ISO/IEC 9899:2018): </li>
+<ul>
+<li> 6.5.2.4 Postfix increment and decrement operators (p: TBD) </li>
+<li> 6.5.3.1 Prefix increment and decrement operators (p: TBD) </li>
+</ul>
+<li> C11 standard (ISO/IEC 9899:2011): </li>
+<ul>
+<li> 6.5.2.4 Postfix increment and decrement operators (p: 85) </li>
+<li> 6.5.3.1 Prefix increment and decrement operators (p: 88) </li>
+</ul>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul>
+<li> 6.5.2.4 Postfix increment and decrement operators (p: 75) </li>
+<li> 6.5.3.1 Prefix increment and decrement operators (p: 78) </li>
+</ul>
+<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
+<ul>
+<li> 3.3.2.4 Postfix increment and decrement operators </li>
+<li> 3.3.3.1 Prefix increment and decrement operators </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> <strong class="selflink"> increment<br>decrement</strong> </td> <td> <a href="operator_arithmetic" title="c/language/operator arithmetic"> arithmetic</a> </td> <td> <a href="operator_logical" title="c/language/operator logical"> logical</a> </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 &amp;= b a |= b a ^= b a &lt;&lt;= b a &gt;&gt;= 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 &amp; b a | b a ^ b a &lt;&lt; b a &gt;&gt; b</code></p>
+</td> <td> <p><code>!a a &amp;&amp; b a || b</code></p>
+</td> <td> <p><code>a == b a != b a &lt; b a &gt; b a &lt;= b a &gt;= b</code></p>
+</td> <td> <p><code>a[b] *a &amp;a a-&gt;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> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/operator_incdec" title="cpp/language/operator incdec">C++ documentation</a></span> for <span class=""><span>Increment/decrement operators</span></span> </td>
+</tr> </table> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 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_incdec" class="_attribution-link">https://en.cppreference.com/w/c/language/operator_incdec</a>
+ </p>
+</div>