Richard said:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
Given that x is an expression (say, the name of an object), I prefer
"sizeof x" to "sizeof(x)" for the same reason that I prefer "return x;"
to "return(x);".
The parentheses make both "sizeof(x)" and "return(x);" look like
function calls, which they're not. Treating sizeof as a unary
operator emphasizes the fact that it's not a function, and more
importantly, it doesn't *act* like a function. The argument to sizeof
(assuming there are no VLAs involved) is not evaluated, and the result
doesn't depend on the value of the expression, only on its type. No
function behaves like that.
The temptation to write sizeof(x), I think, comes from two sources.
One, sizeof(type) does require parentheses, and it looks (too much,
IMHO) like a function call. If you always use parentheses, you don't
have to remember the rule, and it always works. The other is that
sizeof is the only operator specified by a keyword (which looks like
an identifier) rather than by a symbol. If the operator were
specified by, say, a "$" symbol, there'd be no temptation to write
"$(x)" rather than "$ x", any more than you'd write "!(x)", or "-(x)",
or "~(x)". (Some languages have a number of operators represented by
keywords; others use symbols for everything. I think C is unusual in
having just one keyword operator.)
Having said that, I don't strongly object to the unnecessary
parentheses in "sizeof(x)". I don't use them myself, because omitting
them demonstrates and reinforces a deeper understanding of what's
really going on, but I can certainly deal with them in code that I
read. And nobody reading C code should actually be *fooled* by
"sizeof(x)" into thinking that it's a function call.
I find the unnecessary parentheses "sizeof(x)" a bit less annoying
than the ones in "return(x);" (for no good reason I can think of).