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. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>1.2 Second Example
</title>
10 <link href=
"sx1a.html" rev=precedes
>
11 <link href=
"sx1c.html" rel=precedes
>
12 <link href=
"sx1.html" rev=subdocument
>
15 <H2>1.2 Second Example
</H2>
17 <p>Our second example is of little more practical use than the first,
18 but it introduces a few more programming language elements:
20 #include
<stdio.h
>
22 /* print a few numbers, to illustrate a simple loop */
28 for(i =
0; i
< 10; i = i +
1)
29 printf(
"i is %d\n", i);
34 As before, the line
<TT>#include
<stdio.h
></TT> is
38 necessary since we're calling the
<TT>printf
</TT>
39 function, and
<TT>main()
</TT> and the pair of braces
40 <TT>{}
</TT> indicate and delineate the function named
41 <TT>main
</TT> we're (again) writing.
42 </p><p>The first new line is the line
44 /* print a few numbers, to illustrate a simple loop */
46 which is a
<dfn>comment
</dfn>.
47 Anything between the characters
<TT>/*
</TT> and
<TT>*/
</TT>
48 is ignored by the compiler, but may be useful to a person
49 trying to read and understand the program.
50 You can add comments anywhere you want to in the program,
51 to document what the program is, what it does,
54 what the various functions are for and how
<em>they
</em> work,
55 what the various variables are for,
57 </p><p>The second new line, down within the function
<TT>main
</TT>,
62 which
<dfn>declares
</dfn> that our function will use a
63 variable named
<TT>i
</TT>.
64 The variable's type is
<TT>int
</TT>,
65 which is a plain integer.
66 </p><p>Next, we set up a
<dfn>loop
</dfn>:
68 for(i =
0; i
< 10; i = i +
1)
70 The keyword
<TT>for
</TT> indicates that we are setting up a
71 ``
<TT>for
</TT> loop.''
73 is controlled by three expressions, enclosed in parentheses and
74 separated by semicolons.
75 These expressions say that,
77 the loop starts by setting
<TT>i
</TT> to
0,
78 that it continues as long as
<TT>i
</TT> is less than
10,
79 and that after each iteration of the loop,
80 <TT>i
</TT> should be incremented by
1
81 (that is, have
1 added to its value).
82 </p><p>Finally, we have a call to the
<TT>printf
</TT> function,
83 as before, but with several differences.
84 First, the call to
<TT>printf
</TT> is within the
<dfn>body
</dfn>
85 of the
<TT>for
</TT> loop.
86 This means that control flow does not pass once through the
87 <TT>printf
</TT> call, but instead that the call is performed
88 as many times as are dictated by the
<TT>for
</TT> loop.
89 In this case,
<TT>printf
</TT> will be called several times:
90 once when
<TT>i
</TT> is
0,
91 once when
<TT>i
</TT> is
1,
92 once when
<TT>i
</TT> is
2,
93 and so on until
<TT>i
</TT> is
9,
94 for a total of
10 times.
95 </p><p>A second difference in the
<TT>printf
</TT> call is that the
96 string to be printed,
<TT>"i is %d"</TT>, contains a percent sign.
97 Whenever
<TT>printf
</TT> sees a percent sign,
101 that
<TT>printf
</TT> is not supposed to print the exact text
102 of the string, but is instead supposed to read another one of
103 its arguments to decide what to print.
104 The letter after the percent sign tells it
105 what type of argument to expect and how to print it.
106 In this case, the letter
<TT>d
</TT> indicates that
107 <TT>printf
</TT> is to expect an
<TT>int
</TT>, and to print
109 Finally, we see that
<TT>printf
</TT> is in fact being called
110 with another argument, for a total of two, separated by commas.
111 The second argument is the variable
<TT>i
</TT>, which is in
112 fact an
<TT>int
</TT>, as required by
<TT>%d
</TT>.
113 The effect of all of this is that each time it is called,
114 <TT>printf
</TT> will print
116 the current value of the variable
<TT>i
</TT>:
123 </p><p>After several trips through the loop,
124 <TT>i
</TT> will eventually equal
9.
125 After that trip through the loop,
126 the third control expression
127 <TT>i = i +
1</TT> will increment its
130 The condition
<TT>i
< 10</TT> is no longer true,
131 so no more trips through the loop are taken.
132 Instead, control flow jumps down to the statement following the
133 <TT>for
</TT> loop, which is the
<TT>return
</TT> statement.
134 The
<TT>main
</TT> function returns, and the program is finished.
138 <a href=
"sx1a.html" rev=precedes
>prev
</a>
139 <a href=
"sx1c.html" rel=precedes
>next
</a>
140 <a href=
"sx1.html" rev=subdocument
>up
</a>
141 <a href=
"top.html">top
</a>
144 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
145 //
<a href=
"copyright.html">Copyright
</a> 1995-
1997
146 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>