Modern C: C11, C17 & C23 features
8 min lesson
- Modern C refers to the newer standards after C99, mainly C11, C17 and C23, which add safer and more expressive features while staying close to classic C.
- These features matter because they reduce bugs and make code clearer without leaving the C language.
- Think of modern C as the same toolbox you know, with a few new tools that make old jobs easier and safer.
- C11 added _Static_assert, _Generic, threads.h, anonymous structs and unions, and alignas for alignment control.
- C17 is a small bug-fix release; it mainly clarifies the standard and adds no big features.
- C23 adds typeof, constexpr, nullptr, auto type inference, and standard attributes like [[nodiscard]], plus many small quality-of-life changes.
- The computer treats these as compile-time features: asserts are checked at compile time, _Generic selects code based on type, and static_assert catches mistakes early.
- The syntax is: static_assert(sizeof(int) == 4, "int must be 4 bytes");, and #include <threads.h> for standard threads.
- A realistic use: static_assert to guard assumptions about type sizes, and _Generic to write type-safe macros.
- A surprising detail: not every compiler supports all of C23 yet; check your compiler version and use -std=c11, -std=c17 or -std=c2x explicitly.
- The example program uses static_assert and _Generic to show type-based selection in a small, portable way.
- Use static_assert to check assumptions at compile time.
- Use _Generic to build type-safe macros without losing clarity.
- C17 is a small but important cleanup; use -std=c17 for stable modern builds.
- C23 adds many useful features, but check compiler support before relying on them.
- Modern C is still C. You are using the language well!
#include <stdio.h>
// Compile-time check: on this machine, int must be 4 bytes
static_assert(sizeof(int) == 4, "int must be 4 bytes");
// _Generic picks a function name based on the argument type
#define typeName(x) _Generic((x), \
int: "int", \
double: "double", \
char *: "char *", \
default: "other")
int main(void) {
// Each call resolves to a different string at compile time
printf("int -> %s\n", typeName(42));
printf("double -> %s\n", typeName(3.14));
printf("char* -> %s\n", typeName("hi"));
return 0;
}
// Build with: gcc -std=c11 -Wall -Wextra main.c -o appWhat happens: int -> int double -> double char* -> char *
Breaking it down
static_assert
static_assert(sizeof(int) == 4, "int must be 4 bytes");Fails the build if the condition is false. The message helps you understand what went wrong.
_Generic
_Generic((x), int: "int", double: "double", ...)Selects an expression based on the type of x at compile time. This is how type-safe macros are written in modern C.
Macro with backslashes
#define typeName(x) _Generic(...)The backslashes continue the macro across lines. The macro expands to the correct string for the type of x.
Compile flag
-std=c11Tells gcc to use the C11 standard. Use -std=c17 or -std=c2x for newer standards.
Modern C features
| Standard | Feature | Purpose |
|---|---|---|
| C11 | static_assert | Compile-time checks |
| C11 | _Generic | Type-based selection |
| C11 | threads.h | Standard threads |
| C17 | Clarifications | Bug fixes, no big features |
| C23 | typeof | Get the type of an expression |
| C23 | constexpr | Compile-time constants |
| C23 | nullptr | Typed null pointer |
A helpful analogy
Modern C is like the same toolbox you know, with a few new tools that make old jobs easier and safer. The hammer is still a hammer, but now there is a torque wrench that clicks when the bolt is tight, just like static_assert clicks at compile time when a check fails.
Nesting rules
- 1.Use -std=c11, -std=c17 or -std=c2x explicitly; the default may vary by compiler.
- 2.static_assert requires a message string in C11 but the message is optional in C23.
- 3._Generic selects expressions, not statements; keep the branches simple.
- 4.Check compiler support before using C23 features in production code.
Common mistakes
Assuming the default standard is C11
Avoid
gcc main.c // may be gnu17 or gnu11Use instead
gcc -std=c11 main.cThe default depends on the compiler version. Pass -std explicitly so builds are reproducible.
Using _Generic for side effects
Avoid
_Generic((x), int: printf("i"), ...) // messyUse instead
int kind = _Generic((x), int: 1, double: 2);_Generic is meant to select an expression. Using it for statements is confusing and error-prone.
Relying on C23 in older compilers
Avoid
constexpr int N = 5; // C23 onlyUse instead
#define N 5 // portableOlder compilers reject C23 features. Check the compiler version or provide a portable fallback.
Quick tips
- •Add static_assert for sizes and invariants you rely on.
- •Use _Generic to build safer macros, especially wrappers around printf.
- •Compile with -std=c17 for a stable modern baseline today.
- •Try changing the type in typeName to see the macro pick a different string.
- •Keep an eye on your compiler's release notes for new C23 support.