Why is sizeof considered an operator?

Why is sizeof considered an operator and not a function?

Which property needs to be qualified as an operator?

+84
c operators sizeof
Sep 08 '09 at 11:56
source share
9 answers

Since standard C says so, and it receives a single vote.

As the consequences:

  • The sizeof operand can be the sizeof (int) type enclosed in parentheses instead of the expression of the object.
  • Brackets are not needed: int a; printf("%d\n", sizeof a); int a; printf("%d\n", sizeof a); fine. They are often visible, firstly, because they are needed as part of the expression for the type expression, and secondly, because sizeof has a very high priority, so sizeof a + b does not match sizeof (a+b) . But they are not part of the sizeof call, they are part of the operand.
  • You cannot take the sizeof address.
  • An expression that is the sizeof operand is not evaluated at runtime ( sizeof a++ does not change a).
  • An expression that is the sizeof operand can be of any type except void or function types. Indeed, such a point in size.

The function will be different at all of these points. There are probably other differences between a function and a unary operator, but I think that is enough to show why sizeof cannot be a function, even if there is a reason why it wants to be.

+173
Sep 08 '09 at 11:58
source share

It can be used as a compile-time constant, which is possible only if it is an operator and not a function. For example:

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

Syntactically, if it was not an operator, then it should have been a preprocessor macro, since functions cannot accept types as arguments. This would be a difficult macro to implement, since sizeof can accept both types and variables as an argument.

+21
Sep 08 '09 at 12:02
source share

Since standard C says so, and it receives a single vote.

And the standard is probably correct because sizeof takes type and

In the general case, if either a domain or a coded (or both) function contains elements that are significantly more complex than real numbers, this function is called an operator. And vice versa, if neither the domain nor the coding function contains elements that are more complex than real numbers, this function will most likely be called simply a function. Trigonometric functions, such as cosine, are examples of the latter case.

In addition, when functions are used so often that they evolve faster or easier than notation than the general form F (x, y, z, ...), the resulting special forms are also called operators. Examples include infix operators, such as adding "+" and division "/", and postfix operators, such as the factorial "!". This use is not related to the complexity of the subjects involved.

(Wikipedia)

+5
Sep 08 '09 at 12:05
source share

Because it is not a function. You can use it like this:

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

The function has an entry point, code, etc. The function must be run at runtime (or internally), sizeof must be determined at compile time.

+4
Sep 08 '09 at 12:00
source share

Because:

  • when you pass a value to a function, the size of the object is not passed to the function, so the sizeof function has no way to determine the size
  • in C, functions can take only one type of argument; sizeof () should accept all kinds of differnet things (both variables and types! You cannot pass a function type to C)
  • a function call involves creating a copy of the arguments and other unnecessary overhead
+1
Sep 08 '09 at 12:02
source share

There is a slight difference from the function - the sizeof value is resolved at compile time, but not at run time!

+1
Sep 08 '09 at 12:02
source share

Since this is a compile-time operator , calculating the size of an object requires type information that is available only at compile time. This does not work for C ++.

+1
Sep 08 '09 at 12:08
source share

The sizeof operator is a compilation object, not executable, and does not need parentheses as a function. When the code compiles, it replaces the value with the size of this variable at compile time, but in the function, after the function is executed, then we will know the return value.

+1
Oct 27 '10 at 12:11
source share
Operator

sizeof() is the compilation time. It can be used to define parameters or arguments.

0
Aug 6 2018-12-12T00:
source share



All Articles