C stdalign.h Header

Beginner
6 min read
Updated: Nov 2026
3 examples
C11

What Is stdalign.h?

<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
UsePrefer
Ask how a type is alignedalignof(type)
Force a stricter alignmentalignas(N) or alignas(type)
See padding in a structsizeof with alignof
Allocate aligned heap memoryaligned_alloc()

How to Use stdalign.h in C

Query alignment with alignof and request stricter alignment with alignas.

  1. Add #include <stdalign.h> and compile with -std=c11.
  2. Use alignof(type) to get a type's alignment as a size_t.
  3. Put alignas(N) before an object or struct member, where N is a power of two.
  4. Print alignments and sizes with %zu.
  5. For heap memory use aligned_alloc instead of plain malloc.
PieceRequired?Role
#include <stdalign.h>C11Defines alignas and alignof
alignof(type)NoCompile-time alignment query
alignas(N)NoRequests 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

Cheat Sheet

Values shown for alignof are typical on 64-bit desktop platforms.

NameMeaning
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_definedBoth expand to 1 in C11
max_align_tType with the strictest fundamental alignment (stddef.h)
aligned_alloc(a, n)Allocate n bytes aligned to a (stdlib.h)

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

C Examples

Compile with gcc file.c -std=c11 -o out.

Example 1 — Query the Alignment of Types

Print the alignment of four basic types.

C
#include <stdalign.h>
#include <stdio.h>

int main(void) {
    printf("alignof(char)   = %zu\n", alignof(char));
    printf("alignof(short)  = %zu\n", alignof(short));
    printf("alignof(int)    = %zu\n", alignof(int));
    printf("alignof(double) = %zu\n", alignof(double));
    return 0;
}

How It Works

  1. One query per type. alignof(type) is evaluated at compile time.
  2. size_t result. Use %zu to print it.
  3. Platform specific. These values are typical on x86-64; other platforms may differ.

Example 2 — Align a Buffer to 16 Bytes

Request a stricter alignment than the default and verify the address.

C
#include <stdalign.h>
#include <stdint.h>
#include <stdio.h>

int main(void) {
    alignas(16) unsigned char buf[32];
    uintptr_t addr = (uintptr_t)buf;

    printf("aligned to 16: %s\n", addr % 16 == 0 ? "yes" : "no");
    return 0;
}

How It Works

  1. Request alignment. alignas(16) places buf at an address divisible by 16.
  2. Convert to an integer. uintptr_t lets you inspect the address arithmetic.
  3. Check the remainder. A multiple of 16 leaves remainder 0.

Example 3 — See Struct Padding with alignas

Compare a plain struct with one that has an over-aligned member.

C
#include <stdalign.h>
#include <stdio.h>

struct Plain {
    char c;
    int n;
};

struct Wide {
    char c;
    alignas(8) int n;
};

int main(void) {
    printf("Plain: size %zu, align %zu\n", sizeof(struct Plain), alignof(struct Plain));
    printf("Wide:  size %zu, align %zu\n", sizeof(struct Wide), alignof(struct Wide));
    return 0;
}

How It Works

  1. Natural alignment. int needs 4, so Plain pads 3 bytes after c.
  2. Over-aligned member. alignas(8) pushes n to offset 8.
  3. 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.

Performance & Portability Notes

ApproachCostBest for
alignofCompile-time constant; no run-time costQuerying layout
alignas on membersAdds padding and enlarges sizeofHardware-required boundaries
Over-aligned arraysNo run-time cost; can waste stack spaceSIMD 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.

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.

Next: stdarg.h

Write variadic functions with va_list, va_start, and va_end.

stdarg.h tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful