* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes / sx5b.html
blob087b338d7ec9a582a8de857b6039397ca001cf4e
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 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>5.2 Function Prototypes</title>
10 <link href="sx5a.html" rev=precedes>
11 <link href="sx5c.html" rel=precedes>
12 <link href="sx5.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>5.2 Function Prototypes</H2>
17 <p>In modern C programming,
18 it is considered good practice to use prototype declarations
19 for all functions that you call.
20 As we mentioned, these prototypes help to ensure that the
21 compiler can generate correct code for calling the functions,
22 as well as allowing the compiler to catch certain mistakes you
23 might make.
24 </p><p>Strictly speaking, however, prototypes are optional.
25 If you call a function for which the compiler has not seen a
26 prototype,
27 the compiler will do the best it can,
28 assuming that you're calling the function correctly.
29 </p><p>If prototypes are a good idea,
30 and if we're going to get in the habit of writing
31 function prototype declarations for
32 functions we call that we've written (such as <TT>multbytwo</TT>),
33 what happens for library functions such as <TT>printf</TT>?
34 Where are their prototypes?
35 The answer is in that boilerplate line
36 <pre>
37 #include &lt;stdio.h&gt;
38 </pre>
39 we've been including at the top of all of our programs.
40 <TT>stdio.h</TT> is conceptually a file full of
41 external declarations and other information
42 pertaining to the ``Standard I/O''
43 library functions, including <TT>printf</TT>.
44 The <TT>#include</TT> directive
45 (which we'll meet formally in a later chapter)
46 arranges that all of the declarations within <TT>stdio.h</TT>
47 are considered
49 by the compiler,
50 rather as if we'd typed them all in ourselves.
51 Somewhere within these declarations
52 is an external function prototype declaration for <TT>printf</TT>,
53 which satisfies the rule that there should be a prototype
54 for each function we call.
55 (For other standard library functions we call,
56 there will be other ``header files'' to include.)
57 Finally, one more thing about external function prototype declarations.
58 We've said that the distinction between external declarations and
59 defining instances of normal variables
60 hinges on the presence or absence of the keyword <TT>extern</TT>.
61 The situation is a little bit different for functions.
62 The ``defining instance'' of a function is the function,
63 including its body (that is, the brace-enclosed list of
64 declarations and statements implementing the function).
65 An external declaration of a function, even without the keyword
66 <TT>extern</TT>, looks nothing like a function declaration.
67 Therefore, the keyword <TT>extern</TT> is optional in
68 function prototype declarations.
69 If you wish,
70 you can write
71 <pre>
72 int multbytwo(int);
73 </pre>
74 and this is just as good an external function prototype
75 declaration as
76 <pre>
77 extern int multbytwo(int);
78 </pre>
79 (In the first form,
80 without the <TT>extern</TT>,
81 as soon as the compiler sees the semicolon, it knows it's not
82 going to see a function body, so the declaration can't be a definition.)
83 You may want to stay in the habit of using <TT>extern</TT>
84 in all external declarations, including function declarations,
85 since ``<TT>extern</TT> = external declaration''
86 is an easier rule to remember.
87 </p><hr>
88 <p>
89 Read sequentially:
90 <a href="sx5a.html" rev=precedes>prev</a>
91 <a href="sx5c.html" rel=precedes>next</a>
92 <a href="sx5.html" rev=subdocument>up</a>
93 <a href="top.html">top</a>
94 </p>
95 <p>
96 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
97 // <a href="copyright.html">Copyright</a> 1995, 1996
98 // <a href="mailto:scs@eskimo.com">mail feedback</a>
99 </p>
100 </body>
101 </html>