> ## 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.

# C Sizeof

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.

```c theme={"dark"}
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.

```c theme={"dark"}
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.

```c theme={"dark"}
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.

```c theme={"dark"}
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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
