Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c Subject: Re: Unnecessary parentheses (Was: Help: VAX C problem) Message-ID: <672@taumet.com> Date: 16 Apr 91 15:06:01 GMT References: <4072.27f7215c@iccgcc.decnet.ab.com> <1991Apr1.203600.15721@zoo.toronto.edu> <1#.gqcm@rpi.edu> <3176@oucsace.cs.OHIOU.EDU> Organization: Taumetric Corporation, San Diego Lines: 40 sadkins@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@taumet.com