C11 (C standard revision)
C language revisions |
---|
C11 (previously C1X, formally ISO/IEC 9899:2011),[1] is a past standard for the C programming language. It replaced C99 (standard ISO/IEC 9899:1999) and has been superseded by C17 (standard ISO/IEC 9899:2018). C11 mainly standardizes features already supported by common contemporary compilers, and includes a detailed memory model to better support multiple threads of execution. Due to delayed availability of conforming C99 implementations, C11 makes certain features optional, to make it easier to comply with the core language standard.[2][3]
The final draft, N1570,[4] was published in April 2011. The new standard passed its final draft review on October 10, 2011 and was officially ratified by ISO and published as ISO/IEC 9899:2011 on December 8, 2011, with no comments requiring resolution by participating national bodies.
A standard macro __STDC_VERSION__
is defined with value 201112L
to indicate that C11 support is available.[5]
Changes from C99
[edit]The standard includes several changes to the C99 language and library specifications, such as[6]
- Alignment specification (
_Alignas
specifier,_Alignof
operator,aligned_alloc
function,<stdalign.h>
header) - The
_Noreturn
function specifier and the<stdnoreturn.h>
header - Type-generic expressions using the
_Generic
keyword. For example, the following macrocbrt(x)
translates tocbrtl(x)
,cbrt(x)
orcbrtf(x)
depending on the type ofx
:
#define cbrt(x) _Generic((x), long double: cbrtl, \
default: cbrt, \
float: cbrtf)(x)
- Multi-threading support (
_Thread_local
storage-class specifier,<threads.h>
header including thread creation/management functions, mutex, condition variable and thread-specific storage functionality, as well as<stdatomic.h>
[7] for atomic operations supporting the C11 memory model). - Improved Unicode support based on the C Unicode Technical Report ISO/IEC TR 19769:2004 (
char16_t
andchar32_t
types for storing UTF-16/UTF-32 encoded data, including conversion functions in<uchar.h>
and the correspondingu
andU
string literal prefixes, as well as theu8
prefix for UTF-8 encoded literals).[8] - Removal of the
gets
function (in favor of saferfgets
), which was deprecated in the previous C language standard revision, ISO/IEC 9899:1999/Cor.3:2007(E). - Bounds-checking interfaces (Annex K).[9]
- Analyzability features (Annex L).
- More macros for querying the characteristics of floating-point types, concerning subnormal floating-point numbers and the number of decimal digits the type is able to store.
- Anonymous structures and unions, useful when unions and structures are nested, e.g. in
struct T { int tag; union { float x; int n; }; };
. - Static assertions, which are evaluated during translation at a later phase than
#if
and#error
, when types are understood by the translator. - An exclusive create-and-open mode (
"…x"
suffix) forfopen
. This behaves likeO_CREAT|O_EXCL
in POSIX, which is commonly used for lock files. - The
quick_exit
function as a third way to terminate a program, intended to do at least minimal deinitialization.[10] - A new
timespec_get
function and corresponding structure in<time.h>
with a degree of POSIX compatibility. - Macros for the construction of complex values (partly because
real + imaginary*I
might not yield the expected value ifimaginary
is infinite or NaN).[11]
Optional features
[edit]The new revision allows implementations to not support certain parts of the standard — including some that had been mandatory to support in the 1999 revision.[12] Programs can use predefined macros to determine whether an implementation supports a certain feature or not.
Feature | Feature test macro | Availability in C99[13] |
---|---|---|
Analyzability (Annex L) | __STDC_ANALYZABLE__
|
Not available |
Bounds-checking interfaces (Annex K) | __STDC_LIB_EXT1__
|
Not available |
Multithreading (<threads.h> )
|
__STDC_NO_THREADS__
|
Not available |
Atomic primitives and types (<stdatomic.h> and the _Atomic type qualifier)[14]
|
__STDC_NO_ATOMICS__
|
Not available |
IEC 60559 floating-point arithmetic (Annex F) | __STDC_IEC_559__
|
Optional |
IEC 60559 compatible complex arithmetic (Annex G) | __STDC_IEC_559_COMPLEX__
|
Optional |
Complex types (<complex.h> )
|
__STDC_NO_COMPLEX__
|
Mandatory for hosted implementations |
Variable-length arrays[15] | __STDC_NO_VLA__
|
Mandatory |
Compiler support
[edit]Some features of C11 are supported by the GCC starting with version 4.6,[16] Clang starting with version 3.1,[17] IBM XL C starting with version 12.1,[18] and Microsoft Visual C++ starting with VS 2019 (16.8)[19] in September 2020.
Criticism
[edit]The optional bounds-checking interfaces (Annex K) remain controversial and have not been widely implemented, and their deprecation or removal from the next standard revision has been proposed.[20] Even Microsoft, a main proponent of this interface, does not conform to the definition.[21] In addition, Annex K does not include the more useful TR24731-2 (dynamic allocation functions), such as vasprintf
and open_memstream
.[22] The few open-source implementations include Open Watcom C/C++'s "Safer C" library[23] and safeclib.[24]
See also
[edit]- C++23, C++20, C++17, C++14, C++11, C++03, C++98, versions of the C++ programming language standard
- Compatibility of C and C++
References
[edit]- ^ "ISO/IEC 9899:2011 - Information technology -- Programming languages -- C". www.iso.org.
- ^ "WG14 N1250 The C1X Charter" (PDF).
- ^ "Subsetting the C Standard". www.open-std.org.
- ^ WG14 N1570 Committee Draft — April 12, 2011
- ^ "Defect report #411". ISO/IEC JTC1/SC22/WG14 - C. February 2012. Retrieved 2012-05-04.
- ^ WG14 N1516 Committee Draft — October 4, 2010
- ^ "Atomic operations library - cppreference.com". en.cppreference.com.
- ^ "WG14 N1286 — "On Support for TR-19769 and New Character Types", Nick Stoughton, Larry Dwyer" (PDF).
- ^ Berin Babcock-McConnell. "API02-C. Functions that read or write to or from an array should take an argument to specify the source or target size".
- ^ "Abandoning a Process". www.open-std.org.
- ^ "Creation of complex value". www.open-std.org.
- ^ WG14 N1548 Committee Draft — December 2, 2010 6.10.8.3 Conditional feature macros
- ^ ISO 9899:1999 6.10.8 Predefined macro names
- ^ "WG14 N1558 Mar 14-18 meeting minutes (draft)" (PDF).
- ^ ISO 9899:2011 Programming Languages - C 6.7.6.2 4
- ^ "GCC 4.6 Release Series — Changes, New Features, and Fixes - GNU Project - Free Software Foundation (FSF)". gcc.gnu.org.
- ^ "Clang 3.1 Release Notes". llvm.org.
- ^ "Support for ISO C11 added to IBM XL C/C++ compilers". www.ibm.com. 17 April 2014.
- ^ "C11 and C17 Standard Support Arriving in MSVC". devblogs.microsoft.com. 14 September 2020.
- ^ "N1969 — Updated Field Experience With Annex K — Bounds Checking Interfaces". www.open-std.org.
- ^ Leffler, Jonathan. "c - Do you use the TR 24731 'safe' functions?". Stack Overflow.
- ^ "c - Do you use the TR 24731 'safe' functions?". Stack Overflow.
- ^ "Safer C Library - Open Watcom". 3 May 2015. Archived from the original on 2015-05-03.
- ^ "safec: Safe C Library - README". rurban.github.io.
Further reading
[edit]- N1570 (working draft of C11 standard); WG14; 2011. (HTML and ASCII versions)
- ISO/IEC 9899:2011 (official C11 standard); ISO; 2011.
- Plum, Thomas (April 6, 2012). "C Finally Gets A New Standard". Dr. Dobb's Journal.
- Safe C API—Concise solution of buffer overflow, The OWASP Foundation, OWASP AppSec, Beijing 2011