[net.lang.c] parens around sizeof arg

cottrell@nbs-vms.ARPA (02/05/85)

/*
> > (Hey, (guys), let's (not) put extra (parentheses (everywhere)))!
> > sizeof thing
> > sizeof(type)
> 
> Strictly speaking, that is correct syntax.  Still, C allows one to
> parenthesise any expression.  Because "operators" like 'sizeof' and
> 'return' are made up of letters, I like to put their operands in
> parentheses.  Purely visual (not even syntactic) sugar.		;-)
> 
> Joe Yao		hadron!jsdy@seismo.{ARPA,UUCP}

I have to agree with Joe here. I think of sizeof as a compile-time
funxion. Thus the args. Purely style. I always write `if (exp)' &
`while (exp)' but always `return(exp)'. Unless I am cheating, as in

	if (exp)			if (exp) {
		return exp;			exp;
						return;
					}

The example on the left avoids the braces while semi-documenting
the fact that. I know, you are shocked! I'm just a bad guy.
*/

Doug Gwyn (VLD/VMB) <gwyn@BRL-VLD.ARPA> (02/06/85)

sizeof is a unary op, not a function.
sizeof thing and sizeof(type) are quite different.

ksbszabo@wateng.UUCP (Kevin Szabo) (02/10/85)

In article <8048@brl-tgr.ARPA> cottrell@nbs-vms.ARPA writes:
>	if (exp)			if (exp) {
>		return exp;			exp;
>						return;
>					}
>
>The example on the left avoids the braces while semi-documenting
>the fact that. I know, you are shocked! I'm just a bad guy.

Sir, you are a sinner. I hope I never have to look at your code. ( 1/2 :-) )
The code on the left tells me that you are trying to pass back
a value to the calling routine, and I would be confused as to why
the calling routine chose to ignore it. If you insert a comment
to document the fact you are doing something odd, why not just do
it right the first time and avoid the comment? Of course you may
not put in any comments, but I amazed that you can figure out what
your code does three months after you wrote it. 

You really should be coding in B, or assembler.

					Kevin
-- 
Kevin Szabo  watmath!wateng!ksbszabo (U of Waterloo VLSI Group, Waterloo Ont.)

bsa@ncoast.UUCP (Brandon Allbery) (02/24/85)

> Article <8048@brl-tgr.ARPA>, from cottrell@nbs-vms.ARPA
+----------------
| /*
| > > (Hey, (guys), let's (not) put extra (parentheses (everywhere)))!
| > > sizeof thing
| > > sizeof(type)
| > 
| > Strictly speaking, that is correct syntax.  Still, C allows one to
| > parenthesise any expression.  Because "operators" like 'sizeof' and
| > 'return' are made up of letters, I like to put their operands in
| > parentheses.  Purely visual (not even syntactic) sugar.		;-)
| > 
| > Joe Yao		hadron!jsdy@seismo.{ARPA,UUCP}
| 
| I have to agree with Joe here. I think of sizeof as a compile-time
| funxion. Thus the args. Purely style.
| */

Style for you guys, necessity for others.  The only time I use sizeof is
in such expressions as, "sizeof(struct foo)"; and our compiler doesn't
realize that "struct" gets a following name, so "sizeof struct foo" gets
me a syntax error.

--bsa
-- 
Brandon Allbery, decvax!cwruecmp!ncoast!bsa, ncoast!bsa@case.csnet (etc.)
6504 Chestnut Road Independence, Ohio 44131 +1 216 524 1416 (or what have you)
		 -=> Does the Doctor make house calls? <=-

guy@rlgvax.UUCP (Guy Harris) (02/25/85)

> Style for you guys, necessity for others.  The only time I use sizeof is
> in such expressions as, "sizeof(struct foo)"; and our compiler doesn't
> realize that "struct" gets a following name, so "sizeof struct foo" gets
> me a syntax error.

That's a "sizeof(type)", not a "sizeof thing", and the parentheses are
necessary in that case.

	Guy Harris
	{seismo,ihnp4,allegra}!rlgvax!guy

gwyn@Brl-Vld.ARPA (VLD/VMB) (02/25/85)

Just goes to show that people don't really read these things.

g-frank@gumby.UUCP (02/25/85)

> > Style for you guys, necessity for others.  The only time I use sizeof is
> > in such expressions as, "sizeof(struct foo)"; and our compiler doesn't
> > realize that "struct" gets a following name, so "sizeof struct foo" gets
> > me a syntax error.
> 
> That's a "sizeof(type)", not a "sizeof thing", and the parentheses are
> necessary in that case.
>

   Oh, how nice.  Something else for experts in C to know so they can feel
superior to the novices.  Never have one, consistent way to use a language 
construct when you can have two to demonstrate your mastery of the arcana.


-- 
      Dan Frank

	  Q: What's the difference between an Apple MacIntosh
	     and an Etch-A-Sketch?

	  A: You don't have to shake the Mac to clear the screen.

tmb@talcott.UUCP (Thomas M. Breuel) (02/25/85)

> > That's a "sizeof(type)", not a "sizeof thing", and the parentheses are
> > necessary in that case.
> 
>    Oh, how nice.  Something else for experts in C to know so they can feel
> superior to the novices.  Never have one, consistent way to use a language 
> construct when you can have two to demonstrate your mastery of the arcana.

Well, you can think about 'sizeof' this way: it only allows you to take
the size of an object. To get the size of a type, you have to cast an
object to that type. As a notational convenience, the 'C' compiler
allows you to leave out the object that you are casting...

							Thomas.

g-frank@gumby.UUCP (02/27/85)

> > > That's a "sizeof(type)", not a "sizeof thing", and the parentheses are
> > > necessary in that case.
> > 
> >    Oh, how nice.  Something else for experts in C to know so they can feel
> > superior to the novices.  Never have one, consistent way to use a language 
> > construct when you can have two to demonstrate your mastery of the arcana.
> 
> Well, you can think about 'sizeof' this way: it only allows you to take
> the size of an object. To get the size of a type, you have to cast an
> object to that type. As a notational convenience, the 'C' compiler
> allows you to leave out the object that you are casting...
> 

I rest my case.


-- 
      Dan Frank

	  Q: What's the difference between an Apple MacIntosh
	     and an Etch-A-Sketch?

	  A: You don't have to shake the Mac to clear the screen.

henry@utzoo.UUCP (Henry Spencer) (02/27/85)

> Style for you guys, necessity for others.  The only time I use sizeof is
> in such expressions as, "sizeof(struct foo)"; and our compiler doesn't
> realize that "struct" gets a following name, so "sizeof struct foo" gets
> me a syntax error.

The compiler is correct; the syntax error is real.  Look carefully at the
definition of sizeof:  its usage is "sizeof object" or "sizeof(type)".
The parentheses are *not* optional in the second case.  This is part of
the reason why many people put the parentheses in for all sizeofs.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry

geoff@desint.UUCP (Geoff Kuenning) (03/03/85)

>> That's a "sizeof(type)", not a "sizeof thing", and the parentheses are
>> necessary in that case.
> 
>    Oh, how nice.  Something else for experts in C to know so they can feel
> superior to the novices.  Never have one, consistent way to use a language 
> construct when you can have two to demonstrate your mastery of the arcana.
 
Well, the easy answer is in Kernighan and Plaugher:  "When in doubt,
PARENTHESIZE."  A lot of people prefer to parenthesize all sizeof's as a
stylistic thing anyway.
-- 

	Geoff Kuenning
	Unix Consultant
	(213) 545-4413
	...!ihnp4!trwrb!desint!geoff

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (03/06/85)

I had always thought that
	( lvalue_expr )
would not be an lvalue_expr, but checking in the proposed ANSI spec
I see that the parentheses do not affect lvalueness, so extra parens
	sizeof(thing)
do no actual harm, although they are a bit sloppy, as is
	return(expr);
when
	return expr;
would do.

There turns out to be a real use for the unary + operator, which is
to remove lvalueness from an expression:
	+(lvalue_expr)
is not an lvalue.

ksbszabo@wateng.UUCP (Kevin Szabo) (03/08/85)

In article <8985@brl-tgr.ARPA> gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) writes:
>I see that the parentheses do not affect lvalueness, so extra parens
>	sizeof(thing)
>do no actual harm, although they are a bit sloppy, as is
>	return(expr);
>when
>	return expr;
>would do.

Why are parentheses aroung the `expr' sloppy? I had been programming
in C for quite a while before I found out that you could leave the
Parens Out!! All (or almost all) examples in K&R show parentheses
around the expressions, and this is the example that I followed.
( When in doubt, paranthesize??? ).

				Kevin
-- 
Kevin Szabo  watmath!wateng!ksbszabo (U of Waterloo VLSI Group, Waterloo Ont.)

robert@gitpyr.UUCP (Robert Viduya) (03/09/85)

><
Posted from  geoff@desint.UUCP (Geoff Kuenning)
> Well, the easy answer is in Kernighan and Plaugher:  "When in doubt,
> PARENTHESIZE."  A lot of people prefer to parenthesize all sizeof's as a
> stylistic thing anyway.
> -- 

Hear, hear.  I don't just apply this to the sizeof operator but also to
almost all my expressions.  I consider it to be a waste of my time memorizing
all the precedence rules.

				robert
-- 
Robert Viduya
Georgia Institute of Technology

...!{akgua,allegra,amd,hplabs,ihnp4,masscomp,ut-ngp}!gatech!gitpyr!robert
...!{rlgvax,sb1,uf-cgrl,unmvax,ut-sally}!gatech!gitpyr!robert

tmb@talcott.UUCP (Thomas M. Breuel) (03/13/85)

> Why are parentheses aroung the `expr' sloppy? I had been programming

Well, it is something that is semantically and syntactically
superfluous, much the same way that many PASCAL programmers like to
insert extra null statements at the end of program blocks.  The
parentheses serve at most an optical purpose.

						Thomas.

jeff@rtech.ARPA (Jeff Lichtman) (03/21/85)

> > Why are parentheses aroung the `expr' sloppy? I had been programming
> 
> Well, it is something that is semantically and syntactically
> superfluous, much the same way that many PASCAL programmers like to
> insert extra null statements at the end of program blocks.  The
> parentheses serve at most an optical purpose.
> 
> 						Thomas.

You could say the same thing about blank lines between code blocks, or spaces
around operators.  Newlines at the ends of statements are "semantically and
syntactically superfluous" too.

Remember that people (not just compilers) have to read programs.  If extra
parentheses make a piece of code more readable, then they are good.
-- 
Jeff Lichtman at rtech (Relational Technology, Inc.)
aka Swazoo Koolak

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (03/28/85)

Most C programmers I have encountered who use redundant parentheses
in return and sizeof expressions turned out not to be aware that
they were unnecessary.  If they are aware but nonetheless think that
readability is improved, more power to them.