Operators in C and C++
Operator precedence
The following is a table that lists the precedence and associativity of all the operators in the C++ programming language. Operators are listed top to bottom in descending precedence and operators that are on the same row are evaluated with the same precedence, in the given direction.
The syntax of expressions in C and C++ is specified by a context-free grammar. The table given here has been inferred from the grammar.
A precedence table, while mostly adequate, cannot resolve a few details. In particular, note that 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 a ? b , c : d is interpreted as a ? (b, c) : d, and not as the meaningless (a ? b), (c : d). Also, note that the immediate, unparenthesized result of a C cast expression cannot be the operand of sizeof. Therefore, sizeof (int) * x is interpreted as (sizeof(int)) * x and not sizeof ((int) *x).
Operator(s)
Description
Associativity
::
Scope resolution (C++ only)
Left-to-Right
++ --()[].->
Postfix increment and decrementFunction callArray subscriptingElement selection by referenceElement selection through pointer
++ --+ -! ~(type)*&sizeofnew new[]delete delete[]
Prefix increment and decrementUnary plus and minusLogical NOT and bitwise NOTType castIndirection (dereference)Address-of (reference)Size-ofDynamic memory allocation (C++ only)Dynamic memory deallocation (C++ only)
Right-to-Left
.* ->*
Pointer to member (C++ only)
Left-to-Right
* / %
Multiplication, division, and modulus (remainder)
+ -
Addition and subtraction
<< >>
Bitwise left shift and right shift
< <=> >=
Relational “less than” and “less than or equal to”Relational “greater than” and “greater than or equal to”
== !=
Relational “equal to” and “not equal to”
&
Bitwise AND
^
Bitwise XOR (exclusive or)
Bitwise OR (inclusive or)
&&
Logical AND
Logical OR
c?t:f
Ternary conditional (see ?:)
Right-to-Left
=+= -=*= /= %=<<= >>=&= ^= =
Direct assignmentAssignment by sum and differenceAssignment by product, dividend, and remainderAssignment by bitwise left shift and right shiftAssignment by bitwise AND, XOR, and OR
throw
Throw operator (exceptions throwing, C++ only)
Not available
,
Comma
Left-to-Right
[edit]
No comments:
Post a Comment