+ 3
which operator has higher precedence / or * in c++
arithmetical operator
4 Réponses
+ 13
@Wen Qin
Yes I am aware, but since we are talking about C++ here, and modern compilers are pretty consistent, I didn't think I want to confuse new learners by telling them that their usual on-paper arithmetic would cause undefined behaviour. They will get to that eventually. :>
+ 11
They have the same level of precedence and are evaluated from left to right. E.g.
6*5/5 is the same as 5/5*6
+ 6
@Hatsy
Just saying, they are NOT evaluated from left to right. In that case, they are evaluated based on what the system wants to evaluate first. (Mainly based on the compiler you are using, etc)
As the system 'detects' the result to be the same, hence it just "randomly" picks one operator, and do it first.
This is also the reason why sometimes undefined behavior occurs, when 2 operators/functions/etc have the same precedence, but provide different results if one is evaluated over another and vice versa.
If they were evaluated from the left to right, there wont be so much undefined behaviors anymore. :V
But yes, most IDE/Compiler would tell your code to evaluate from left-to-right, but otherwise, it will just do it randomly.
+ 3
👍👍