_Alignas C11 alignment specifier
_Alignas C11 alignment specifier
C11 _Alignas accepts a constant expression or type name to request valid alignment for an object or struct member without weakening its existing alignment.
| Category | C11 features |
|---|---|
| Kind | Keyword / Statement |
| C standard | C11 |
📝 Syntax
_Alignas(16) char buffer[64];
_Alignas(double) unsigned char storage[sizeof(double)];
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
alignment remainder = 0
ℹ️ Info The example includes stdint.h and converts through void * to uintptr_t rather than treating the pointer directly as size_t. GCC 10.2 supports 16-byte alignment and provides uintptr_t, so the address modulo 16 is reliably 0.
🏷️ Related C11 features
📚 Related Tutorials
❓ FAQ
QCan _Alignas request weaker alignment than an object's type originally requires?
ANo. Such a request violates the constraints. It is used to request a valid, stricter alignment supported by the implementation; when multiple specifiers are present, the strictest applies.
QDoes _Alignas(16) change a char object's size to 16?
ANo. An alignment specifier constrains the object's address, not its type or sizeof. Array and structure sizes still follow their own type-layout rules. A member's requested alignment can nevertheless affect its enclosing structure's offsets, padding, and sizeof.
QWhy use uintptr_t rather than size_t to check pointer alignment?
AWhen provided, uintptr_t is intended for round-trip conversion between object pointers and unsigned integers. size_t only guarantees that it can represent object sizes, not pointer values. Taking the remainder after conversion is simply a way to inspect the address.