Why is sizeof considered an operator?

COperatorsSizeof

C Problem Overview


Why is sizeof considered an operator and not a function?

What property is necessary to qualify as an operator?

C Solutions


Solution 1 - C

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 - C

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 - C

> 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 - C

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 - C

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.

Solution 6 - C

Because:

  • when you pass a value to a function, the size of the object is not passed to the function, so a sizeof "function" would have no way of determining the size
  • in C, functions can only accept one type of argument; sizeof() needs to accept all sorts of differnet things (variables as well as types! You can't pass a type to a function in C)
  • calling a function involves making a copy of the arguments and other unnecessary overhead

Solution 7 - C

There is small diference from function - value of sizeof is resolved on compile time, but not at runtime!

Solution 8 - C

Because it is a compile-time operator that, in order to calculate the size of an object, requires type information that is only available at compile-time. This doesn't hold for C++.

Solution 9 - C

sizeof() operator is a compile time would be occurrence. It can be used to determine the parameters or arguments.

Solution 10 - C

Sizeof(), I think obviously it's both a function and an operator. Why? Because a function holds parentheses for entry at the stage of entry. But mainly also an operator cause operators are action character, therefore sizeof is an action statement that acts on the operand in the parentheses.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArpitView Question on Stackoverflow
Solution 1 - CSteve JessopView Answer on Stackoverflow
Solution 2 - CJohn KugelmanView Answer on Stackoverflow
Solution 3 - CDaniel BrücknerView Answer on Stackoverflow
Solution 4 - CMichał GórnyView Answer on Stackoverflow
Solution 5 - CDeepak Kumar 'SORTED'View Answer on Stackoverflow
Solution 6 - CArteliusView Answer on Stackoverflow
Solution 7 - CDewfyView Answer on Stackoverflow
Solution 8 - CJoão SilvaView Answer on Stackoverflow
Solution 9 - CganeshView Answer on Stackoverflow
Solution 10 - CEnoch Asanda HallView Answer on Stackoverflow