C Reference ยท Concepts and Tools
What is new in C11
A tour of the C11 standard with runnable examples: _Generic, _Static_assert, _Alignas, anonymous structs, _Noreturn, atomics, threads and Unicode support.
C11 (ISO/IEC 9899:2011) brought C into the multicore era and added a few long-wanted language tools. Its headline additions are a memory model with atomics, a threads library, generic selection with _Generic, compile-time assertions, and alignment control. C17 later fixed defects in C11 without adding features, so "C11" in practice also means C17.
Some of C11 is optional: implementations may leave out threads (__STDC_NO_THREADS__), atomics (__STDC_NO_ATOMICS__), complex numbers and variable-length arrays (__STDC_NO_VLA__). The online runner supports the features shown on this page.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Language additions
| Feature | Notes | Page |
|---|---|---|
_Generic |
Compile-time selection by type; the basis for type-generic macros such as <tgmath.h>. |
_Generic |
_Static_assert (static_assert via <assert.h>) |
A compile-time check with a message. | static_assert |
_Alignas, _Alignof (<stdalign.h>) |
Request and query alignment. | alignas and alignof |
_Noreturn (<stdnoreturn.h>) |
A function never returns; replaced by [[noreturn]] in C23. |
noreturn |
_Thread_local (thread_local) |
One copy of a variable per thread. | thread_local |
_Atomic and <stdatomic.h> |
Lock-free (where possible) operations with defined memory ordering. | stdatomic.h |
| Anonymous structs and unions | Members of an unnamed inner aggregate are accessed directly. | union |
| Unicode literals | u8"...", u"..." (char16_t), U"..." (char32_t); <uchar.h> conversion functions. |
uchar.h |
| Macros with the same type | CMPLX() for complex numbers; FLT_DECIMAL_DIG, DBL_HAS_SUBNORM in <float.h>. |
complex.h |
| VLAs optional | __STDC_NO_VLA__ may be defined. |
Variable-length arrays |
Library additions
<threads.h>:thrd_create,thrd_join,mtx_lock,cnd_wait,tss_get,call_once. A small, portable counterpart of POSIX threads; see threads.h.- Bounds-checking interfaces (Annex K):
strcpy_s,fopen_s,gets_s, and others. Optional, implemented by MSVC but not by glibc; do not count on them. See Bounds-checked functions. aligned_allocin<stdlib.h>; see aligned_alloc.quick_exitandat_quick_exit: exit without running full cleanup.timespec_getandstruct timespecin<time.h>; see timespec.getswas removed (it was already deprecated for years; it cannot be used safely).- File opening mode
"x":fopen(path, "wx")fails if the file already exists.
Usage notes
_Genericselects, it does not convert. Every branch must still compile, andchar *andconst char *are different types, as the example shows.- A
_Static_assertis not anassert(): it is checked at compile time and costs nothing at run time. - Atomics need care.
atomic_intoperations are indivisible; ordinaryintshared between threads is a data race (undefined behavior). - Threads and the runner:
<threads.h>works in the sandbox, but there is one CPU and a short time limit. Keep thread counts small. - Support check:
#ifndef __STDC_NO_THREADS__before including<threads.h>. - C99 mode (
-std=c99) rejects most of this page; use-std=c11or newer.
Related
- C standards, What is new in C23, Portability
- pthread, Compiler flags
- Tutorial: Threads, Modern C