Operator precedence

From Rosetta Code
Revision as of 10:53, 17 August 2012 by rosettacode>NevilleDNZ (draft task)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Operator precedence is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
This page uses content from Wikipedia. The original article was at Operators in C and C++. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)

The following contains tables of precedence and associativity of all the operators various languages. Operators are listed top to bottom, in descending precedence. Descending precedence refers to the priority of evaluation. Considering an expression, an operator which is listed on some row will be evaluated prior to any operator that is listed on a row further below it. Operators that are in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same precedence, in the given direction.

C++

The following is a table that lists the precedence and associativity of all the operators in the C and C++ languages. An operator's precedence is unaffected by overloading.

Precedence Operator Description Associativity
1

highest

:: Scope resolution (C++ only) Left-to-right
2 ++ Suffix increment
-- Suffix decrement
() Function call
[] Array subscripting
. Element selection by reference
-> Element selection through pointer
typeid() Run-time type information (C++ only) (see typeid)
const_cast Type cast (C++ only) (see const cast)
dynamic_cast Type cast (C++ only) (see dynamic_cast)
reinterpret_cast Type cast (C++ only) (see reinterpret cast)
static_cast Type cast (C++ only) (see static cast)
3 ++ Prefix increment Right-to-left
-- Prefix decrement
+ Unary plus
- Unary minus
! Logical NOT
~ Bitwise NOT
(type) Type cast
* Indirection (dereference)
& Address-of
sizeof Size-of
new, new[] Dynamic memory allocation (C++ only)
delete, delete[] Dynamic memory deallocation (C++ only)
4 .* Pointer to member (C++ only) Left-to-right
->* Pointer to member (C++ only)
5 * Multiplication
/ Division
% Modulo (remainder)
6 + Addition
- Subtraction
7 << Bitwise left shift
>> Bitwise right shift
8 < Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
9 == Equal to
!= Not equal to
10 & Bitwise AND
11 ^ Bitwise XOR (exclusive or)
12 | Bitwise OR (inclusive or)
13 && Logical AND
14 || Logical OR
15 ?: Ternary conditional (see ?:) Right-to-left
16 = Direct assignment
+= Assignment by sum
-= Assignment by difference
*= Assignment by product
/= Assignment by quotient
%= Assignment by remainder
<<= Assignment by bitwise left shift
>>= Assignment by bitwise right shift
&= Assignment by bitwise AND
^= Assignment by bitwise XOR
|= Assignment by bitwise OR
17 throw Throw operator (exceptions throwing, C++ only)
18 , Comma Left-to-right