summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Foperator_incdec.html
blob: 37060cb378300e7a5586493223c44643881bf9d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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>