C Reference ยท Operators and Expressions
The sizeof operator in C
Reference for the C sizeof operator: size_t result, compile-time evaluation, unevaluated operands, VLAs, arrays versus pointers and common mistakes.
sizeof yields the size in bytes of its operand's type, as a value of type size_t. The operand is either a parenthesized type name, sizeof(int), or an expression, sizeof x. In both cases only the type matters. For an expression, the expression is not evaluated, so side effects inside it never happen.
For almost every type the result is a constant known at compile time, which makes sizeof the standard way to allocate the right amount of memory and to count array elements. See also the keyword page sizeof.
size_t bytes = sizeof(double); size_t count = sizeof table / sizeof table[0];
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
sizeof unary-expression sizeof ( type-name )
| Form | Description |
|---|---|
sizeof expr |
The size of the type of expr. No parentheses are needed around an expression, but sizeof (a + b) is fine. |
sizeof(type) |
The size of the type. Parentheses are required. |
Result: a size_t (an unsigned integer type from <stddef.h>). It is an integer constant expression unless the operand has variable length array type.
Errors / undefined behavior: it is a compile error to apply sizeof to a function type, an incomplete type (such as struct s; that is declared but not defined, or void) or a bit-field. GCC accepts sizeof(void) as 1 as an extension; do not use it. sizeof of a VLA is evaluated at run time and the VLA expression's side effects do happen.
Usage notes
sizeof(char)is 1 by definition. All other sizes are counted in chars, which areCHAR_BITbits (8 on common platforms).- Arrays and pointers differ.
sizeof arris the whole array in bytes; once an array has decayed to a pointer, for example as a function parameter, you get the pointer size (the second example above shows this). Pass the length separately. - Element count idiom:
sizeof arr / sizeof arr[0]works only for real arrays in the same scope. Wrap it in a macro if you use it often, and note that it silently gives the wrong answer for a pointer. - Prefer the expression form for allocation:
p = malloc(n * sizeof *p);stays correct when the type ofpchanges. - Structs may contain padding, so
sizeof(struct)can exceed the sum of its members. See struct padding. - Do not hard-code sizes.
intis 4 bytes and pointers are 8 on typical 64-bit systems, but the standard allows other values. Use<stdint.h>types when you need exact widths. - Printing: use
%zu. Using%dor%luis a type mismatch and undefined behavior on some platforms. - Precedence:
sizeofis a unary operator, sosizeof x + 1means(sizeof x) + 1. sizeofversusstrlen:sizeof "hello"is 6 (includes the terminator) whilestrlen("hello")is 5 and is computed at run time.