Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Operators in C and C++
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Expression evaluation order== During expression evaluation, the order in which sub-expressions are evaluated is determined by [[order of operations|precedence]] and [[operator associativity|associativity]]. An operator with higher precedence is evaluated before a operator of lower precedence and the operands of an operator are evaluated based on associativity. The following table describes the precedence and associativity of the C and C++ operators. Operators are shown in groups of equal precedence with groups ordered in descending precedence from top to bottom (lower order is higher precedence).<ref>{{cite book | title = ISO/IEC 9899:201x Programming Languages - C | date = 19 December 2011 | publisher = open-std.org – The C Standards Committee | pages = 465}}</ref><ref>{{cite tech report |title=the ISO C 1999 standard, section 6.5.6 note 71 |institution=ISO |year=1999 }}</ref><ref>{{cite web |title=C++ Built-in Operators, Precedence and Associativity |url=https://docs.microsoft.com/en-US/cpp/cpp/cpp-built-in-operators-precedence-and-associativity |website=docs.microsoft.com |access-date=11 May 2020 |language=en-us}}</ref> Operator precedence is not affected by overloading. {| class="wikitable" |- ! style="text-align: left" | Order ! style="text-align: left" | Operator ! style="text-align: left" | Description ! style="text-align: left" | Associativity |- ! 1 <small>highest</small> | <code>::</code> | [[Scope resolution operator#C.2B.2B|Scope resolution]] (C++ only) | None |- ! rowspan=11| 2 | style="border-bottom-style: none" | <code>++</code> | style="border-bottom-style: none" | Postfix increment | style="vertical-align: top" rowspan="11" | Left-to-right |- | style="border-bottom-style: none; border-top-style: none" | <code>--</code> | style="border-bottom-style: none; border-top-style: none" | Postfix decrement |- | style="border-bottom-style: none; border-top-style: none" | <code>()</code> | style="border-bottom-style: none; border-top-style: none" | Function call |- | style="border-bottom-style: none; border-top-style: none" | <code>[]</code> | style="border-bottom-style: none; border-top-style: none" | Array subscripting |- | style="border-bottom-style: none; border-top-style: none" | <code>.</code> | style="border-bottom-style: none; border-top-style: none" | Element selection by reference |- | style="border-bottom-style: none; border-top-style: none" | <code>-></code> | style="border-bottom-style: none; border-top-style: none" | Element selection through pointer |- | style="border-bottom-style: none; border-top-style: none" | <code>typeid()</code> | style="border-bottom-style: none; border-top-style: none" | [[Run-time type information]] (C++ only) (see [[typeid]]) |- | style="border-bottom-style: none; border-top-style: none" | <code>const_cast</code> | style="border-bottom-style: none; border-top-style: none" | Type cast (C++ only) (see [[const_cast]]) |- | style="border-bottom-style: none; border-top-style: none" | <code>dynamic_cast</code> | style="border-bottom-style: none; border-top-style: none" | Type cast (C++ only) (see [[dynamic cast]]) |- | style="border-bottom-style: none; border-top-style: none" | <code>reinterpret_cast</code> | style="border-bottom-style: none; border-top-style: none" | Type cast (C++ only) (see [[reinterpret_cast]]) |- | style="border-top-style: none" | <code>static_cast</code> | style="border-top-style: none" | Type cast (C++ only) (see [[static_cast]]) |- ! rowspan="13" | 3 | style="border-bottom-style: none" | <code>++</code> | style="border-bottom-style: none" | Prefix increment | rowspan="13" style="vertical-align: top" | Right-to-left |- | style="border-bottom-style: none; border-top-style: none" | <code>--</code> | style="border-bottom-style: none; border-top-style: none" | Prefix decrement |- | style="border-bottom-style: none; border-top-style: none" | <code>+</code> | style="border-bottom-style: none; border-top-style: none" | Unary plus |- | style="border-bottom-style: none; border-top-style: none" | <code>-</code> | style="border-bottom-style: none; border-top-style: none" | Unary minus |- | style="border-bottom-style: none; border-top-style: none" | <code>!</code> | style="border-bottom-style: none; border-top-style: none" | Logical NOT |- | style="border-bottom-style: none; border-top-style: none" | <code>~</code> | style="border-bottom-style: none; border-top-style: none" | Bitwise NOT ([[ones' complement]]) |- | style="border-bottom-style: none; border-top-style: none" | <code>(''type'')</code> | style="border-bottom-style: none; border-top-style: none" | Type cast |- | style="border-bottom-style: none; border-top-style: none" | <code>*</code> | style="border-bottom-style: none; border-top-style: none" | Indirection (dereference) |- | style="border-bottom-style: none; border-top-style: none" | <code>&</code> | style="border-bottom-style: none; border-top-style: none" | Address-of |- | style="border-bottom-style: none; border-top-style: none" | <code>sizeof</code> | style="border-bottom-style: none; border-top-style: none" | [[Sizeof]] |- | style="border-bottom-style: none; border-top-style: none" | <code>_Alignof</code> | style="border-bottom-style: none; border-top-style: none" | Alignment requirement (since C11) |- | style="border-bottom-style: none; border-top-style: none" | <code>new</code>, <code>new[]</code> | style="border-bottom-style: none; border-top-style: none" | Dynamic memory allocation (C++ only) |- | style="border-top-style: none" | <code>delete</code>, <code>delete[]</code> | style="border-top-style: none" | Dynamic memory deallocation (C++ only) |- ! rowspan=2| 4 | style="border-bottom-style: none" | <code>.*</code> | style="border-bottom-style: none" | Pointer to member (C++ only) | style="vertical-align: top" rowspan="2" | Left-to-right |- | style="border-bottom-style: none; border-top-style: none" | <code>->*</code> | style="border-bottom-style: none; border-top-style: none" | Pointer to member (C++ only) |- ! rowspan=3| 5 | style="border-bottom-style: none" | <code>*</code> | style="border-bottom-style: none" | Multiplication | style="vertical-align: top" rowspan="3" | Left-to-right |- | style="border-bottom-style: none; border-top-style: none" | <code>/</code> | style="border-bottom-style: none; border-top-style: none" | Division |- | style="border-bottom-style: none; border-top-style: none" | <code>%</code> | style="border-bottom-style: none; border-top-style: none" | [[Modulo operation|Modulo]] (remainder) |- ! rowspan=2| 6 | style="border-bottom-style: none" | <code>+</code> | style="border-bottom-style: none" | Addition | style="vertical-align: top" rowspan="2" | Left-to-right |- | style="border-bottom-style: none; border-top-style: none" | <code>-</code> | style="border-bottom-style: none; border-top-style: none" | Subtraction |- ! rowspan=2| 7 | style="border-bottom-style: none" | <code><<</code> | style="border-bottom-style: none" | [[Bitwise operation|Bitwise]] left shift | style="vertical-align: top" rowspan="2" | Left-to-right |- | style="border-bottom-style: none; border-top-style: none" | <code>>></code> | style="border-bottom-style: none; border-top-style: none" | [[Bitwise operation|Bitwise]] right shift |- ! rowspan=1| 8 | style="border-bottom-style:none;" | <code><=></code> | style="border-bottom-style:none;" | [[Three-way comparison]] (Introduced in [[C++20]] - C++ only) | style="vertical-align: top" rowspan="1" | Left-to-right |- ! rowspan=4| 9 | style="border-bottom-style: none" | <code><</code> | style="border-bottom-style: none" | Less than | style="vertical-align: top" rowspan="4" | Left-to-right |- | style="border-bottom-style: none; border-top-style: none" | <code><=</code> | style="border-bottom-style: none; border-top-style: none" | Less than or equal to |- | style="border-bottom-style: none; border-top-style: none" | <code>></code> | style="border-bottom-style: none; border-top-style: none" | Greater than |- | style="border-bottom-style: none; border-top-style: none" | <code>>=</code> | style="border-bottom-style: none; border-top-style: none" | Greater than or equal to |- ! rowspan=2| 10 | style="border-bottom-style: none" | <code>==</code> | style="border-bottom-style: none" | Equal to | style="vertical-align: top" rowspan="2" | Left-to-right |- | style="border-bottom-style: none; border-top-style: none" | <code>!=</code> | style="border-bottom-style: none; border-top-style: none" | Not equal to |- ! 11 | <code>&</code> | Bitwise AND | Left-to-right |- ! 12 | <code>^</code> | Bitwise XOR (exclusive or) | Left-to-right |- ! 13 | <code><nowiki>|</nowiki></code> | Bitwise OR (inclusive or) | Left-to-right |- ! 14 | <code>&&</code> | Logical AND | Left-to-right |- ! 15 | <code><nowiki>||</nowiki></code> | Logical OR | Left-to-right |- ! rowspan="2" | 16 | style="border-bottom-style: none" | <code>co_await</code> | rowspan="2" | Coroutine processing (C++ only) | rowspan="2" | Right-to-left |- | style="border-top-style: none" | <code>co_yield</code> |- ! rowspan="13" | 17 | style="border-bottom-style: none" | <code>?:</code> | style="border-bottom-style: none" | [[Ternary conditional operator]] | rowspan="13" | Right-to-left |- | style="border-bottom-style: none; border-top-style: none" | <code>=</code> | style="border-bottom-style: none; border-top-style: none" | Direct assignment |- | style="border-bottom-style: none; border-top-style: none" | <code>+=</code> | style="border-bottom-style: none; border-top-style: none" | Assignment by sum |- | style="border-bottom-style: none; border-top-style: none" | <code>-=</code> | style="border-bottom-style: none; border-top-style: none" | Assignment by difference |- | style="border-bottom-style: none; border-top-style: none" | <code>*=</code> | style="border-bottom-style: none; border-top-style: none" | Assignment by product |- | style="border-bottom-style: none; border-top-style: none" | <code>/=</code> | style="border-bottom-style: none; border-top-style: none" | Assignment by quotient |- | style="border-bottom-style: none; border-top-style: none" | <code>%=</code> | style="border-bottom-style: none; border-top-style: none" | Assignment by remainder |- | style="border-bottom-style: none; border-top-style: none" | <code><<=</code> | style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise left shift |- | style="border-bottom-style: none; border-top-style: none" | <code>>>=</code> | style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise right shift |- | style="border-bottom-style: none; border-top-style: none" | <code>&=</code> | style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise AND |- | style="border-bottom-style: none; border-top-style: none" | <code>^=</code> | style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise XOR |- | style="border-bottom-style: none; border-top-style: none" | <code><nowiki>|</nowiki>=</code> | style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise OR |- | style="border-top-style: none" | <code>throw</code> | style="border-top-style: none" | Throw operator (exceptions throwing, C++ only) |- ! 18 <small>lowest</small> | <code>,</code> | [[Comma operator|Comma]] | Left-to-right |} ===Details=== Although this table is adequate for describing most evaluation order, it does not describe a few details. The [[ternary operator]] allows any arbitrary expression as its middle operand, despite being listed as having higher precedence than the assignment and comma operators. Thus <code>a ? b, c : d</code> is interpreted as <code>a ? (b, c) : d</code>, and not as the meaningless <code>(a ? b), (c : d)</code>. So, the expression in the middle of the conditional operator (between <code>'''?'''</code> and <code>''':'''</code>) is parsed as if parenthesized. Also, the immediate, un-parenthesized result of a C cast expression cannot be the operand of <code>sizeof</code>. Therefore, <code>sizeof (int) * x</code> is interpreted as <code>(sizeof(int)) * x</code> and not <code>sizeof ((int) * x)</code>. ===Chained expressions=== The precedence table determines the order of binding in chained expressions, when it is not expressly specified by parentheses. * For example, <code>++x*3</code> is ambiguous without some precedence rule(s). The precedence table tells us that: {{mono|x}} is 'bound' more tightly to {{mono|++}} than to {{mono|*}}, so that whatever {{mono|++}} does (now or later—see below), it does it ONLY to {{mono|x}} (and not to <code>x*3</code>); it is equivalent to (<code>++x</code>, <code>x*3</code>). * Similarly, with <code>3*x++</code>, where though the post-fix {{mono|++}} is designed to act AFTER the entire expression is evaluated, the precedence table makes it clear that ONLY {{mono|x}} gets incremented (and NOT <code>3*x</code>). In fact, the expression (<code>tmp=x++</code>, <code>3*tmp</code>) is evaluated with {{mono|tmp}} being a temporary value. It is functionally equivalent to something like (<code>tmp=3*x</code>, <code>++x</code>, <code>tmp</code>). [[Image:Precedence 2.png|center|thumb|Precedence and bindings]] * Abstracting the issue of precedence or binding, consider the diagram above for the expression 3+2*y[i]++. The compiler's job is to resolve the diagram into an expression, one in which several unary operators (call them 3+( . ), 2*( . ), ( . )++ and ( . )[ i ]) are competing to bind to y. The order of precedence table resolves the final sub-expression they each act upon: ( . )[ i ] acts only on y, ( . )++ acts only on y[i], 2*( . ) acts only on y[i]++ and 3+( . ) acts 'only' on 2*((y[i])++). It is important to note that WHAT sub-expression gets acted on by each operator is clear from the precedence table but WHEN each operator acts is not resolved by the precedence table; in this example, the ( . )++ operator acts only on y[i] by the precedence rules but binding levels alone do not indicate the timing of the postfix ++ (the ( . )++ operator acts only after y[i] is evaluated in the expression). ===Binding=== The binding of operators in C and C++ is specified by a factored language grammar, rather than a precedence table. This creates some subtle conflicts. For example, in C, the syntax for a conditional expression is: <syntaxhighlight lang="c">logical-OR-expression ? expression : conditional-expression</syntaxhighlight> while in C++ it is: <syntaxhighlight lang="cpp">logical-OR-expression ? expression : assignment-expression</syntaxhighlight> Hence, the expression: <syntaxhighlight lang="text">e = a < d ? a++ : a = d</syntaxhighlight> is parsed differently in the two languages. In C, this expression is a syntax error, because the syntax for an assignment expression in C is: <syntaxhighlight lang="c">unary-expression '=' assignment-expression</syntaxhighlight> In C++, it is parsed as: <syntaxhighlight lang="cpp">e = (a < d ? a++ : (a = d))</syntaxhighlight> which is a valid expression.<ref>{{cite web |title=C Operator Precedence - cppreference.com |url=https://en.cppreference.com/w/c/language/operator_precedence |website=en.cppreference.com |access-date=10 April 2020}}</ref><ref>{{Cite web|url=https://stackoverflow.com/questions/13515434/does-the-c-c-ternary-operator-actually-have-the-same-precedence-as-assignment/13515505|title=Does the C/C++ ternary operator actually have the same precedence as assignment operators?|website=Stack Overflow|access-date=2019-09-22}}</ref> To use the comma operator in a function call argument expression, variable assignment, or a comma-separated list, use of parentheses is required.<ref>{{cite web |title=Other operators - cppreference.com |url=https://en.cppreference.com/w/c/language/operator_other |website=en.cppreference.com |access-date=10 April 2020}}</ref><ref>{{cite web |title=c++ - How does the Comma Operator work |url=https://stackoverflow.com/questions/54142/how-does-the-comma-operator-work/ |website=Stack Overflow |access-date=1 April 2020}}</ref> For example, <syntaxhighlight lang="cpp"> int a = 1, b = 2, weirdVariable = (++a, b), d = 4; </syntaxhighlight> ===Criticism of bitwise and equality operators precedence=== The precedence of the bitwise logical operators has been criticized.<ref name="Bell">{{Citation | url = https://www.bell-labs.com/usr/dmr/www/chist.html | title = C history § Neonatal C| publisher = Bell labs}}.</ref> Conceptually, & and | are arithmetic operators like * and +. The expression {{cpp|1=a & b == 7}} is syntactically parsed as {{cpp|1=a & (b == 7)}} whereas the expression {{cpp |1=a + b == 7}} is parsed as {{cpp|1=(a + b) == 7}}. This requires parentheses to be used more often than they otherwise would. Historically, there was no syntactic distinction between the bitwise and logical operators. In [[BCPL]], [[B (programming language)|B]] and early C, the operators {{cpp|&& {{!!}}}} didn't exist. Instead {{cpp|& {{!}}}} had different meaning depending on whether they are used in a 'truth-value context' (i.e. when a Boolean value was expected, for example in {{cpp|1= if (a==b & c) {...} }} it behaved as a logical operator, but in {{cpp|1= c = a & b}} it behaved as a bitwise one). It was retained so as to keep [[backward compatibility]] with existing installations.<ref>{{cite web|url=https://www.perlmonks.org/?node_id=1159769|title=Re^10: next unless condition|website=www.perlmonks.org|access-date=23 March 2018}}</ref> Moreover, in C++ (and later versions of C) equality operations, with the exception of the three-way comparison operator, yield [[Boolean data type|bool]] type values which are conceptually a single bit (1 or 0) and as such do not properly belong in "bitwise" operations.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)