From WikiChip
Difference between revisions of "c/c11"
< c

m
(grammar, clarity)
 
(7 intermediate revisions by 3 users not shown)
Line 24: Line 24:
 
== Alignment ==
 
== Alignment ==
 
{{main|c/alignment of objects|l1=Alignment of objects}}
 
{{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 ''alignof'' are defined in [[stdalign.h|<stdalign.h>]].
+
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 ==
Line 41: Line 41:
  
 
== 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 respectably. For example:
+
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 69: Line 69:
 
== Static assertion ==
 
== Static assertion ==
 
{{main|c/static assertion|l1=Static assertion}}
 
{{main|c/static assertion|l1=Static assertion}}
C11 added support for compile-time assertions. Static assertions which takes place after the preprocessor compilation phase is over thereby allowing the use of C operators such as ''sizeof''.
+
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 which would prevent them from being reentrant.
+
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 which must execute even if the compiler's optimizer thinks 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 thinks the memset is not doing anything.
+
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 91: Line 91:
  
 
== Compiler support ==
 
== Compiler support ==
Due to its relative new status; most compilers have a lot implemented but none are complete.
 
  
 
{| 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:#FFCC66;text-align:center;" | Partial || style="text-align:center;" | -std=c11 || No thread support
+
| Clang || style="background:#99FF66;text-align:center;" | Supported || style="text-align:center;" | -std=c11 || No support for (optional) <threads.h>
 
|-
 
|-
| GCC || style="background:#FFCC66;text-align:center;" | Partial || style="text-align:center;" | -std=c11<ref>[http://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html Options Controlling C Dialect]</ref> || Missing thread, Unicode support<ref>[http://gcc.gnu.org/wiki/C11Status C11 Status]</ref>
+
| 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:#FF6666;text-align:center;" | No Support || ||  
+
| 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;" | ||

Latest revision as of 05:52, 14 September 2019

C Standards

Standard Revisions

Technical Reports

Other

v · d · e

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.

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

References[edit]

External links[edit]