<stdalign.h> is the C11 header that provides the convenience spellings alignas (for _Alignas) and alignof (for _Alignof). They let you ask for and query the alignment of types and objects.
Alignment is the address multiple at which a value must be stored. A double usually needs an 8-byte boundary. Over-aligning a buffer helps SIMD code and hardware with strict boundaries. In C23 alignas and alignof become keywords.
Remember
#include <stdalign.h>
alignof(type) -> alignment in bytes (size_t)
alignas(16) char buf[64]; -> buffer aligned to 16 bytes
alignas(double) char b[8]; -> align like a double
C11: macros for _Alignas / _Alignof
C23: real keywords
Query alignment with alignof and request stricter alignment with alignas.
Add #include <stdalign.h> and compile with -std=c11.
Use alignof(type) to get a type's alignment as a size_t.
Put alignas(N) before an object or struct member, where N is a power of two.
Print alignments and sizes with %zu.
For heap memory use aligned_alloc instead of plain malloc.
Piece
Required?
Role
#include <stdalign.h>
C11
Defines alignas and alignof
alignof(type)
No
Compile-time alignment query
alignas(N)
No
Requests stricter alignment for an object or member
Pseudocode
Pseudocode
a = alignof(double) // usually 8
alignas(16) buf[32] // buf address is a multiple of 16
struct S { char c; alignas(8) int n; }
-> n placed at an offset divisible by 8
-> sizeof(struct S) grows to keep arrays aligned
Reference
Cheat Sheet
Values shown for alignof are typical on 64-bit desktop platforms.
Name
Meaning
alignof(T)
Alignment requirement of type T (a size_t)
alignas(N)
Align an object or member to N bytes (a power of two)
alignas(T)
Align like type T
__alignas_is_defined, __alignof_is_defined
Both expand to 1 in C11
max_align_t
Type with the strictest fundamental alignment (stddef.h)
aligned_alloc(a, n)
Allocate n bytes aligned to a (stdlib.h)
Trace
Worked Walkthrough — Struct Padding
See how alignas moves a member and enlarges the struct (typical 64-bit layout).
Calculation
struct Plain { char c; int n; };
c at offset 0
n at offset 4 (3 bytes of padding)
sizeof = 8, alignof = 4
struct Wide { char c; alignas(8) int n; };
c at offset 0
n at offset 8 (7 bytes of padding)
sizeof = 16, alignof = 8
Natural alignment.int needs 4, so Plain pads 3 bytes after c.
Over-aligned member.alignas(8) pushes n to offset 8.
Struct alignment follows.Wide inherits alignment 8 and is padded to a size of 16.
Edge Cases & Pitfalls
Alignment requests have a few strict rules.
pow2
Powers of two only
alignas(N) needs a valid alignment, which is a power of two. alignas(0) has no effect.
weaker
Cannot weaken alignment
You may not request a weaker alignment than the type's natural alignment.
size_t
alignof returns size_t
Print the result with %zu, not %d.
c23
Keywords in C23
C23 makes alignas and alignof keywords; the header stays valid but is no longer needed.
Analysis
Performance & Portability Notes
Approach
Cost
Best for
alignof
Compile-time constant; no run-time cost
Querying layout
alignas on members
Adds padding and enlarges sizeof
Hardware-required boundaries
Over-aligned arrays
No run-time cost; can waste stack space
SIMD loads and cache-line control
Misaligned access is undefined behavior in C and a crash on some CPUs. Let the compiler place objects, and use alignas only when you need a stricter boundary.
Remember
Key Takeaways
Role: query and request alignment with alignof and alignas.
Standard: C11 macros for _Alignof and _Alignas.
Output: print alignments and sizes with %zu.
Layout: stricter alignment can add padding to structs.
One line:stdalign.h tells you how values must sit in memory and lets you ask for a stricter boundary.
Frequently Asked Questions
stdalign.h is a C11 header that defines alignas and alignof as friendly spellings of the _Alignas specifier and _Alignof operator.
They are the same. alignas is a macro defined by stdalign.h that expands to _Alignas. In C23 alignas is a real keyword.
A size_t giving the alignment requirement of a type in bytes. Print it with %zu.
Some CPUs fault on misaligned access, and aligned data is faster. SIMD instructions and hardware buffers often require stricter boundaries.
No. C23 makes alignas and alignof keywords, so the header is no longer required, though it remains valid.