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 _Alignof operator is a unary operator introduced in C11 that evaluates to the alignment requirement of a specified type. It returns the number of bytes representing the boundary on which an object of the given type must be allocated in memory.
_Alignof(type-name)

Technical Specifications

  • Return Type: The operator evaluates to an integer constant expression of type size_t (defined in <stddef.h>).
  • Operand: The operand must be a parenthesized type name. Unlike the sizeof operator, _Alignof cannot accept an expression as its operand.
  • Evaluation: The result is determined entirely at compile-time.

Type-Specific Behavior

  • Scalar Types: Returns the architecture-specific alignment boundary for the type (e.g., _Alignof(int) typically returns 4).
  • Array Types: When applied to an array type, _Alignof evaluates to the alignment requirement of the array’s underlying element type, not the alignment of the entire array block.
  • Struct and Union Types: When applied to a struct or union, it evaluates to the strictest (largest) alignment requirement among all of its members.

Constraints and Restrictions

The _Alignof operator will result in a compilation error if applied to:
  1. Function types.
  2. Incomplete types (such as forward-declared structs without a defined body).
  3. The void type.

Standard Library Integration

While _Alignof is the native keyword, the C standard library provides a convenience macro in the <stdalign.h> header:
#include <stdalign.h>

// 'alignof' expands directly to '_Alignof'
size_t alignment = alignof(int); 
Note: In the C23 standard, alignof becomes a native keyword, making the inclusion of <stdalign.h> obsolete for this purpose, though _Alignof remains valid for backward compatibility.

Syntax Visualization

#include <stddef.h>

struct MixedData {
    char a;      // Typically requires 1-byte alignment
    double b;    // Typically requires 8-byte alignment
};

int main(void) {
    // Evaluates to 1
    size_t char_align = _Alignof(char);       
    
    // Evaluates to the alignment of 'int' (e.g., 4)
    size_t array_align = _Alignof(int[10]);   
    
    // Evaluates to the strictest member alignment (e.g., 8 for 'double')
    size_t struct_align = _Alignof(struct MixedData); 

    return 0;
}
Master C with Deep Grasping Methodology!Learn More