1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<div class="section-level-extent" id="Function-Prototypes"> <div class="nav-panel"> <p> Next: <a href="c_002b_002b-comments" accesskey="n" rel="next">C++ Style Comments</a>, Previous: <a href="attribute-syntax" accesskey="p" rel="prev">Attribute Syntax</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="section" id="Prototypes-and-Old-Style-Function-Definitions"><span>6.40 Prototypes and Old-Style Function Definitions<a class="copiable-link" href="#Prototypes-and-Old-Style-Function-Definitions"> ¶</a></span></h1> <p>GNU C extends ISO C to allow a function prototype to override a later old-style non-prototype definition. Consider the following example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">/* <span class="r">Use prototypes unless the compiler is old-fashioned.</span> */
#ifdef __STDC__
#define P(x) x
#else
#define P(x) ()
#endif
/* <span class="r">Prototype function declaration.</span> */
int isroot P((uid_t));
/* <span class="r">Old-style function definition.</span> */
int
isroot (x) /* <span class="r">??? lossage here ???</span> */
uid_t x;
{
return x == 0;
}</pre>
</div> <p>Suppose the type <code class="code">uid_t</code> happens to be <code class="code">short</code>. ISO C does not allow this example, because subword arguments in old-style non-prototype definitions are promoted. Therefore in this example the function definition’s argument is really an <code class="code">int</code>, which does not match the prototype argument type of <code class="code">short</code>. </p> <p>This restriction of ISO C makes it hard to write code that is portable to traditional C compilers, because the programmer does not know whether the <code class="code">uid_t</code> type is <code class="code">short</code>, <code class="code">int</code>, or <code class="code">long</code>. Therefore, in cases like these GNU C allows a prototype to override a later old-style definition. More precisely, in GNU C, a function prototype argument type overrides the argument type specified by a later old-style definition if the former type is the same as the latter type before promotion. Thus in GNU C the above example is equivalent to the following: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int isroot (uid_t);
int
isroot (uid_t x)
{
return x == 0;
}</pre>
</div> <p>GNU C++ does not support old-style function definitions, so this extension is irrelevant. </p> </div> <div class="nav-panel"> <p> Next: <a href="c_002b_002b-comments">C++ Style Comments</a>, Previous: <a href="attribute-syntax">Attribute Syntax</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
<p class="_attribution-p">
© Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
<a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Function-Prototypes.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Function-Prototypes.html</a>
</p>
</div>
|