Why is sizeof
considered an operator and not a function?
What property is necessary to qualify as an operator?
Because the C standard says so, and it gets the only vote.
As consequences:
sizeof (int)
, instead of an object expression.int a; printf("%d\n", sizeof a);
is perfectly fine. They're often seen, firstly because they're needed as part of a type cast expression, and secondly because sizeof has very high precedence, so sizeof a + b
isn't the same as sizeof (a+b)
. But they aren't part of the invocation of sizeof, they're part of the operand.sizeof a++
does not modify a).A function would differ on all those points. There are probably other differences between a function and a unary operator, but I think that's enough to show why sizeof could not be a function even if there was a reason to want it to be.