Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The sizeof operator is a built-in unary operator that yields the size, in bytes, of its operand. In C, a “byte” is strictly defined as the amount of memory required to store a single char object, guaranteeing that sizeof(char) always evaluates to 1.

Syntax

The operator accepts two types of operands: a parenthesized type name or an expression.
sizeof(type-name)
sizeof expression
sizeof(expression) 
Note: Parentheses are mandatory when the operand is a type name (e.g., sizeof(int)). When the operand is an expression, parentheses are syntactically optional but commonly used for precedence clarity.

Return Type

The result of a sizeof operation is an unsigned integer type defined as size_t. This type is implementation-defined but guaranteed to be large enough to represent the size of the largest possible object on the target architecture. It is declared in the <stddef.h> header.

Evaluation Semantics

Compile-Time Evaluation For all standard data types, pointers, structures, and fixed-size arrays, sizeof is evaluated entirely at compile time. It is treated as an integer constant expression and does not incur runtime overhead. Unevaluated Context When the operand is an expression of a non-Variable Length Array type, the compiler determines the size based solely on the type of the expression’s result. The expression itself is placed in an unevaluated context. Consequently, no side effects within the expression are executed.
int a = 5;
size_t size = sizeof(a++); 
// 'size' receives the size of an int.
// 'a' remains 5; the increment is never executed.
Runtime Evaluation (Variable Length Arrays) If the operand has a Variable Length Array (VLA) type, introduced in C99, the size cannot be determined at compile time, and the sizeof operator is evaluated at runtime. Crucially, the C standard dictates that if the operand has a VLA type, the operand itself is evaluated. This means any side effects present within the operand expression will execute. If the operand is a VLA type-name, the size expression within the brackets is evaluated. However, applying sizeof to an already-declared VLA variable evaluates the variable itself, but does not re-evaluate the size expression from its original declaration.
int n = 10;
int vla[n];

// 1. VLA variable
size_t s1 = sizeof(vla); 
// Evaluated at runtime. The operand 'vla' is evaluated, yielding the array.
// No side effects occur. The original 'n' from the declaration is NOT re-evaluated.

// 2. Expression with a VLA type
int (*p)[n] = &vla;
size_t s2 = sizeof(*p++); 
// The expression '*p++' yields a VLA type. 
// The operand IS evaluated at runtime; the side effect (p++) executes.

// 3. VLA type-name
size_t s3 = sizeof(int[n++]); 
// The operand is a VLA type-name. 
// Evaluated at runtime; the side effect within the size expression (n++) executes.

Structural Padding and Alignment

When applied to aggregate types (struct or union), the value returned by sizeof includes the total memory footprint of the object. This encompasses all members as well as any internal or trailing padding bytes inserted by the compiler to satisfy architecture-specific memory alignment requirements.
struct Example {
    char a;    // 1 byte
               // 3 bytes of padding (architecture dependent)
    int b;     // 4 bytes
};
// sizeof(struct Example) typically yields 8, not 5.

Operand Constraints

The sizeof operator violates compilation constraints if applied to the following:
  1. Incomplete types: Types lacking a complete definition, such as void or forward-declared structures without a defined body.
  2. Function types: While you can evaluate the size of a pointer to a function, you cannot evaluate the size of a function itself.
  3. Bit-fields: It is illegal to apply sizeof directly to a struct member defined as a bit-field, as bit-fields are not guaranteed to occupy standard byte boundaries.
Master C with Deep Grasping Methodology!Learn More