alignas

From cppreference.com
< c

Template:c/align/alignas/navbar

Specifies the alignment requirement of a type or an object.

Contents

[edit] Syntax

alignas( constant-expression ) (since C11)
alignas( type-name ) (since C11)

[edit] Explanation

The alignas macro may be applied to the declaration of a variable or to the definition of a struct, union or enum.

When used in form alignas(constant-expression), and constant-expression is an integral constant expression that evaluates to a positive value, the declared entity will have its alignment requirement set to exactly the result of the constant-expression, except if it would weaken the natural alignment requirement of the type.

When used in form alignas(type-name), it is exactly equivalent to alignas(alignof(type-name)), that is, the alignment requirement of the declared entity will be equal the alignment requirement of type-name.

[edit] Notes

alignas(0) has no effect.

When multiple alignas macros are applied to the same variable, the strictest one is used.

As of the ISO C11 standard, the C language has the _Alignas keyword and defines in the header <stdalign.h> the preprocessor macro alignas which expands to the keyword.

[edit] Keywords

_Alignas

[edit] Example

#include <stdio.h>
#include <stdalign.h>
 
alignas(16) char a,b;
char c;
 
int main(void)
{
    printf("%p\n", (void*)&a);
    printf("%p\n", (void*)&b);
    printf("%p\n", (void*)&c);
}

Possible output:

0x601040
0x601030
0x601031

[edit] See also

(C11)
get the alignment requirement of a type or an object
(function macro)
C++ documentation for alignas specifier