sizeof Query object or type size
sizeof Query object or type size
sizeof returns byte counts as size_t without array decay. It is usually compile-time; C99 VLAs may evaluate at runtime, and malloc sizes are unavailable.
| Category | Operators |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
sizeof(int) /* Or */ sizeof variable
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
elements = 10
char bytes = 1
ℹ️ Info This C89 example casts size_t results to unsigned long and prints them with %lu. The array has exactly 10 elements and char has size 1, so its output has no platform variation. C99 variable-length arrays are the exception to compile-time sizeof evaluation.
🏷️ Related operators
📚 Related Tutorials
❓ FAQ
QDoes sizeof on a function's array parameter return the whole array size?
ANo. An array parameter in a function declaration is adjusted to a pointer, so sizeof returns the pointer size. Pass the element count separately, or calculate it earlier in the scope of the actual array.
QIs the expression inside sizeof never evaluated?
AAn expression whose type is not variably modified is normally only typed, not evaluated, but C99 variable-length arrays are an exception: determining their size at runtime may evaluate the operand. Do not rely on side effects here.
QCan sizeof determine how many elements malloc allocated?
ANo. Applying sizeof to the pointer returned by malloc yields only the pointer's own size, so the program must store the allocation length separately. It works for a static array because the array type contains its complete bound information.