Operator Precedence


The following tables show the order in which operators are evaluated. Please note the following.
o Summary table.
o Table in detail.
o Operators listed by type.


Summary precedence table.

All operators on the same line have the same precedence. The first line has the highest precedence.

() [] -> .
! ~ ++ -- + - * & sizeof
* / %
+ -
<< >>
< <= >= >
== !=
&
^
|
&&
||
?:
= += -= *= /= %= &= ^= |= <<= >>=
,

Lary Huang has told me that the postfix unary -- and postfix unary ++ have a higher precedence than the prefix unary -- and prefix unary ++.


Detailed precedence table.

All operators in the same 'block' have the same precedence. The first block has the highest precedence.

Group Operator Description Example
Membership.
() Function call
 /Grouping
count = function(4,3);
(a + b) / 4;
[] Array value = array[5] + increment;
-> Structure pointer ptr->age = 34;
. Structure member obj.age = 34;
:: Scoping Class::age = 2;
++ Post - Increment for( i = 0; i < 10; i++ ) ...
Post - Decrement for( i = 10; i > 0; i-- ) ...
Unary.
! Logical NOT if( !done ) ...
~ Bitwise complement flags = ~flags;
++ Pre-Increment for( i = 0; i < 10; ++i ) ...
-- Pre-Decrement for( i = 10; i > 0; --i ) ...
+ Unary plus int i = +1;
- Unary Minus int i = -1;
* Dereference, Pointer to data data = *ptr;
& Address of a variable address = &obj;
sizeof size in bytes int size = sizeof(floatNum);
(type) type cast int i = (int) floatNum;
selector
->* Member pointer selector ptr->*var = 24;
.* Member pointer selector obj.*var = 24;
Binary.
* Multiply. int i = 2 * 4;
/ Divide. float f = 10 / 3;
% Modulo. int rem = 4 % 3;
Binary term
+ Addition int i = 2 + 3;
- Subtraction. int i = 5 - 1;
Bitwise Shift
<< Bitwise shift left. int flags = 33 << 1;
>> Bitwise shift Right. int flags = 33 >> 1;
Relational.
< Less than. if( i < 42 ) ...
> Greater than. if( i > 42 ) ...
<= Less than or equal too. if( i <= 42 ) ...
>= Greater than or equal too. if( i >= 42 ) ...
Equality
== Equal too. if( i == 42 ) ...
!= Not equal too. if( i != 42 ) ...
Bitwise AND
& bitwise AND flags = flags & 42;
Bitwise XOR
^ bitwise Excusive OR flags = flags ^ 42;
Bitwise OR
| bitwise OR flags = flags | 42;
Logical.
&& Logical AND if( conditionA && conditionB ) ...
Logical.
|| Logical OR if( conditionA || conditionB ) ...
Conditional
? : Conditional construct. int i = (a > b) ? a : b;
Assignment
= Equals int a = b;
+= assignment a += 3;
-= assignment b -= 4;
*= assignment a *= 5;
/= assignment a /= 2;
%= assignment a %= 3;
&= assignment flags &= new_flags;
^= assignment flags ^= new_flags;
|= assignment flags ^= new_flags;
<<= assignment flags <<= 2;
>>= assignment flags >>= 2;
Series
, Comma for( i = 0, j = 0; i < 10; i++, j++ ) ...


See also:

o Expressions and operators.
o Assignment Operators.


Top Master Index Keywords Functions


Martin Leslie