(→Version detection) |
(grammar, clarity) |
||
(11 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
− | {{C standards}} | + | {{c title|C11}}{{C standards}} |
'''C11''' is the latest major revision of the [[C]] standard which was ratified by ISO on December 8, 2011 by the the international standardization working group for the [[C]] programming language, ''ISO/IEC JTC1/SC22/WG14''<ref>[http://www.iso.org/iso/home/store/catalogue_tc/catalogue_detail.htm?csnumber=57853 Information technology -- Programming languages -- C]</ref>. C11 obsoletes the previous standard, C99, published in 1999. In April of 2011 the final draft ([http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf N1570]) was published. The draft passed its final review on October 10, 2011 which was consequently ratified and published as '''ISO/IEC 9899:2011'''. C11 was also later adopted by INCITS as an ANSI standard on May 23, 2012 as '''INCITS/ISO/IEC 9899-2012'''<ref>[http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2FISO%2FIEC+9899-2012 INCITS/ISO/IEC 9899-2012]</ref>. | '''C11''' is the latest major revision of the [[C]] standard which was ratified by ISO on December 8, 2011 by the the international standardization working group for the [[C]] programming language, ''ISO/IEC JTC1/SC22/WG14''<ref>[http://www.iso.org/iso/home/store/catalogue_tc/catalogue_detail.htm?csnumber=57853 Information technology -- Programming languages -- C]</ref>. C11 obsoletes the previous standard, C99, published in 1999. In April of 2011 the final draft ([http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf N1570]) was published. The draft passed its final review on October 10, 2011 which was consequently ratified and published as '''ISO/IEC 9899:2011'''. C11 was also later adopted by INCITS as an ANSI standard on May 23, 2012 as '''INCITS/ISO/IEC 9899-2012'''<ref>[http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2FISO%2FIEC+9899-2012 INCITS/ISO/IEC 9899-2012]</ref>. | ||
− | The C11 standard adds concurrency support, provides a mechanism for | + | The C11 standard adds concurrency support, provides a mechanism for {{C|generic selection|type-generic expressions}}, adds alignment-related facilities, {{C|static assertion}}, Unicode support, floating-point characteristic macros, no-return functions, anonymous structures and unions |
, and various bound-checking and reentrancy functions. | , and various bound-checking and reentrancy functions. | ||
Line 23: | Line 23: | ||
== Alignment == | == Alignment == | ||
− | {{main| | + | {{main|c/alignment of objects|l1=Alignment of objects}} |
− | C11 provides a new mechanism for working with objects alignments. The '''_Alignof''' operator was added which yields the alignment requirement (an integer constant) of its operand type (which remains unevaluted). A set of convenient macros such as ''alignof'' and '' | + | C11 provides a new mechanism for working with objects alignments. The '''_Alignof''' operator was added which yields the alignment requirement (an integer constant) of its operand type (which remains unevaluted). A set of convenient macros such as ''alignof'' and ''alignas'' are defined in [[stdalign.h|<stdalign.h>]]. |
For example: | For example: | ||
Line 35: | Line 35: | ||
=== Extended alignment === | === Extended alignment === | ||
− | Extended alignment is the ability for an implementation to support alignment requirement greater than _Alignof (max_align_t). A type having extended alignment requirement is called an ''over-aligned type''. That feature is optional and need not be supported. | + | Extended alignment is the ability for an implementation to support alignment requirement greater than _Alignof (max_align_t). A type having extended alignment requirement is called an ''over-aligned type''. That feature is optional and need not be supported. |
== Aligned memory allocations == | == Aligned memory allocations == | ||
− | C11 added the | + | C11 added the {{C|stdlib.h/aligned_alloc|aligned_alloc}} function which allocates space for an object whose alignment is provided by you. |
== Unicode support == | == Unicode support == | ||
− | C11 adds support for Unicode characters and string literals. A set of prefixes have been added to support [[UTF-8]], [[UTF-16]], and [[UTF-32]] character strings. The ''u8'' prefix for strings creates a UTF-8 encoded string. Likewise the ''u'' and ''U''' prefixes create UTF-16 and UTF-32 strings | + | C11 adds support for Unicode characters and string literals. A set of prefixes have been added to support [[UTF-8]], [[UTF-16]], and [[UTF-32]] character strings. The ''u8'' prefix for strings creates a UTF-8 encoded string. Likewise the ''u'' and ''U''' prefixes create UTF-16 and UTF-32 strings respectively. For example: |
<source lang="C"> | <source lang="C"> | ||
Line 64: | Line 64: | ||
== Type-generic expressions == | == Type-generic expressions == | ||
− | {{main| | + | {{main|c/generic selection|l1=Generic selection}} |
C11 now provides a mechanism for type-generic expressions, a mechanism needed to perform type-generic math without compiler magic. The feature is achieved via the addition of the new '''_Generic''' keyword. | C11 now provides a mechanism for type-generic expressions, a mechanism needed to perform type-generic math without compiler magic. The feature is achieved via the addition of the new '''_Generic''' keyword. | ||
== Static assertion == | == Static assertion == | ||
− | {{main|static assertion | + | {{main|c/static assertion|l1=Static assertion}} |
− | C11 added support for compile-time assertions. Static assertions | + | C11 added support for compile-time assertions. Static assertions are evaluated after the preprocessor compilation phase is over, thereby allowing the use of C operators such as ''sizeof''. |
== Bounds-checking interfaces annex (Annex K) == | == Bounds-checking interfaces annex (Annex K) == | ||
Line 75: | Line 75: | ||
=== Reentrant functions === | === Reentrant functions === | ||
− | The functions ''strtok_s'', ''wcstok_s'', ''bsearch_s'', and ''qsort_s'', were added which keep track of their state via an argument that is passed instead of a static buffer | + | The functions ''strtok_s'', ''wcstok_s'', ''bsearch_s'', and ''qsort_s'', were added, which keep track of their state via an argument that is passed instead of a static buffer to ensure they are reentrant. |
=== %n format specifier === | === %n format specifier === | ||
− | In various format printing functions, all %specifiers with the exception of %n are used for output purpose. On the other hand, the %n specifier is user for input. The ''_s'' version of the functions was added which gets rid of the %n specifier entirely. | + | In various format printing functions, all %specifiers with the exception of %n are used for output purpose. On the other hand, the %n specifier is user for input. The ''_s'' version of the functions was added, which gets rid of the %n specifier entirely. |
=== memset_s === | === memset_s === | ||
− | The memset_s function is a special version of memset | + | The memset_s function is a special version of memset that must execute even if the compiler's optimizer considers it superfluous. The best case for this is when a function attempts to zero-out some confidential information such as a password right before returning, thus making the optimizer think that the memset is not doing anything. |
== Analyzability annex (Annex L) == | == Analyzability annex (Annex L) == | ||
Line 87: | Line 87: | ||
== Concurrency == | == Concurrency == | ||
− | {{main| | + | {{main|c/concurrency|l1=Concurrency}} |
− | C11's largest addition to the C programming language is the addition of semantics of | + | C11's largest addition to the C programming language, is the addition of semantics of multi‐threaded programs. The standard also added a facility for performing inter‐thread communication via atomic variables as well as thread-specific storage. Thread support is optional and must be tested for via the '''__STDC_NO_THREADS__''' macro. |
== Compiler support == | == Compiler support == | ||
− | |||
{| class="wikitable" | {| class="wikitable" | ||
Line 101: | Line 100: | ||
| ACK || style="background:#FF6666;text-align:center;" | No Support || || | | ACK || style="background:#FF6666;text-align:center;" | No Support || || | ||
|- | |- | ||
− | | Clang || style="background:# | + | | Clang || style="background:#99FF66;text-align:center;" | Supported || style="text-align:center;" | -std=c11 || No support for (optional) <threads.h> |
|- | |- | ||
− | | GCC || style="background:# | + | | GCC || style="background:#99FF66;text-align:center;" | Supported || style="text-align:center;" | -std=c11<ref>[http://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html Options Controlling C Dialect]</ref> || No support for (optional) <threads.h><ref>[http://gcc.gnu.org/wiki/C11Status C11 Status]</ref> |
|- | |- | ||
− | | TCC || style="background:# | + | | TCC || style="background:#FFCC66;text-align:center;" | Partial || || |
|- | |- | ||
| ICC || style="background:#FFCC66;text-align:center;" | Partial || style="text-align:center;" | || | | ICC || style="background:#FFCC66;text-align:center;" | Partial || style="text-align:center;" | || | ||
Line 118: | Line 117: | ||
* [http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1570.pdf C11 final draft] | * [http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1570.pdf C11 final draft] | ||
− | |||
[[Category:C standards]] | [[Category:C standards]] |
Latest revision as of 04:52, 14 September 2019
C11 is the latest major revision of the C standard which was ratified by ISO on December 8, 2011 by the the international standardization working group for the C programming language, ISO/IEC JTC1/SC22/WG14[1]. C11 obsoletes the previous standard, C99, published in 1999. In April of 2011 the final draft (N1570) was published. The draft passed its final review on October 10, 2011 which was consequently ratified and published as ISO/IEC 9899:2011. C11 was also later adopted by INCITS as an ANSI standard on May 23, 2012 as INCITS/ISO/IEC 9899-2012[2].
The C11 standard adds concurrency support, provides a mechanism for type-generic expressions, adds alignment-related facilities, static assertion, Unicode support, floating-point characteristic macros, no-return functions, anonymous structures and unions , and various bound-checking and reentrancy functions.
Contents
- 1 Version detection
- 2 New headers
- 3 No-return functions
- 4 Alignment
- 5 Aligned memory allocations
- 6 Unicode support
- 7 Type-generic expressions
- 8 Static assertion
- 9 Bounds-checking interfaces annex (Annex K)
- 10 Analyzability annex (Annex L)
- 11 Concurrency
- 12 Compiler support
- 13 References
- 14 External links
Version detection[edit]
C11 can be detected via the __STDC_VERSION__ mandatory macro and the __STDC_LIB_EXT1__ conditional feature macro which must equal 201112L. Note that the official standard has a defect where the macro's definition is set to '201ymmL'. The defect has been corrected in Defect Report #411.
#if __STDC_VERSION__ >= 201112L
/* C11 support */
#endif
New headers[edit]
C11 introduces 5 new standard headers: <stdalign.h>, <stdatomic.h>, <stdnoreturn.h>, <threads.h>, and <uchar.h>. The two concerned with concurrency (stdatomic.h and threads.h) are optional.
No-return functions[edit]
Some functions do not ever return for various reasons. The new _Noreturn keyword can be used to mark functions that do not return to their caller such as exit(), longjmp(), and thrd_exit(). Using _Noreturn can assist compiler's optimizers as well as eliminating possible diagnostic messages.
Note that the use of _Noreturn must be limited to functions that cannot return under any circumstance. No-return functions that may return under certain conditions will exhibit undefined behavior.
Alignment[edit]
- Main article: Alignment of objects
C11 provides a new mechanism for working with objects alignments. The _Alignof operator was added which yields the alignment requirement (an integer constant) of its operand type (which remains unevaluted). A set of convenient macros such as alignof and alignas are defined in <stdalign.h>.
For example:
printf("The alignment of char is %zd.\n", _Alignof (char));
Which must print 1 for the alignment of char. Additionally, the alignment can be specified as an integer using the _Alignas alignment specifier.
On each platform, a some type has the largest alignment requirement. C11 added the max_align_t object type whose alignment is as great as is supported by the implementation.
Extended alignment[edit]
Extended alignment is the ability for an implementation to support alignment requirement greater than _Alignof (max_align_t). A type having extended alignment requirement is called an over-aligned type. That feature is optional and need not be supported.
Aligned memory allocations[edit]
C11 added the aligned_alloc function which allocates space for an object whose alignment is provided by you.
Unicode support[edit]
C11 adds support for Unicode characters and string literals. A set of prefixes have been added to support UTF-8, UTF-16, and UTF-32 character strings. The u8 prefix for strings creates a UTF-8 encoded string. Likewise the u and U' prefixes create UTF-16 and UTF-32 strings respectively. For example:
#include <uchar.h>
/* UTF-8 */
char u8str[] = u8"ππ This is a UTF-8 string ππ.";
char u8chr = u8'π';
/* UTF-16 */
#ifdef __STD_UTF_16__
char16_t u16str[] = u"ΩΩ This is a UTF-16 string ΩΩ.";
char16_t u16char = u'Ω';
#endif
/* UTF-32 */
#ifdef __STD_UTF_32__
char32_t u32str[] = U"δδ This is a UTF-32 string δδ.";
char32_t u32char = U'δ';
#endif
Type-generic expressions[edit]
- Main article: Generic selection
C11 now provides a mechanism for type-generic expressions, a mechanism needed to perform type-generic math without compiler magic. The feature is achieved via the addition of the new _Generic keyword.
Static assertion[edit]
- Main article: Static assertion
C11 added support for compile-time assertions. Static assertions are evaluated after the preprocessor compilation phase is over, thereby allowing the use of C operators such as sizeof.
Bounds-checking interfaces annex (Annex K)[edit]
The Bounds-checking interfaces annex provides a set of functions with more restricted behaviors in order to allow for more secure coding practices.
Reentrant functions[edit]
The functions strtok_s, wcstok_s, bsearch_s, and qsort_s, were added, which keep track of their state via an argument that is passed instead of a static buffer to ensure they are reentrant.
%n format specifier[edit]
In various format printing functions, all %specifiers with the exception of %n are used for output purpose. On the other hand, the %n specifier is user for input. The _s version of the functions was added, which gets rid of the %n specifier entirely.
memset_s[edit]
The memset_s function is a special version of memset that must execute even if the compiler's optimizer considers it superfluous. The best case for this is when a function attempts to zero-out some confidential information such as a password right before returning, thus making the optimizer think that the memset is not doing anything.
Analyzability annex (Annex L)[edit]
The Analyzability Annex identifies a small subset of all undefined behaviors and sets some implementation constraints on the resulting behavior in an attempt to mitigate potential issues. An implementation that conforms to Annex L must define __STDC_ANALYZABLE__.
Concurrency[edit]
- Main article: Concurrency
C11's largest addition to the C programming language, is the addition of semantics of multi‐threaded programs. The standard also added a facility for performing inter‐thread communication via atomic variables as well as thread-specific storage. Thread support is optional and must be tested for via the __STDC_NO_THREADS__ macro.
Compiler support[edit]
C11 Compiler Support | |||
Compiler Name | Support Level | Enforcement Switch | Note |
---|---|---|---|
ACK | No Support | ||
Clang | Supported | -std=c11 | No support for (optional) <threads.h> |
GCC | Supported | -std=c11[3] | No support for (optional) <threads.h>[4] |
TCC | Partial | ||
ICC | Partial | ||
Visual C++ | Partial | Missing most headers |