2

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?

Community
  • 1
  • 1

5 Answers5

6

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.

Oliver Charlesworth
  • 260,367
  • 30
  • 546
  • 667
4

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

leonbloy
  • 69,336
  • 20
  • 133
  • 185
3

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
  • 331
  • 1
  • 8
2

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

unbeli
  • 28,007
  • 5
  • 54
  • 55
1

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
  • 31,954
  • 11
  • 81
  • 141