_Alignof operator

Queries the alignment requirement of its operand type.

Syntax

_Alignof( type-name ) (since C11)

This operator is typically used through the convenience macro alignof, which is provided in the header stdalign.h.

Explanation

Returns the alignment requirement of the type named by type-name. If type-name is an array type, the result is the alignment requirement of the array element type. The type-name cannot be function type or an incomplete type.

The result is an integer constant of type size_t.

The operand is not evaluated (so external identifiers used in the operand do not have to be defined).

Notes

The use of alignof with expressions (as in C++) is allowed by some C compilers as a non-standard extension.

Keywords

_Alignof.

Example

#include <stdio.h>
#include <stddef.h>
#include <stdalign.h>
 
int main(void)
{
    printf("Alignment of char = %zu\n", alignof(char));
    printf("Alignment of max_align_t = %zu\n", alignof(max_align_t));
    printf("alignof(float[10]) = %zu\n", alignof(float[10]));
    printf("alignof(struct{char c; int n;}) = %zu\n",
            alignof(struct {char c; int n;}));    
}

Possible output:

Alignment of char = 1
Alignment of max_align_t = 16
alignof(float[10]) = 4
alignof(struct{char c; int n;}) = 4

References

  • C11 standard (ISO/IEC 9899:2011):
    • 6.5.3.4 The sizeof and _Alignof operators (p: 90-91)

See also

a type with alignment requirement as great as any other scalar type
(typedef)
_Alignas specifier sets alignment requirements of an object (since C11)
C++ documentation for alignof operator
doc_C_Language
2016-10-10 18:36:50
Comments
Leave a Comment

Please login to continue.