Why is sizeof considered an operator?

24,951

Solution 1

Because the C standard says so, and it gets the only vote.

As consequences:

  • The operand of sizeof can be a parenthesised type, sizeof (int), instead of an object expression.
  • The parentheses are unnecessary: 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.
  • You can't take the address of sizeof.
  • The expression which is the operand of sizeof is not evaluated at runtime (sizeof a++ does not modify a).
  • The expression which is the operand of sizeof can have any type except void, or function types. Indeed, that's kind of the point of sizeof.

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.

Solution 2

It can be used as a compile-time constant, which is only possible if it's an operator rather than a function. For instance:

union foo {
    int i;
    char c[sizeof(int)];
};

Syntactically if it weren't an operator then it would have to be a preprocessor macro since functions can't take types as arguments. That would be a difficult macro to implement since sizeof can take both types and variables as an argument.

Solution 3

Because the C standard says so, and it gets the only vote.

And the standard is probably correct because sizeof takes a type and

In general, if either the domain or codomain (or both) of a function contains elements significantly more complex than real numbers, that function is referred to as an operator. Conversely, if neither the domain nor the codomain of a function contain elements more complicated than real numbers, that function is likely to be referred to simply as a function. Trigonometric functions such as cosine are examples of the latter case.

Additionally, when functions are used so often that they have evolved faster or easier notations than the generic F(x,y,z,...) form, the resulting special forms are also called operators. Examples include infix operators such as addition "+" and division "/", and postfix operators such as factorial "!". This usage is unrelated to the complexity of the entities involved.

(Wikipedia)

Solution 4

Because it's not a function. You can use it like that:

int a;
printf("%d\n", sizeof a);

Function does have entry point, code, etc. Function is to be run at runtime (or inlined), sizeof has to be determined at compile-time.

Solution 5

sizeof operator is compile time entity not runtime and don't need parenthesis like a function. When code is compiled then it replace the value with the size of that variable at compile time but in function after function gets execute then we will know the returning value.

Share:
24,951

Related videos on Youtube

Arpit
Author by

Arpit

Updated on July 08, 2022

Comments

  • Arpit
    Arpit almost 2 years

    Why is sizeof considered an operator and not a function?

    What property is necessary to qualify as an operator?

  • crashmstr
    crashmstr over 14 years
    Wow, just what I was thinking!
  • Jonathan Leffler
    Jonathan Leffler over 14 years
    Except for VLA - variable length array - arguments.
  • Jonathan Leffler
    Jonathan Leffler over 14 years
    +1 but note that it is not a compile-time constant when the argument is a VLA - variable length array.
  • Aaron McDaid
    Aaron McDaid about 9 years
    I believe things are more complex nowadays due to variable-length arrays (VLA). IIRC, the standard would even allow sizeof to have side effects if there is a VLA in the expression.
  • anatolyg
    anatolyg over 7 years
    @glglgl No, that doesn't make any sense. In that context, (int) is nothing fancy - just a name of a type inside parentheses. Parentheses here are a part of the syntax of sizeof - they are required when taking the size of a type, but not required when taking the size of an expression. See e.g. here
  • glglgl
    glglgl over 7 years
    @anatolyg My comment was long ago, I suppose I was trying to be sarcastic then.
  • Jonathan Leffler
    Jonathan Leffler almost 7 years
    The standard uses two notations for sizeof: sizeof unary-expression and sizeof ( type-name ) — so in the C11 standard it is not deemed to be a 'cast' but a parenthesized type name. The net result is much the same. (For comparison, a cast expression is ( type-name ) cast-expression.) And I hate the way that comment Markdown works differently from Q&A Markdown!
  • phuclv
    phuclv over 2 years
    The standard already states clearly that sizeof is an operator
  • Ayxan Haqverdili
    Ayxan Haqverdili over 2 years
    printf("%d\n", sizeof a) is not perfectly fine. %d is the wrong specifier. It has undefined behavior.