source: public/doc/gnu-c/Complete-Explanation.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.4 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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&rsquo;s the explanation of the code of the example in the
66previous 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>.
72Therefore, the example program defines a function named <code>main</code> to
73provide a way to start it. Whatever that function does is what the
74program 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
77runs, but it doesn&rsquo;t come first in the example code. The order of the
78function definitions in the source code makes no difference to the
79program&rsquo;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
83arguments, define <code>main</code> with <code>void</code> as the parameter list.
84(<code>void</code> as a function&rsquo;s parameter list normally means &ldquo;call with
85no arguments,&rdquo; but <code>main</code> is a special case.)
86</p>
87<p>The function <code>main</code> returns 0 because that is
88the conventional way for <code>main</code> to indicate successful execution.
89It could instead return a positive integer to indicate failure, and
90some utility programs have specific conventions for the meaning of
91certain 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>
95function, 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
100function <code>printf</code> copies most of that string directly as output,
101including the newline character at the end of the string, which is
102written as &lsquo;<samp>\n</samp>&rsquo;. The output goes to the program&rsquo;s <em>standard
103output</em> destination, which in the usual case is the terminal.
104</p>
105<p>&lsquo;<samp>%</samp>&rsquo; in the template introduces a code that substitutes other text
106into the output. Specifically, &lsquo;<samp>%d</samp>&rsquo; means to take the next
107argument to <code>printf</code> and substitute it into the text as a decimal
108number. (The argument for &lsquo;<samp>%d</samp>&rsquo; must be of type <code>int</code>; if it
109isn&rsquo;t, <code>printf</code> will malfunction.) So the output is a line that
110looks 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
117it is defined by the C library, which makes it available in all C
118programs. However, each program does need to <em>declare</em>
119<code>printf</code> so it will be called correctly. The <code>#include</code>
120line takes care of that; it includes a <em>header file</em> called
121<samp>stdio.h</samp> into the program&rsquo;s code. That file is provided by the
122operating system and it contains declarations for the many standard
123input/output functions in the C library, one of which is
124<code>printf</code>.
125</p>
126<p>Don&rsquo;t worry about header files for now; we&rsquo;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
130constant; it can be any string (see <a href="Strings.html">Strings</a>). However, using a
131constant is the most common case.
132</p>
133<p>To learn more about <code>printf</code> and other facilities of the C
134library, 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
135Reference Manual</cite>.
136</p>
137<hr>
138<div class="header">
139<p>
140Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.