_Alignof C11 alignment query

_Alignof C11 alignment query

C11's _Alignof returns a complete type's alignment as size_t for type names, not expressions. Arrays yield element alignment; values are ABI-dependent.

CategoryC11 features
KindKeyword / Statement
C standardC11

📝 Syntax

_Alignof(double)  /* Usually 8 */

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

alignof(double) = 8
alignof(char) = 1
ℹ️ Info alignof(double) = 8 is the output of the common x86-64 GCC 10.2 ABI, while alignof(char) = 1 follows from the basic type's alignment rules. double alignment depends on the target ABI and should not be hard-coded as a portable constant.

📚 Related Tutorials

❓ FAQ

QWhy doesn't _Alignof(double) necessarily return 8?
AAlignment is determined by the implementation and target ABI, not by double's object size. Even if double occupies 8 bytes, some 32-bit ABIs require only 4-byte alignment.
QDo _Alignof and sizeof query the same thing?
ANo. sizeof reports how many bytes an object occupies, while _Alignof reports the alignment in bytes required for its starting address. An 8-byte type might have alignment 4, 8, or another value.
QCan a variable name be written directly after _Alignof?
AStandard C11 requires a type name in parentheses, not an arbitrary expression. To query an object's type, write that type or use a standard mechanism such as _Generic; typeof is a compiler extension.
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%

🙏 帮我们做得更好

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

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