* X more docs for C
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes.int / sx11a.html
blob33ad52b5acbf255564c506db0194a07351c2077d
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>25.1 Declaring ``varargs'' Functions</title>
10 <link href="sx11.html" rev=precedes>
11 <link href="sx11b.html" rel=precedes>
12 <link href="sx11.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>25.1 Declaring ``varargs'' Functions</H2>
17 <p>The ANSI/ISO C Standard requires that
18 all functions which accept a variable number of arguments
19 be declared explicitly to do so,
20 and also that a function prototype be ``in scope''
21 (that is, available) whenever a varargs function is called.
22 You may remember that,
23 under certain circumstances,
24 unknown functions can be called at will,
25 with the compiler assuming
26 that they take ``normal'' arguments
27 and return <TT>int</TT>.
28 However,
29 that exception
30 does not apply here.
31 (In other words, for varargs functions,
32 function prototypes aren't just a good idea, they're the law.)
33 The reason
34 that prototypes are strictly required
35 is that varargs functions may use special calling sequences;
36 that is,
37 the compiler may have to generate special code for these calls.
38 </p><p>The presence of a variable-length argument list is indicated
39 by an <dfn>ellipsis</dfn> in the prototype.
40 For example, the prototype for <TT>printf</TT>,
41 as found in <TT>&lt;stdio.h&gt;</TT>,
42 looks something like this:
43 <pre>
44 extern int printf(const char *, ...);
45 </pre>
46 Those three dots <TT>...</TT> don't mean that I left something out;
47 they <em>are</em> the ellipsis notation;
48 this is the syntax that C uses
49 to indicate the presence of a variable-length argument list.
50 This prototype says that <TT>printf</TT>'s first argument
51 is of type <TT>const char *</TT>,
52 and that it takes a variable
53 (and hence unspecified)
54 number of additional arguments.
55 </p><p>The ellipsis notation must follow
56 the ordinary, fixed arguments,
57 and there must be at least one fixed argument.
58 It is impossible in Standard C to define a function
59 which accepts only variable arguments.
60 (In general, this is not too much of a restriction,
61 because the function must always be able to determine,
62 for itself,
63 how many arguments there are in the list,
64 invariably
65 by inspecting the first argument(s) in the list.
66 In other words,
67 the function will generally need at least one
68 well-defined, fixed argument anyway,
69 to get a toehold on the problem of figuring out
70 what the other arguments are.)
71 </p><hr>
72 <p>
73 Read sequentially:
74 <a href="sx11.html" rev=precedes>prev</a>
75 <a href="sx11b.html" rel=precedes>next</a>
76 <a href="sx11.html" rev=subdocument>up</a>
77 <a href="top.html">top</a>
78 </p>
79 <p>
80 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
81 // <a href="copyright.html">Copyright</a> 1996-1999
82 // <a href="mailto:scs@eskimo.com">mail feedback</a>
83 </p>
84 </body>
85 </html>