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
|
<h1 id="firstHeading" class="firstHeading">Expressions</h1> <p>An expression is a sequence of <i>operators</i> and their <i>operands</i>, that specifies a computation.</p>
<p>Expression evaluation may produce a result (e.g., evaluation of <code>2+2</code> produces the result <code>4</code>), may generate side-effects (e.g. evaluation of <code><a href="http://en.cppreference.com/w/c/io/fprintf"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"%d"</span>,<span class="nu0">4</span><span class="br0">)</span></code> sends the character <code>'4'</code> to the standard output stream), and may designate objects or functions.</p>
<h4 id="General"> General</h4> <ul>
<li> <a href="value_category" title="c/language/value category">value categories</a> (lvalue, non-lvalue object, function designator) classify expressions by their values </li>
<li> <a href="eval_order" title="c/language/eval order">order of evaluation</a> of arguments and subexpressions specifies the order in which intermediate results are obtained </li>
</ul> <h3 id="Operators"> Operators</h3> <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> <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 &= 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> <ul>
<li> <a href="operator_precedence" title="c/language/operator precedence"> operator precedence</a> defines the order in which operators are bound to their arguments </li>
<li> <a href="operator_alternative" title="c/language/operator alternative"> alternative representations</a> are alternative spellings for some operators </li>
</ul> <h4 id="Conversions"> Conversions</h4> <ul>
<li> <a href="conversion" title="c/language/conversion">Implicit conversions</a> take place when types of operands do not match the expectations of operators </li>
<li> <a href="cast" title="c/language/cast">Casts</a> may be used to explicitly convert values from one type to another. </li>
</ul> <h4 id="Other"> Other</h4> <ul><li> <a href="constant_expression" title="c/language/constant expression">constant expressions</a> can be evaluated at compile time and used in compile-time context (<span class="t-rev-inl t-since-c99"><span>non-VLA </span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span>array sizes, static initializers, etc) </li></ul> <table class="t-rev-begin"> <tr class="t-rev t-since-c11">
<td> <ul><li> <a href="generic" title="c/language/generic">generic selections</a> can execute different expressions depending on the types of the arguments </li></ul> </td> <td><span class="t-mark-rev t-since-c11">(since C11)</span></td>
</tr> </table> <table class="t-rev-begin"> <tr class="t-rev t-since-c99">
<td> <ul>
<li> Floating-point expressions may raise exceptions and report errors as specified in <a href="../numeric/math/math_errhandling" title="c/numeric/math/math errhandling"><code>math_errhandling</code></a> </li>
<li> The standard <a href="../preprocessor/impl" title="c/preprocessor/impl">#pragmas</a> <code>FENV_ACCESS</code>, <code>FP_CONTRACT</code>, and <code>CX_LIMITED_RANGE</code> as well as the <a href="../types/limits/flt_eval_method" title="c/types/limits/FLT EVAL METHOD">floating-point evaluation precision</a> and <a href="../numeric/fenv/fe_round" title="c/numeric/fenv/FE round">rounding direction</a> control the way floating-point expressions are executed. </li>
</ul> </td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
</tr> </table> <h3 id="Primary_expressions"> Primary expressions</h3> <p>The operands of any operator may be other expressions or they may be <i>primary expressions</i> (e.g. in <code>1+2*3</code>, the operands of operator+ are the subexpression <code>2*3</code> and the primary expression <code>1</code>).</p>
<p>Primary expressions are any of the following:</p>
<div class="t-li1">
<span class="t-li">1)</span> Constants and literals (e.g. <code>2</code> or <code>"Hello, world"</code>)</div> <div class="t-li1">
<span class="t-li">2)</span> Suitably declared <a href="identifier" title="c/language/identifier">identifiers</a> (e.g. <code>n</code> or <code><a href="http://en.cppreference.com/w/c/io/fprintf"><span class="kw851">printf</span></a></code>)</div> <table class="t-rev-begin"> <tr class="t-rev t-since-c11">
<td> <span class="t-li">3)</span> <a href="generic" title="c/language/generic">Generic selections</a> </td> <td><span class="t-mark-rev t-since-c11">(since C11)</span></td>
</tr> </table> <p>Any expression in parentheses is also classified as a primary expression: this guarantees that the parentheses have higher precedence than any operator.</p>
<h4 id="Constants_and_literals"> Constants and literals</h4> <p>Constant values of certain types may be embedded in the source code of a C program using specialized expressions known as literals (for lvalue expressions) and constants (for non-lvalue expressions)</p>
<ul>
<li> <a href="integer_constant" title="c/language/integer constant">integer constants</a> are decimal, octal, or hexadecimal numbers of integer type. </li>
<li> <a href="character_constant" title="c/language/character constant">character constants</a> are individual characters of type <code>int</code> suitable for conversion to a character type or of type <span class="t-rev-inl t-since-c23"><span><code>char8_t</code>,</span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span> <span class="t-rev-inl t-since-c11"><span><code>char16_t</code>, <code>char32_t</code>, or </span><span><span class="t-mark-rev t-since-c11">(since C11)</span></span></span><code>wchar_t</code> </li>
<li> <a href="floating_constant" title="c/language/floating constant">floating constants</a> are values of type <code>float</code>, <code>double</code>, or <code>long double</code> </li>
</ul> <table class="t-rev-begin"> <tr class="t-rev t-since-c23">
<td> <ul>
<li> predefined constants <a href="bool_constant" title="c/language/bool constant"><code>true</code>/<code>false</code></a> are values of type <code>bool</code> </li>
<li> predefined constant <a href="nullptr" title="c/language/nullptr"><code>nullptr</code></a> is a value of type <code><a href="../types/nullptr_t" title="c/types/nullptr t">nullptr_t</a></code> </li>
</ul> </td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td>
</tr> </table> <ul><li> <a href="string_literal" title="c/language/string literal">string literals</a> are sequences of characters of type <code>char[]</code><span class="t-rev-inl t-since-c23"><span>, <code>char8_t[]</code></span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span><span class="t-rev-inl t-since-c11"><span>, <code>char16_t[]</code>, <code>char32_t[]</code>,</span><span><span class="t-mark-rev t-since-c11">(since C11)</span></span></span> or <code>wchar_t[]</code> that represent null-terminated strings </li></ul> <table class="t-rev-begin"> <tr class="t-rev t-since-c99">
<td> <ul><li> <a href="compound_literal" title="c/language/compound literal">compound literals</a> are values of struct, union, or array type directly embedded in program code </li></ul> </td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
</tr> </table> <h3 id="Unevaluated_expressions"> Unevaluated expressions</h3> <p>The operands of the <a href="sizeof" title="c/language/sizeof"><code>sizeof</code> operator</a> are expressions that are not evaluated<span class="t-rev-inl t-since-c99"><span> (unless they are VLAs)</span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span>. Thus, <code><a href="http://en.cppreference.com/w/c/types/size_t"><span class="kw100">size_t</span></a> n <span class="sy1">=</span> <span class="kw4">sizeof</span><span class="br0">(</span><a href="http://en.cppreference.com/w/c/io/fprintf"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"%d"</span>, <span class="nu0">4</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span></code> does not perform console output.</p>
<table class="t-rev-begin"> <tr class="t-rev t-since-c11">
<td> <p>The operands of the <a href="_alignof" title="c/language/ Alignof"><code>_Alignof</code> operator</a>, the controlling expression of a <a href="generic" title="c/language/generic">generic selection</a>, and size expressions of VLAs that are operands of <code>_Alignof</code> are also expressions that are not evaluated.</p>
</td> <td><span class="t-mark-rev t-since-c11">(since C11)</span></td>
</tr> </table> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 6.5 Expressions (p: 55-75) </li>
<li> 6.6 Constant expressions (p: 76-77) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 6.5 Expressions (p: 76-105) </li>
<li> 6.6 Constant expressions (p: 106-107) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 6.5 Expressions (p: 67-94) </li>
<li> 6.6 Constant expressions (p: 95-96) </li>
</ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul>
<li> 3.3 EXPRESSIONS </li>
<li> 3.4 CONSTANT EXPRESSIONS </li>
</ul>
</ul> <h3 id="See_also"> 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/expressions" title="cpp/language/expressions">C++ documentation</a></span> for <span class=""><span>Expressions</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/expressions" class="_attribution-link">https://en.cppreference.com/w/c/language/expressions</a>
</p>
</div>
|