Code: Select all
a + b ? c : d +
f + g
is not the same as
a + (b ? c : d) +
f + g
Don't ask me what happens if + would be &&.
Moderators: hgm, Rebel, chrisw
Code: Select all
a + b ? c : d +
f + g
is not the same as
a + (b ? c : d) +
f + g
Try using this:Henk wrote:Just found a bug. Maybe I posted it before.
I think I will always place parenthesis when using ?: in the future for this is certainly not the first time.Code: Select all
a + b ? c : d + f + g is not the same as a + (b ? c : d) + f + g
Don't ask me what happens if + would be &&.
Code: Select all
Operator Preceedence in C (C++ removed)
(3)---------------------------------- (9)----------------------------------
++ increment == equal to
-- !=
+ unary plus (10)---------------------------------
- & bitwise AND
! logical NOT (11)---------------------------------
~ bitwise NOT ^ bitwise XOR
(type) cast (12)---------------------------------
* indirection, not multiply |
& address of (13)---------------------------------
sizeof &&
new (14)---------------------------------
delete ||
(5)---------------------------------- (15)---------------------------------
* multiplication ?:
/ (16)---------------------------------
% modulo = direct assignment
(6)---------------------------------- +=
+ addition -=
- *=
(7)---------------------------------- /=
<< bitwise left shift %=
>> <<=
(8)---------------------------------- >>=
< less than &=
<= ^=
> |=
>= (18)---------------------------------
, comma
-------------------------------------
{ ++ -- + - ! ~ (type) * & sizeof new delete }
{ * / % }
{ + - }
{ << >> }
{ < <= > >= }
{ == != }
{ & } { ^ } { | } { && } { || }
{ = += -= *= /= %= <<= >>= &= ^= |= }
Code: Select all
a + b ? c : d +
f + g
is the same as
( a + b) ? c : (d +
f + g )
Yes, both + and &&, || has higher precedence than ?:Henk wrote:Ok this means
Am I right ? And the same holds if + would be && or ||Code: Select all
a + b ? c : d + f + g is the same as ( a + b) ? c : (d + f + g )
Code: Select all
if (a & b == c)
Code: Select all
if ((a & b) == c)
You need to study the operator precedence chart for C, then this ceases to be a trap.Henk wrote:Just found a bug. Maybe I posted it before.
I think I will always place parenthesis when using ?: in the future for this is certainly not the first time.Code: Select all
a + b ? c : d + f + g is not the same as a + (b ? c : d) + f + g
Don't ask me what happens if + would be &&.
Expressions with many brackets are difficult to edit. Best is to split expressions. But I don't know if compiler will optimize them.kbhearn wrote:Precedence is easy, always use brackets where it's not obvious - makes things more readable anyways.
&& and || are the really fun ones. in order to support forms like:
if (ptr && (*ptr > 0)) { ... }
&& actually guarantees that the right side of the operator will not be evaluated at all if the left side is false. This of course could be confusing as heck if you're doing a long list of function calls and want to check if they all return true and then wonder why the last one never gets called