vade retro Pascal - redundant parentheses revisited

Werner Almesberger werner at
Tue Oct 22 15:52:33 EDT 2013

kyak wrote:
> A nice example (for 16-bit implementation) is given in MISRA-C:2004,
> Rule 12.1:

Yeah, things get tricky if sizeof(int) == sizeof(int16_t).

> d = (a + b) + c; /* d is 9; a + b wraps modulo 65536 */
> The guideline says:
> "In addition to the use of parentheses to override default operator
> precedence, parentheses should
> also be used to emphasise it.

I think they're on very dangerous ground there and what they say
doesn't seem to fit the example:

The "plain" version

	d = a + b + c;

is equivalent to "emphasized precedence"

	d = (a + b) + c;

which is the case where the unexpected happens (or not, depending on
the platform). To make it portable, you need a cast:

	d = (uint16_t) (a + b) + c;

In this case, the parentheses are already required for the cast, so
"emphasizing precedence" doesn't even enter the picture.

There are situations where putting redundant parentheses has a high
probability of improving understandability, e.g., because of an
unusual combination of operators, but this example is just not one
of them.

> /* this example also deviates from several other rules */

Good :)

- Werner

More information about the discussion mailing list