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.

CategoryOperators
KindKeyword / Statement
C standardC89 / 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 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏