Operator precedence

Hi

I was looking up operator precedence on wikipedia for my current project of making an interpreter and it says that in C and C++ the operators <, <=, >, >= have a higher precedence than == and !=.
They are all comparison operators so I find it strange that equality and inequality should be compared later. Could someone explain this please?

Thanks
My opinion on this:

!= and == naturally seem like they should be in the "middle" of a multi-part expression, moreso than <, etc. Giving them a lower priority means that they'll always be applied to the largest chunk of code on either side reasonable.

From memory, only the logical operations (&&, ||) have lower priority...

I don't know if that makes any sense. It was probably a coin toss early in C development, as leaving them both at the same priority would mean people would need to be more careful and use parenthesis more to get their desired result, whereas as it is now the behavior is better defined.
Consider: if( a > b == true ) { /*...*/ }
Evaluating if( a > b == true ) { /*...*/ } left-to-right it doesn't matter what the precedence is, but if that is changed to if( true == a > b ) { /*...*/ } then it should work out the same.

So yeah, I can see now there is a good reason.
Last edited on
but if that is changed to [...]

Precisely.
Topic archived. No new replies allowed.