Unnecessary parenthesis
David Goodenough
dg at lakart.UUCP
Sat Jul 9 05:27:16 AEST 1988
>From article <326 at marob.MASA.COM>, by daveh at marob.MASA.COM (Dave Hammond):
> Is different code produce by the compiler for "return n" and "return(n)" ?
>
> How about "if (x>1 && y<2)" and "if ((x>1) && (y<2))" ? Do unnecessary
> parenthesis generate more code ?
These two expressions *_SHOULD_* generate the same code on any decent
self respecting compiler (They do even on BDS C 1.43 under CP/M :-). It
is worth noting that this is a weakness of RD parsers: the amount of
work incurred by unnecessary parentheses is quite considerable, especially
in a language such as C, with 15 levels of operator precedence. As for
LR parsers I don't know - I have not studied them to the same degree. The
reason why people tend to parenthesize where it is not needed is to
increase clarity. Quick - do these two mean the same:
if ((a == 1 && b == 2) || (c == 3 && d == 4))
if (a == 1 && b == 2 || c == 3 && d == 4)
In the first case, my intention is much clearer (to myself included).
I believe (as do many others) that parentheses serve a double function:
overriding the normal precedence of operators, AND making things readable.
--
dg at lakart.UUCP - David Goodenough +---+
| +-+-+
....... !harvard!cca!lakart!dg +-+-+ |
+---+
More information about the Comp.lang.c
mailing list