Unnecessary parentheses (Was: Help: VAX C problem)

Stanley Friesen sarima at tdatirv.UUCP
Sat Apr 20 03:11:28 AEST 1991


In article <3176 at oucsace.cs.OHIOU.EDU> sadkins at oucsace.cs.OHIOU.EDU (Scott W. Adkins) writes:
<  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.

These books are making a (very poor) attempt to explain what the Turbo C
books explain correctly below.  They are *wrong*: sizeof is *always*
an operator, period.

[The idea is that a 'function' requires parens, but an 'operator' does not].

< 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.

Quite correctly, since there is no 'function sizeof'.  What is happening here
is that the operator sizeof can take either an expression or a 'type cast'
as its argument (it is a unary operator).  [Essentially an abstract type
name must be enclosed in parens anyewhere it is used.]

Thus the parens after a sizeof always belong to the argument *not* to
the operator.

<   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...).

Turbo C is right, parens are only required around an abstract type name.
An expression may be used without any parens (but since any expression is
unchanged by adding parens they harm nothing).

I also use parens after all sizeof operators, more for readability than
for any other reason.

<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))

This is a little more than I usually do.  However the outer parens are often
a good idea, since the precedence of ?: is so low.
-- 
---------------
uunet!tdatirv!sarima				(Stanley Friesen)



More information about the Comp.lang.c mailing list