Unnecessary parentheses (Was: Help: VAX C problem)
Scott W. Adkins
sadkins at oucsace.cs.OHIOU.EDU
Tue Apr 9 11:46:39 AEST 1991
In article <1#.gqcm at rpi.edu> xor at aix01.aix.rpi.edu (Joseph Schwartz) writes:
>
>Is this true? The grammar in K&R1 (pg 218) does not require parens in the
>return statement. However, the example on page 68 (and indeed all of the
>examples in the book) show the parentheses around the expression.
>
When I learned C, I was taught that parens were unecessary, but it was
highly recommended to use them for the same reason parens were to be used
with macros (i.e. #define). I have used them ever since and have grown
quite attached to them. Anymore, the program just would not look right
if I leave the parens out. Oh well :-)
>Another place I tend to see unnecessary parens is with the sizeof
>operator. You can use "sizeof (typename)" or "sizeof expression"...
>I often see "sizeof (expression)"...usually there's no space between
>the sizeof and the left paren, which may reinforce the belief that
>the parens "belong to" the sizeof operator.
>
Let us clarify the sizeof operator/function a little bit more. I cannot
say that this is strictly K&R, because I can't. I do not have access to
such manuals (or am too lazy to find out I have access to them). In many
of the C books, particulary Microsoft, they make a distinction between
the sizeof operator and the sizeof function. Unfortunatly, they do not
mention the difference and do not tell you that parens are needed or not,
they just use them. In some of the Turbo C books, a distinction is made
and described very clearly. Quoting from "Using Turbo C" by Herbert Schildt,
"Turbo C includes the compile-time operator called sizeof that returns the
size of the variable of type that is its operand. The keyword sizeof precedes
the operand's variable or type name. If sizeof operates on a data type, then
the type must appear in parentheses."
In this case, they do not indicate that one is a function and the other is
an operator. Would someone please help me out here. Is this specific to
IBM PC's or is it part of standard C? I am rather curious myself. I have
always used the parens because I have never been quite sure when it was safe
to not use them (although, now I do...).
>Just curious...how often do you folks purposely insert unnecessary parens
>into expressions? Sometimes I'll do it just to make the grouping clearer
>(just in case the next person to read the code doesn't know all the
>precedence rules by heart). I'm talking about expressions in general,
>not just in conjunction with return or sizeof.
>
I use parens like they were going out of style. Another case that parens are
more likely to be used, in my case anyway, is the shorthand conditional
statements:
variable = conditional ? then_part : else_part
usually becomes something like:
variable = (conditional ? (then_part) : (else_part))
-------------------------------------------------------------------------------
Scott W. Adkins Internet: sadkins at oucsace.cs.ohiou.edu
~~~~~~~~~~~~~~~ ak323 at cleveland.freenet.edu
(Flame me, not the net!) Bitnet: cs823 at ouaccvmb.bitnet
-------------------------------------------------------------------------------
You have seen the number of errors you get when writing your program. When you
consider that the large quantity of your program is yet to be written, be very
thankful that 90% of your errors are not in the part you have so far written.
More information about the Comp.lang.c
mailing list