ISO C: Difference between revisions

Tags: Mobile edit Mobile web edit
 
(3 intermediate revisions by the same user not shown)
Line 15: Line 15:
* Implementations are "encouraged to ensure that representation of C and C++ atomic types is the same."
* Implementations are "encouraged to ensure that representation of C and C++ atomic types is the same."


==Aliasing==
==Asserts==
* <tt>_Static_assert()</tt> is evaluated at compile time, huzzah!
 
==Generic dispatch==
The new <tt>_Genetic</tt> keyword allows mapping the type of an expression to a set of expressions. This is mainly usable for type-based dispatch of the kind seen in <tt>tgmath.h</tt>. You can't, so far as I can tell, use it for true parametric polymorphism. Tony Finch demonstrates a clever use for [https://fanf.livejournal.com/144696.html parametric constness]:
<code>
    #define strchr(s,c) _Generic((s),                    \
        const char * : (const char *)(strchr)((s), (c)), \
        char *      :              (strchr)((s), (c)))</code>
 
==Types==
===Aliasing===
* See [[Compiler Design]] page
* See [[Compiler Design]] page
* -O2 implies -fstrict-aliasing, at least as of gcc 4.3
* -O2 implies -fstrict-aliasing, at least as of gcc 4.3
Line 21: Line 32:
** How to use Berkeley sockets API? See http://archives.free.net.ph/message/20080529.200047.b40321b6.fi.html, etc
** How to use Berkeley sockets API? See http://archives.free.net.ph/message/20080529.200047.b40321b6.fi.html, etc


==Types==
 
===stdint.h===
===stdint.h===
An excellent addition in C99. Type constructions of the form u?int{variety}_t (u prefix denotes unsigned). Varieties include:
An excellent addition in C99. Type constructions of the form u?int{variety}_t (u prefix denotes unsigned). Varieties include:
Line 29: Line 40:
* <tt>least{8,16,32,64}</tt>: Minimum native integer type having at least the specified width.
* <tt>least{8,16,32,64}</tt>: Minimum native integer type having at least the specified width.
* <tt>{8,16,32,64}</tt>: Integer type having precisely this width.
* <tt>{8,16,32,64}</tt>: Integer type having precisely this width.
====inttypes.h====
ugly <tt>printf</tt> specifiers for the types in <tt>stdint.h</tt>. e.g.:
* <tt>printf("%"PRIu64"\n", some_uint64_tvar);</tt>
===tgmath.h===
===tgmath.h===
===_Generic===
C11 introduced the new keyword <tt>[https://en.cppreference.com/w/c/language/generic _Generic]</tt>, allowing one of several type-tagged expressions to be chosen based on the type of a controlling expression.
===<tt>static</tt> in array parameters===
===<tt>static</tt> in array parameters===
Beginning with C99, <tt>static</tt> can be inserted into array parameters ala:
Beginning with C99, <tt>static</tt> can be inserted into array parameters ala: