sizeof: Operator or Function? [duplicate]
Possible Duplicate:
Why is sizeof an operator?
Why is sizeof
supposed to be an operator in C, & not a function?
Its usage seems similar to that o开发者_如何转开发f a function call, as in sizeof(int)
.
Is it supposed to be some kind of a pseudo-function?
Because it's not a function; it's not possible to write a function that can take a type as an argument and return its size! Note also that sizeof
is implemented at compile-time, and that the parentheses aren't necessary if you're using it on a variable.
A function call is evaluated at runtime. sizeof
must be evaluated at compile time (though C99 introduces some exceptions to this, I believe)
A function in C cannot do what sizeof needs to do.
A function by definition is allocated space at runtime and can operate on data only.
While sizeof operates on datatypes as well, which are compiler specific. A function does not know how to interpret the datatype "int" and ask the compiler for its size, by design.
Before C99 sizeof was totally compile-time, but in the new standard, it can be used to get information at runtime as well, for variable-length arrays.
function is something you can call at runtime. sizeof is only understood by the compiler.
sizeof has its effects at complite time not execution time. You only need the ( ) when you enter a type such as sizeof(int) but if 'i' is of type int you could do sizeof i
精彩评论