Lesson 39 of 50 · c
C11 & C17 Modern Features
Duration: 12 mins
The C language evolves. Modern standards add safety, concurrency, and convenience.
| Feature | Header | Usage |
|---|---|---|
_Static_assert | <assert.h> | Compile‑time assertion (_Static_assert(sizeof(int)==4, "int not 4 bytes");). |
| Anonymous structs/unions | — | Allows nested structs without a name (useful for low‑level memory mapping). |
_Thread_local | <threads.h> | Declares thread‑local storage (_Thread_local int counter;). |
alignas / alignof | <stdalign.h> | Controls/alters alignment constraints. |
static_assert (macro) | <assert.h> | Same as _Static_assert but macro for pre‑C11 compilers. |
generic selections | <tgmath.h> | Type‑generic macros (generic_max(1, 2.5) resolves to proper overload). |
atomic types | <stdatomic.h> | Lock‑free atomic operations (atomic_int a; atomic_fetch_add(&a, 1);). |
aligned_alloc | <stdlib.h> | Allocate memory with specific alignment. |
📚 Example: Compile‑time Size Check
#include <assert.h>
_Static_assert(sizeof(void*) == 8, "Pointer size is not 8 bytes");