Operator Precedence

You may recall from high school arithmetic that an expression like 5 + 3 * 9, is calculated as though it were written 5 + (3 * 9), not (5 + 3) * 9; that is, you have to perform the * before the +, so that correct result is 32, not 72, as it would be under the latter interpretation. The reason for performing the operations in the former order is that multiplication has a higher precedence than addition. Well, every operator in C++ also has a precedence that determines the order of application of each operator in an expression with more than one operator. This seems like a good idea at first glance, since after all, arithmetic does follow precedence rules like the one we just saw. Unfortunately, C++ is just a little more complicated ...

Get Learning to Program in C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.