Operator precedence: Difference between revisions
(→Tcl: Added description) |
m (→{{header|Tcl}}: Tidy up, link to more information) |
||
Line 262: | Line 262: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Tcl only supports operators within an expression context (such as the <code>expr</code> command): |
Tcl only supports operators within an expression context (such as the [http://www.tcl.tk/man/tcl8.6/TclCmd/expr.htm#M6 <code>expr</code>] command, which lists the operators with more detail): |
||
{| class="wikitable" |
{| class="wikitable" |
||
Line 271: | Line 271: | ||
|- |
|- |
||
! highest |
! highest |
||
| - + ~ ! |
| '''- + ~ !''' |
||
| Unary minus, unary plus, bit-wise NOT, logical NOT |
| Unary minus, unary plus, bit-wise NOT, logical NOT. |
||
|- |
|- |
||
! |
! |
||
| ** |
| '''**''' |
||
| Exponentiation |
| Exponentiation. Right-to-left associative. |
||
|- |
|- |
||
! |
! |
||
| * / % |
| '''* / %''' |
||
| Multiply, divide, remainder. |
|||
| Multiply, divide, remainder. None of these operators may be applied to string operands, and remainder may be applied only to integers. The remainder will always have the same sign as the divisor and an absolute value smaller than the divisor. |
|||
|- |
|- |
||
! |
! |
||
| + - |
| '''+ -''' |
||
| Add and subtract |
| Add and subtract. |
||
|- |
|- |
||
! |
! |
||
| << >> |
| '''<< >>''' |
||
| Left and right shift |
| Left and right shift. |
||
|- |
|- |
||
! |
! |
||
| < > <= >= |
| '''< > <= >=''' |
||
| Boolean less, greater, less than or equal, and greater than or equal |
| Boolean less, greater, less than or equal, and greater than or equal. |
||
|- |
|- |
||
! |
! |
||
| == != |
| '''== !=''' |
||
| Boolean equal and not equal |
| Boolean equal and not equal. |
||
|- |
|- |
||
! |
! |
||
| eq ne |
| '''eq ne''' |
||
| Boolean string equal and string not equal |
| Boolean string equal and string not equal. |
||
|- |
|- |
||
! |
! |
||
| in ni |
| '''in ni''' |
||
| List containment and negated list containment. |
|||
| List containment and negated list containment. Each operator produces a zero/one result and treats its first argument as a string and its second argument as a Tcl list. The in operator indicates whether the first argument is a member of the second argument list; the ni operator inverts the sense of the result. |
|||
|- |
|- |
||
! |
! |
||
| & |
| '''&''' |
||
| Bit-wise AND |
| Bit-wise AND. |
||
|- |
|- |
||
! |
! |
||
| ^ |
| '''^''' |
||
| Bit-wise exclusive OR |
| Bit-wise exclusive OR. |
||
|- |
|- |
||
! |
! |
||
| <nowiki>|</nowiki> |
| '''<nowiki>|</nowiki>''' |
||
| Bit-wise OR. Valid for integer operands only. |
| Bit-wise OR. Valid for integer operands only. |
||
|- |
|- |
||
! |
! |
||
| && |
| '''&&''' |
||
| Logical AND |
| Logical AND. Evaluates its second operand lazily. |
||
|- |
|- |
||
! |
! |
||
| <nowiki>||</nowiki> |
| '''<nowiki>||</nowiki>''' |
||
| Logical OR |
| Logical OR. Evaluates its second operand lazily. |
||
|- |
|- |
||
! lowest |
! lowest |
||
| ''x'' |
| ''x'' '''?''' ''y'' ''':''' ''z'' |
||
| If-then-else, as in [[C]] |
| If-then-else, as in [[C]]. Evaluates its second and third operands lazily. |
||
|} |
|} |
Revision as of 06:59, 19 August 2012
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 |
J
Precedence | Grammatical classification | Associativity |
---|---|---|
highest | conjunctions | long left scope |
adverbs | ||
lowest | verbs | long right scope |
See http://www.jsoftware.com/help/dictionary/partsofspeech.htm for tokens in each grammatical class.
Note that other parts of speech do not have any precedence, because they are not "operators".
Note that this is an imprecise statement of the grammatical rules. For a complete treatment, see http://www.jsoftware.com/help/dictionary/dicte.htm
Here's an informal treatment of the grammar:
Conjunctions require a left and right argument, either of which may be a noun or a verb. If one argument is omitted the conjunction is curried with the remaining argument, forming an adverb (if the right argument is omitted, the precedence of the conjunction is treated as lower than the precedence of a verb).
Adverbs require a single left argument, which may be a noun or a verb.
Verbs require a right argument which must be a noun and may accept an optional left argument (which must also be a noun). Unless we're working with a dangling (rightmost) conjunction, verbs have lower precedence than adverbs and conjunctions.
Nouns are not operators and accept no arguments.
The result of a verb must be a noun.
The result of an adverb or a conjunction can have any one of these grammatical classifications, and verb results are typical (and, thus, the result of an adverb or a conjunction may accept further arguments). Adverbs and conjunctions serve a role analogous to that of macros in other languages.
OCaml
This table contains the precedence and associativity of operators and other expression constructs in OCaml, including user-defined operators.
PHP
Python
See this table and the whole page for details on Python version 3.x
Tcl
Tcl only supports operators within an expression context (such as the expr
command, which lists the operators with more detail):
Precedence | Operator | Description |
---|---|---|
highest | - + ~ ! | Unary minus, unary plus, bit-wise NOT, logical NOT. |
** | Exponentiation. Right-to-left associative. | |
* / % | Multiply, divide, remainder. | |
+ - | Add and subtract. | |
<< >> | Left and right shift. | |
< > <= >= | Boolean less, greater, less than or equal, and greater than or equal. | |
== != | Boolean equal and not equal. | |
eq ne | Boolean string equal and string not equal. | |
in ni | List containment and negated list containment. | |
& | Bit-wise AND. | |
^ | Bit-wise exclusive OR. | |
| | Bit-wise OR. Valid for integer operands only. | |
&& | Logical AND. Evaluates its second operand lazily. | |
|| | Logical OR. Evaluates its second operand lazily. | |
lowest | x ? y : z | If-then-else, as in C. Evaluates its second and third operands lazily. |