1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Complete Explanation (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Complete Explanation (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Complete Explanation (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Complete-Program.html" rel="up" title="Complete Program">
|
---|
33 | <link href="Complete-Line_002dby_002dLine.html" rel="next" title="Complete Line-by-Line">
|
---|
34 | <link href="Complete-Example.html" rel="prev" title="Complete Example">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Complete-Explanation"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Complete-Line_002dby_002dLine.html" accesskey="n" rel="next">Complete Line-by-Line</a>, Previous: <a href="Complete-Example.html" accesskey="p" rel="prev">Complete Example</a>, Up: <a href="Complete-Program.html" accesskey="u" rel="up">Complete Program</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Complete-Program-Explanation"></span><h3 class="section">2.2 Complete Program Explanation</h3>
|
---|
64 |
|
---|
65 | <p>Here’s the explanation of the code of the example in the
|
---|
66 | previous section.
|
---|
67 | </p>
|
---|
68 | <p>This sample program prints a message that shows the value of <code>fib
|
---|
69 | (20)</code>, and exits with code 0 (which stands for successful execution).
|
---|
70 | </p>
|
---|
71 | <p>Every C program is started by running the function named <code>main</code>.
|
---|
72 | Therefore, the example program defines a function named <code>main</code> to
|
---|
73 | provide a way to start it. Whatever that function does is what the
|
---|
74 | program does. See <a href="The-main-Function.html">The main Function</a>.
|
---|
75 | </p>
|
---|
76 | <p>The <code>main</code> function is the first one called when the program
|
---|
77 | runs, but it doesn’t come first in the example code. The order of the
|
---|
78 | function definitions in the source code makes no difference to the
|
---|
79 | program’s meaning.
|
---|
80 | </p>
|
---|
81 | <p>The initial call to <code>main</code> always passes certain arguments, but
|
---|
82 | <code>main</code> does not have to pay attention to them. To ignore those
|
---|
83 | arguments, define <code>main</code> with <code>void</code> as the parameter list.
|
---|
84 | (<code>void</code> as a function’s parameter list normally means “call with
|
---|
85 | no arguments,” but <code>main</code> is a special case.)
|
---|
86 | </p>
|
---|
87 | <p>The function <code>main</code> returns 0 because that is
|
---|
88 | the conventional way for <code>main</code> to indicate successful execution.
|
---|
89 | It could instead return a positive integer to indicate failure, and
|
---|
90 | some utility programs have specific conventions for the meaning of
|
---|
91 | certain numeric <em>failure codes</em>. See <a href="Values-from-main.html">Values from main</a>.
|
---|
92 | </p>
|
---|
93 | <span id="index-printf"></span>
|
---|
94 | <p>The simplest way to print text in C is by calling the <code>printf</code>
|
---|
95 | function, so here we explain what that does.
|
---|
96 | </p>
|
---|
97 | <span id="index-standard-output"></span>
|
---|
98 | <p>The first argument to <code>printf</code> is a <em>string constant</em>
|
---|
99 | (see <a href="String-Constants.html">String Constants</a>) that is a template for output. The
|
---|
100 | function <code>printf</code> copies most of that string directly as output,
|
---|
101 | including the newline character at the end of the string, which is
|
---|
102 | written as ‘<samp>\n</samp>’. The output goes to the program’s <em>standard
|
---|
103 | output</em> destination, which in the usual case is the terminal.
|
---|
104 | </p>
|
---|
105 | <p>‘<samp>%</samp>’ in the template introduces a code that substitutes other text
|
---|
106 | into the output. Specifically, ‘<samp>%d</samp>’ means to take the next
|
---|
107 | argument to <code>printf</code> and substitute it into the text as a decimal
|
---|
108 | number. (The argument for ‘<samp>%d</samp>’ must be of type <code>int</code>; if it
|
---|
109 | isn’t, <code>printf</code> will malfunction.) So the output is a line that
|
---|
110 | looks like this:
|
---|
111 | </p>
|
---|
112 | <div class="example">
|
---|
113 | <pre class="example">Fibonacci series item 20 is 6765
|
---|
114 | </pre></div>
|
---|
115 |
|
---|
116 | <p>This program does not contain a definition for <code>printf</code> because
|
---|
117 | it is defined by the C library, which makes it available in all C
|
---|
118 | programs. However, each program does need to <em>declare</em>
|
---|
119 | <code>printf</code> so it will be called correctly. The <code>#include</code>
|
---|
120 | line takes care of that; it includes a <em>header file</em> called
|
---|
121 | <samp>stdio.h</samp> into the program’s code. That file is provided by the
|
---|
122 | operating system and it contains declarations for the many standard
|
---|
123 | input/output functions in the C library, one of which is
|
---|
124 | <code>printf</code>.
|
---|
125 | </p>
|
---|
126 | <p>Don’t worry about header files for now; we’ll explain them later in
|
---|
127 | <a href="Header-Files.html">Header Files</a>.
|
---|
128 | </p>
|
---|
129 | <p>The first argument of <code>printf</code> does not have to be a string
|
---|
130 | constant; it can be any string (see <a href="Strings.html">Strings</a>). However, using a
|
---|
131 | constant is the most common case.
|
---|
132 | </p>
|
---|
133 | <p>To learn more about <code>printf</code> and other facilities of the C
|
---|
134 | library, see <a href="https://www.gnu.org/software/libc/manual/html_node/index.html#Top">The GNU C Library</a> in <cite>The GNU C Library
|
---|
135 | Reference Manual</cite>.
|
---|
136 | </p>
|
---|
137 | <hr>
|
---|
138 | <div class="header">
|
---|
139 | <p>
|
---|
140 | Next: <a href="Complete-Line_002dby_002dLine.html" accesskey="n" rel="next">Complete Line-by-Line</a>, Previous: <a href="Complete-Example.html" accesskey="p" rel="prev">Complete Example</a>, Up: <a href="Complete-Program.html" accesskey="u" rel="up">Complete Program</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
141 | </div>
|
---|
142 |
|
---|
143 |
|
---|
144 |
|
---|
145 | </body>
|
---|
146 | </html>
|
---|