    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: The ANSI Standard For C</title>
        <link>https://members.accu.org/index.php/articles/1658</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 1, #3 - Feb 1988</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c227/">013</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-227/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+227/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;The ANSI Standard For C</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 28 June 2010 08:58:00 +01:00 or Mon, 28 June 2010 08:58:00 +01:00</p>
<p><strong>Summary:</strong>&nbsp;A summary of the proposed ANSI extensions to the C Programming Language By Steven W.Palmer</p>
<p><strong>Body:</strong>&nbsp;<h2>Introduction</h2>

<p>Since it's introduction in 1972 by Dennis Ritchie at Bell Laboratories, C has matured over the years with each new implementation. A number of features designed to provide the language with stricter type checking and to cater for the increasing sophistication of the underlying hardware have evolved from different companies in different formats. The new proposed ANSI standard attempts to bring together the more popular and versatile extensions with a totally revised reference manual which removes many of the ambiguities of the early one. This article looks briefly at each of the new ANSI extensions.</p>

<h2>Trigraphs</h2>

<p>To cater for systems supporting only the ISO 646-1083 character set which provides only a sub-set of the full ASCII character set, ANSI allows the unsupported characters to be represented by trigraphs. A trigraph is two consecutive question marks, followed by a representable character.</p>

<p>The proposed trigraphs are shown below</p>

<p><table class="sidebartable">
    <tr>
      <th valign="top">Trigraph<br>
      </th>
      <th valign="top">Character<br>
      </th>
    </tr>
    <tr>
      <td>??( </td>
      <td>[</td>
    </tr>
    <tr>
      <td>??) </td>
      <td>]</td>
    </tr>
    <tr>
      <td>??-</td>
      <td>~</td>
    </tr>
    <tr>
      <td>??&lt; </td>
      <td>{</td>
    </tr>
    <tr>
      <td>??&gt; </td>
      <td>}</td>
    </tr>
    <tr>
      <td>??! </td>
      <td>|</td>
    </tr>
    <tr>
      <td>??' </td>
      <td>^</td>
    </tr>
    <tr>
      <td>??= </td>
      <td>#</td>
    </tr>
    <tr>
      <td>??/ </td>
      <td>\</td>
    </tr>
</table></p>
<br>

<p>The new escape code <tt>\?</tt> has been provided to prevent strings that resemble trigraphs from being expanded. For example</p>

<p><pre class="programlisting">
puts(&quot;The trigraph of [ is \??(&quot;);</p>
</pre></p>

<p>without the escape character, the ??( would be converted to [.</p>

<h2>Reserved Words</h2>

<p>The keywords, <tt>asm</tt>, <tt>fortran</tt> and <tt>entry</tt>, are no longer reserved (although <tt>asm</tt> is preserved in the C++ reference manual, and may later be re-introduced back into C.) The keywords <tt>const</tt>, <tt>volatile</tt> and <tt>signed</tt> have been introduced. <tt>const</tt> is a type modifier that indicates that the identifier has read-only properties (i.e. it may not be assigned a value after initialisation.) <tt>volatile</tt> specifies that the identifier is subject to external changes, and must not be changed by the optimiser. <tt>signed</tt> is already present in most compilers, and complements <tt>unsigned</tt>.</p>

<h2>Escape Codes</h2>

<p>The standard set of character codes is extended with <tt>\a</tt> for the BELL character, and <tt>\v</tt> for the VERTICAL TAB character. Numeric escape codes now support hexadecimal codes in the format <tt>\xnn</tt> or <tt>\Xnn</tt> where nn is a two-digit hexadecimal number. Both of these extensions are already implemented in most modern C compilers.</p>

<p>The use of an escape character followed by a newline to indicate line continuance was originally restricted to macro definitions and strings. ANSI now allows it to apply to any line of C source code.</p>

<h2>Strings</h2>

<p>Strings may be concatenated by following the terminating quote of one string with the initial quote of another, and with only whitespace in between. For example</p>

<p><pre class="programlisting">
puts(&quot;This is a very long string that has to be split\
over two lines to fit&quot;);
</pre>

can now be written

<pre class="programlisting">
puts(&quot;This is a very long string that has to&quot;
&quot;be split over two lines to fit&quot;);
</pre></p>

<p>The decision as to whether similar constant strings share the same storage space has not been resolved, although it is still considered bad practice to modify string constants. Microsoft C V5.0 supports string concatenation.</p>

<h2>Preprocessor</h2>

<p>The following preprocessor directives have been added; <tt>#error</tt>, <tt>#pragma</tt> and <tt>#elif</tt>. <tt>#if</tt> has been extended to allow the use of the 'defined' keyword in preference to <tt>#ifdef</tt>. <tt>#error</tt> forces it's argument to be written to the user console. This allows users to implement their own error checking at compile time. For example, if a program must not be run with a STACK size of larger than 30000 bytes, then</p>

<p><pre class="programlisting">
#if STACK &gt; 30000
#error &quot;STACK too big - Truncated&quot;
#undef STACK
#define STACK 30000
#endif
</pre></p>

<p>will detect this and inform the user. The actual behavior of <tt>#error</tt> in relation to the compiler is mostly compiler dependent at the moment.</p>

<p><tt>#pragma</tt> allows options to be passed to the compiler from within the source code. The choice of options is manufacturer dependent, and will vary between systems. As an example, Microsoft C V4.0 provides a single <tt>#pragma</tt> to toggle generation of stack checking code. <tt>#pragma</tt> check_stack+ causes the compiler to include code to check the stack on entry to subsequently declared functions to ensure that there is enough room for local variables. <tt>#pragma</tt> check_stack- switches off stack checking code.</p>

<p>The preprocessing rules have been rewritten to remove the ambiguities inherent in the old compilers. One notable change is that ANSI now allows the use of a macro name in it's definition. Where this occurs, the macro name will not be expanded. This allows the use of a macro to replace or redefine an existing function or macro. For example</p>

<p><pre class="programlisting">
#define sqrt(x) (((x) &lt; 0) ? 0 : sqrt(x))
</pre></p>

<p>Here, if the sqrt macro is passed a negative value, then it will implicitly return 0, otherwise it will call the actual sqrt function.</p>

<h2>Predefined Macros</h2>

<p>ANSI specifies that the following macros are predefined:</p>

<p><table class="sidebartable">
<tr><td>__LINE__</td><td>The current source code line number.</td></tr>
<tr><td>__FILE__</td><td>The source code file name.</td></tr>
<tr><td>__DATE__</td><td>The date at the time the macro was translated in the form &quot;mmm dd yyyy&quot;</td></tr>
<tr><td>__TIME__</td><td>The time when the macro was translated in the form &quot;hh:mm:ss&quot;</td></tr>
<tr><td>__STDC__</td><td>A predefined macro with a non-zero value</td></tr>
</table></p>
<br>

<h2>Stringization</h2>

<p>Stringization, and token-pasting described in the next section, are already supported by Microsoft C V5.0 and later versions. ANSI allows macro arguments to be converted to strings in the expansion by prefixing the argument name in the definition by the single <tt>#</tt> character. For example</p>

<p><pre class="programlisting">
#define ASSERT(n,l) puts(&quot;Error &quot; #n &quot; in line &quot; #l) 
... 
ASSERT(90, __LINE__);
</pre>
expands to (assuming __LINE__ is 100)
<pre class="programlisting">
puts(&quot;Error &quot; &quot;90&quot; &quot; in line &quot; &quot;100&quot;);
</pre>
which, after string concatenation, is equivalent to
<pre class="programlisting">
puts(&quot;Error 90 in line 100&quot;);
</pre></p>

<h2>Token-Pasting</h2>

<p>Token-pasting is the merging of two disjoint tokens in the definition to create a single new token. The inclusion of <tt>##</tt> between two objects forces the preprocessor to combine the two objects after all macro expansion has been performed. For example</p>

<p><pre class="programlisting">
#define gencode(x) callgen##x()
</pre>
If the following is encountered in the source code
<pre class="programlisting">
gencode(12)
</pre>
it will be preprocessed to
<pre class="programlisting">
callgen12()
</pre></p>

<h2>Numeric Constants</h2>

<p>The single type modifier letter, '<tt>L</tt>' or '<tt>l</tt>' which was used to cast an integer to a long integer is now complemented by '<tt>U</tt>' and '<tt>F</tt>'. '<tt>U</tt>' can be used with any integral constant, and casts the type to unsigned. It may be used in conjunction with the long-integer modifier letter.</p>

<p>'<tt>F</tt>' can only be used with floating point values, and changes the default type of a floating point number from double to float. A floating point literal is always assumed to be of type double by the compiler.</p>

<p>However, the proper use of function prototypes eliminates the need to use a type modifier at all. Where needed, a cast is more explicit.</p>

<p>The type, <tt>long float</tt>, is no longer supported. An additional type, <tt>long double</tt>, has been introduced. It's range is implementation defined, but should be the same as, or longer than, <tt>double</tt>.</p>

<h2>Generic Type</h2>

<p>The type, <tt>void *</tt>, has been included as a generic type which can be cast to any other valid C type. For example</p>

<p><pre class="programlisting">
char *malloc(unsigned int);
</pre>
has now been replaced by
<pre class="programlisting">
void *malloc(unsigned int);
</pre></p>

<p>The size of <tt>void *</tt> is virtual, so the use of <tt>sizeof</tt> with <tt>void *</tt> has no defined meaning.</p>

<h2>Initialisation</h2>

<p>Unions may be initialised. The value assigned to the union must be the same type as the first member of the union. For example</p>

<p><pre class="programlisting">
union {
   long Vid_IO_Ptr;
   char *VidPtr;
} VidMap = 0xb8000000;
</pre></p>

<h2>Volatile</h2>

<p><tt>volatile</tt> is a new type specifier that is really only of interest to the optimiser. It specifies that the object it declares is volatile. In other words, it's value may be changed by an external event during program execution. For example</p>

<p><pre class="programlisting">
volatile int i;
...
set_vsync(&amp;i);
wait_vsync(); /* Synchronise with first VSYNC */
i = 0;
while (i) {
   /* Do odd-jobs while waiting for vertical sync */
   scan_keyb();
   check_comm();
}
</pre></p>

<p>In the above code, taken from an actual program, the optimiser would detect that identifier i was not altered inside the loop body, and would optimise out (remove) the body of the loop! The use of <tt>volatile</tt> warns it that the identifier is modified by forces outside the programs control.</p>

<h2>Constants</h2>

<p>The introduction of the <tt>const</tt> keyword is hoped to reduce the use of #define to</p>

<p>denote constant values in a program. The use of <tt>const</tt> in place of #define is preferred as it conveys far more information to the compiler about the programmers intentions.</p>

<p>Including <tt>const</tt> in a declaration specifies that the identifier does not (and should not) change value during program execution. A constant must be initialised at declaration. Any attempt to subsequently change it's value will generate a warning from the compiler.</p>

<p>Unfortunately, without proper protection, there is nothing to stop an external library function from modifying a constant when passed the address of that constant. For example</p>

<p><pre class="programlisting">
const int p = -1;
...
n = libfunc(&amp;p);
</pre></p>

<p>if the library function, libfunc, modifies the value of p, then the problem will never be detected. There is no way, in C, to indicate that a function taking a pointer argument will modify the data to which the pointer points. Unless the programmer is careful, the use of the above code will cause problems if the constant was placed in ROM or the hardware traps write-access to constant storage.</p>

<p>I would have liked to have seen the use of <tt>volatile</tt> extended to function declarations to indicate that a function modifies external data through the use of a pointer. For example</p>

<p><pre class="programlisting">
int libfunc(volatile int *);
</pre></p>

<h2>Operators</h2>

<p>The unary plus operator has now been incorporated into the C language with the same precedence as the unary minus. Other than this, the set of operators and their associated precedence have not been changed.</p>

<h2>Run-Time Library</h2>

<p>The standard C library has been enhanced with extra functions proposed by ANSI. Many of these functions are already available in many compilers, most notably Microsoft which has very close association with the ANSI committee.</p>

<h2>Conclusion</h2>

<p>In these few pages, I have attempted to outline the major extensions provided by the proposed ANSI C Standard. At the time of writing, the standard has still not appeared, and is not expected to for many more months. However, looking at the range of extensions supplied with the new Microsoft C V5.0, it would probably be safe to assume that all these extensions will appear in the final standard with little or no change.</p>

<p>A more complete coverage of the proposals is given in the standard C reference manual for all serious C programmers:</p>

<p style="margin-left: 1em;">C: A Reference Manual. Samuel P.Harbison. Guy L.Steele.<br>
Prentice Hall Software Series. Second Edition.</p>

<p>The book costs over &pound;20, but is very good value for money. Make sure that you get the second edition.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
