Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

sizeof — size in bytes

The sizeof operator returns the size of a type or expression in bytes as size_t. Distinguish array size from pointer size, and remember the variable-length-array exception.

Version: C23; repository compiler flag, production runtime not attested

Syntax

sizeof unary-expression
sizeof (type-name)

sizeof is an operator, not a function. A type name must be enclosed in parentheses. Without enclosing parentheses, an expression operand must match unary-expression in the C grammar. For example, sizeof a + b parses as (sizeof a) + b, not as sizeof(a + b). The result has unsigned type size_t; print it with %zu.

sizeof(char) is 1; a C byte need not have 8 bits. Structure and union sizes include padding. Do not assume sizeof(int) is 4.

sizeof a / sizeof a[0] counts elements of an actual array, not a pointer. An array-declared parameter is adjusted to a pointer; sizeof cannot recover the supplied array's length.

Usually the operand is not evaluated and the result is constant: sizeof(i++) does not increment i. A variable-length-array (VLA) operand is the exception and is evaluated. Avoid side effects. Functions, incomplete types (including void), and bit-fields are forbidden operands.

Examples

#include <stdio.h>

int main(void)
{
    int values[] = {10, 20, 30};
    int i = 0;

    printf("char: %zu\n", sizeof(char));
    printf("elements: %zu\n", sizeof values / sizeof values[0]);
    (void)sizeof(i++);
    printf("i: %d\n", i);
    return 0;
}

Result

char: 1
elements: 3
i: 0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources