_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.

CategoryC11 features
KindKeyword / Statement
C standardC11

📝 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 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏