diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gcc~13/function-prototypes.html | |
new repository
Diffstat (limited to 'devdocs/gcc~13/function-prototypes.html')
| -rw-r--r-- | devdocs/gcc~13/function-prototypes.html | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/devdocs/gcc~13/function-prototypes.html b/devdocs/gcc~13/function-prototypes.html new file mode 100644 index 00000000..04da0a63 --- /dev/null +++ b/devdocs/gcc~13/function-prototypes.html @@ -0,0 +1,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> |
