* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes / sx1c.html
blobd339d7c333313e5457422122f4e607cb99c49eac
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>1.3 Program Structure</title>
10 <link href="sx1b.html" rev=precedes>
11 <link href="sx2.html" rel=precedes>
12 <link href="sx1.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>1.3 Program Structure</H2>
17 <p>We'll have more to say later about program structure,
18 but for now let's observe a few basics.
19 A program consists of one or more functions;
20 it may also contain global variables.
21 (Our two example programs so far have contained one function apiece,
22 and no global variables.)
23 At the top of a source file are typically a few boilerplate
24 lines such as <TT>#include &lt;stdio.h&gt;</TT>,
25 followed by the definitions (i.e. code) for the functions.
26 (It's also possible to split up the several functions making up
27 a larger program into several source files, as we'll see in a
28 later chapter.)
29 </p><p>Each function is further composed of <dfn>declarations</dfn>
30 and <dfn>statements</dfn>, in that order.
31 When a sequence of statements should act as one
32 (for example,
33 when they should all serve together as the body of a loop)
34 they can be enclosed in braces
35 (just as for the outer body of the entire function).
37 The simplest kind of statement is an <dfn>expression statement</dfn>,
38 which is an expression
39 (presumably performing some useful operation)
40 followed
42 by a semicolon.
43 Expressions are further composed of <dfn>operators</dfn>, <dfn>objects</dfn>
44 (variables), and <dfn>constants</dfn>.
45 </p><p>C source code consists of several <dfn>lexical elements</dfn>.
46 Some are words, such as <TT>for</TT>, <TT>return</TT>,
47 <TT>main</TT>, and <TT>i</TT>, which are either
48 <dfn>keywords</dfn> of the language (<TT>for</TT>, <TT>return</TT>)
49 or <dfn>identifiers</dfn> (names) we've chosen for our own
50 functions and variables (<TT>main</TT>, <TT>i</TT>).
51 There are <dfn>constants</dfn> such as <TT>1</TT> and
52 <TT>10</TT> which introduce new values into the program.
53 There are <dfn>operators</dfn> such as <TT>=</TT>,
54 <TT>+</TT>, and <TT>&gt;</TT>, which manipulate variables
55 and values.
56 There are other punctuation characters
57 (often called <dfn>delimiters</dfn>),
58 such as parentheses and squiggly braces <TT>{}</TT>,
59 which indicate how the other elements of the program are grouped.
60 Finally, all of the preceding elements can be separated by
61 <dfn>whitespace</dfn>: spaces, tabs, and the ``carriage
62 returns'' between lines.
63 </p><p>The source code for a C program is, for the most part,
64 ``free form.''
65 This means that the compiler does not care how the code is arranged:
66 how it is broken into lines, how the lines are indented,
68 whether
69 whitespace is
70 used between things like variable names and other punctuation.
71 (Lines like <TT>#include &lt;stdio.h&gt;</TT>
72 are an exception;
73 they must appear alone on their own lines, generally unbroken.
74 Only lines beginning with <TT>#</TT> are affected by this rule;
76 we'll see other examples later.)
77 You can use whitespace, indentation, and appropriate line breaks
78 to make your programs more readable for yourself and other people
79 (even though the compiler doesn't care).
80 You can place explanatory <dfn>comments</dfn> anywhere in your
81 program--any text between the characters <TT>/*</TT> and
82 <TT>*/</TT> is ignored by the compiler.
83 (In fact, the compiler pretends that all it saw was whitespace.)
84 Though comments are ignored by the compiler,
85 well-chosen comments can make a program <em>much</em> easier to read
86 (for its author, as well as for others).
87 </p><p>The usage of whitespace is our first <dfn>style</dfn> issue.
88 It's typical to leave a blank line between different parts of
89 the program, to leave a space on either side of operators such
90 as <TT>+</TT> and <TT>=</TT>, and to <dfn>indent</dfn> the bodies of
91 loops and other control flow constructs.
92 Typically, we arrange the indentation so that
93 the subsidiary statements controlled by a loop statement
94 (the ``loop body,'' such as the <TT>printf</TT> call
95 in our second example program) are all aligned with each other and
96 placed one tab stop (or some consistent number of spaces) to
97 the right of the controlling statement.
98 This indentation (like all whitespace) is not required by the
99 compiler,
100 but it makes programs <em>much</em> easier to read.
101 (However, it can also be misleading, if used incorrectly or in
102 the face of inadvertent mistakes. The compiler will decide
103 what ``the body of the loop'' is based on its own
104 rules, not the indentation, so if the indentation does not
105 match the compiler's interpretation, confusion is inevitable.)
106 </p><p>To drive home the point that the compiler doesn't care about
107 indentation, line breaks, or other whitespace, here are a few
108 (extreme) examples:
109 The fragments
110 <pre>
111 for(i = 0; i &lt; 10; i = i + 1)
112 printf("%d\n", i);
113 </pre>
115 <pre>
116 for(i = 0; i &lt; 10; i = i + 1) printf("%d\n", i);
117 </pre>
119 <pre>
120 for(i=0;i&lt;10;i=i+1)printf("%d\n",i);
121 </pre>
123 <pre>
124 for(i = 0; i &lt; 10; i = i + 1)
125 printf("%d\n", i);
126 </pre>
128 <pre>
129 for ( i
130 = 0 ;
131 i &lt; 10
132 ; i =
133 i + 1
134 ) printf (
135 "%d\n" , i
137 </pre>
139 <pre>
141 (i=0;
142 i&lt;10;i=
143 i+1)printf
144 ("%d\n", i);
145 </pre>
146 are all treated exactly the same way by the compiler.
147 </p><p>Some programmers argue forever over
148 the best set of
149 ``rules''
150 for indentation and other aspects of
151 programming style,
152 calling to mind the old philosopher's debates about the number
153 of angels that could dance on the head of a pin.
154 Style issues
155 (such as how a program is laid out)
156 <em>are</em> important,
157 but they're not something to be too dogmatic about,
158 and there are
159 also
160 other, deeper style issues besides mere layout
161 and typography.
162 Kernighan and Ritchie take a fairly moderate stance:
163 <blockquote>Although
164 C compilers do not care about how a program looks,
165 proper indentation and spacing are critical
166 in making programs easy
167 for people to read.
168 We recommend writing only one statement per line,
169 and using blanks around operators to clarify grouping.
170 The position of braces is less important,
171 although people hold passionate beliefs.
172 We have chosen one of several popular styles.
173 Pick a style that suits you,
174 then use it consistently.
175 </blockquote></p><p>There is some value in having a reasonably standard style
176 (or a few standard styles)
177 for code layout.
178 Please don't take the above advice to
179 ``pick a style that suits you''
180 as an invitation to invent your own brand-new style.
182 (perhaps after you've been programming in C for a while)
183 you have specific objections to specific facets of existing styles,
184 you're welcome to modify them,
185 but if you don't have any particular leanings,
186 you're probably best off copying an existing style at first.
188 (If you want to place your own stamp of originality
189 on the programs that you write,
190 there are better avenues for your creativity than inventing a bizarre layout;
191 you might instead try to make the logic easier to follow,
192 or the user interface easier to use,
193 or the code freer of bugs.)
194 </p><hr>
196 Read sequentially:
197 <a href="sx1b.html" rev=precedes>prev</a>
198 <a href="sx2.html" rel=precedes>next</a>
199 <a href="sx1.html" rev=subdocument>up</a>
200 <a href="top.html">top</a>
201 </p>
203 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
204 // <a href="copyright.html">Copyright</a> 1995-1997
205 // <a href="mailto:scs@eskimo.com">mail feedback</a>
206 </p>
207 </body>
208 </html>