tags:

views:

109

answers:

5

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 of a function call, as in sizeof(int).

Is it supposed to be some kind of a pseudo-function?

+2  A: 

A function call is evaluated at runtime. sizeof must be evaluated at compile time (though C99 introduces some exceptions to this, I believe)

leonbloy
Unless you have a VLA...
Carl Norum
+5  A: 

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.

Oli Charlesworth
A: 

function is something you can call at runtime. sizeof is only understood by the compiler.

unbeli
+1  A: 

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

JonH
A: 

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.

sarshad