Unnecessary parentheses (Was: Help: VAX C problem)
Stephen Clamage
steve at taumet.com
Wed Apr 17 01:06:01 AEST 1991
sadkins at oucsace.cs.OHIOU.EDU (Scott W. Adkins) writes:
>Let us clarify the sizeof operator/function a little bit more. I cannot
>say that this is strictly K&R, because I can't....
>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?
In the ANSI C specification (section 3.3.3 and 3.3.3.4), sizeof is called
a unary operator. Its operand is either a unary-expression or a type-name
in parentheses. "unary-expression" and "type-name" are further defined
elsewhere in the grammar. Among other kinds of expressions, any
expression enclosed in parens is a unary-expression, so you can always
use parens. For example:
int i;
char c;
sizeof c + i /* A */
sizeof (c) + i /* B */
sizeof (c + i) /* C */
The values of A and B are the same, since sizeof operates on c. The
result of sizeof on a char type is guaranteed to be 1 by the language.
In C, however, the entire expression (c+i) is the operand of sizeof,
the type of the expression is int, and the result of sizeof on an int
is whatever the implementation says it is, typically 2 or 4.
Now consider:
sizeof(char) /* guaranteed to be 1 */
sizeof char /* syntax error */
sizeof( (int*)[10] ) /* array of 10 pointers to int */
For types, the parens are required as part of the syntax, independent
of whatever parens the type description may require.
--
Steve Clamage, TauMetric Corp, steve at taumet.com
More information about the Comp.lang.c
mailing list