On Wed, Jan 28, 2026 at 10:08:37PM +0100, Alejandro Colomar via Mutt-dev wrote:
> Hi Derek,
>
> On 2026-01-28T15:46:08-0500, Derek Martin wrote:
>
> [...]
> > - sizeof IS NOT A FUNCTION[*]. Do not format it like one, i.e. use:
> > ✓ sizeof varname // no, doesn't need parens
> > ✓ sizeof (type name) // parens needed, but not a function call!
> > ✓ struct foo *x; size_t s = sizeof *x; // doesn't need parens or to be
> > initialized/non-null!
>
> Being a function is orthogonal to needing parentheses or not, as you
> show in your comment about type names.
No... my complaint is not just about needing parentheses. By
convention, we typically write conditionals with a space, e.g.:
if (c) ...
Whereas we write functions without, e.g.:
f();
The point mainly is about not using the latter when using sizeof
because it is not a function, and should not be thought of as one.
Its value is generally known at compile time. [Maybe always in C, vs.
C++?] Coding style should, and generally does, indicate intention by
convention; when sizeof is written like a function the intention is
wrong.
However, operators are neither. And...
> For consistency, it is better to always use parens, since you need them
> in half of the cases.
Completely disagree.
1. Consistency for its own sake is not a virtue.
2. Parens are punctuation, whether in natural language or in
programming languages (at least in a sense). Punctuation should
be used exactly when it is needed for clarity, and avoided all
other times for brevity. If you don't NEED it, it's just
clutter. [Ack. that compilers require more precision than
humans. Typically.] ;-)
3. Technically, I believe parens are required in zero cases, because
you can always rewrite the second as either the first or third
forms, which don't need them (and in fact this is the convention
I personally use). At least I can't think of a case in C where
that's not true. If you came up with one I would probably write
it a different way.
4. If you want to argue for consistency, then sizeof should never
require parens, because (at least prior to C99) no other operator
requires the use of parentheses, save one: the type cast, whose
syntax token(s) is the actual parentheses.
5. As an observer you can certainly argue convincingly that the
expression sizeof (type) is not one but TWO operators: sizeof,
and a type cast (or a unique thing akin to that--it has the exact
same syntax). I can't say what the designers' intent was, but I
would be surprised if that was not it... I've always thought that
was the idea, since I learned C in 1986.
> Operators not requiring parentheses complicate parsing in compilers and
> is a historic accident that can't be removed
I doubt that--I imagine it was quite intentional on language
designers' part that operators, generally, be written exactly as they
are in mathematics, without requiring parentheses, whenever possible
(allowing for symbolic substitutions for certain operations, which
happens in mathematics as well, anyway). We want to write x = a + 1
because that's how we'd write it algebraically. Likewise, we want to
write functions as f() because we write them that way in mathematics.
I confess, my compilers knowledge is very rudimentary, but I rather
suspect that the complication with sizeof is that it operates on a C
data type, which in C can only happen in a very limited number of
contexts, most of which are not numerical or mathematical, sizeof
being the main (if not only) exception, which must be distinguished
from all the other places types can occur... or something along those
lines. Regardless, to that I would say that the compiler is where the
complexity should exist, not the user interface (i.e. the
human-readable syntax).
The history is interesting enough...
--
Derek D. Martin http://www.pizzashack.org/ GPG Key ID: 0xDFBEAD02
-=-=-=-=-
This message is posted from an invalid address. Replying to it will result in
undeliverable mail due to spam prevention. Sorry for the inconvenience.