alignas - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# alignas

From cppreference.com
< c

Template:c/align/alignas/navbar

Specifies the alignment requirement of a type or an object.

| This section is incomplete Reason: this is a copy of the C++ reference page with minor modifications, it needs to be re-written for C

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

Run this code

    #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

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

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c/align/alignas&oldid=72394"
Category:

- Todo with reason