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.1 A First Example
</title>
10 <link href=
"sx1.html" rev=precedes
>
11 <link href=
"sx1b.html" rel=precedes
>
12 <link href=
"sx1.html" rev=subdocument
>
15 <H2>1.1 A First Example
</H2>
17 <p>[This section corresponds to K
&R Sec.
1.1]
18 </p><p>The best way to learn programming
19 is to dive right in and start writing real programs.
22 which would otherwise seem abstract
24 and the positive feedback you get from getting even a small
26 gives you a great incentive to improve it or write the next one.
27 </p><p>Diving in with ``real'' programs right away has
30 if you're using a conventional compiler,
31 you can't run a fragment of a program and see what it does;
32 nothing will run until you have a complete
33 (if tiny or trivial) program.
34 You can't learn everything you'd need to write a complete
36 so you'll have to take some things ``on faith'' and
37 parrot them in your first programs before you begin to
39 (You can't learn to program just one expression or statement at
40 a time any more than you can learn to speak a foreign language
42 If all you know is a handful of words,
43 you can't actually
<em>say
</em> anything:
44 you also need to know something about the language's word order
45 and grammar and sentence structure and declension of articles
47 </p><p>Besides the occasional necessity to take things on faith,
48 there is a more serious potential drawback
51 it's a small step from learning-by-doing
52 to learning-by-trial-and-error,
53 and when you learn programming by trial-and-error,
54 you can very easily learn many errors.
55 When you're not sure whether something will work,
56 or you're not even sure what you could use that might work,
57 and you try something,
59 you do
<em>not
</em> have any guarantee that what you tried
60 worked for the right reason.
61 You might just have ``learned'' something that works only by
63 or only on your compiler,
64 and it may be very hard to un-learn it later,
65 when it stops working.
67 whenever you're not sure of something,
68 be very careful before you go off and try it
69 ``just to see if it will work.''
70 Of course, you can never be absolutely sure that something is
71 going to work before you try it,
73 otherwise we'd never have to try things.
76 have an expectation that something is going
77 to work before you try it,
78 and if you can't predict how to do something or whether
79 something would work and find yourself having to determine it
81 make a note in your mind that whatever you've just learned
82 (based on the outcome of the experiment)
84 </p><p>The first example program in K
&R is the first example
85 program in any language:
86 print or display a simple string, and exit.
87 Here is my version of K
&R's ``hello, world'' program:
89 #include
<stdio.h
>
93 printf(
"Hello, world!\n");
97 If you have a C compiler, the first thing to do is figure out
98 how to type this program in and compile it and run it and see
99 where its output went.
100 (If you don't have a C compiler yet, the first thing to do
102 </p><p>The first line is practically boilerplate;
103 it will appear in almost all programs we write.
104 It asks that some definitions
105 having to do with the ``Standard I/O Library''
106 be included in our program;
107 these definitions are needed
108 if we are to call the library function
<TT>printf
</TT> correctly.
109 </p><p>The second line says that we are defining a function named
<TT>main
</TT>.
110 Most of the time, we can name our functions anything we want,
111 but the function name
<TT>main
</TT> is special:
112 it is the function that will be ``called'' first when
113 our program starts running.
114 The empty pair of parentheses indicates that our
<TT>main
</TT>
115 function accepts no
<dfn>arguments
</dfn>, that is,
116 there isn't any information which needs to be passed in when
117 the function is called.
118 </p><p>The braces
<TT>{
</TT> and
<TT>}
</TT>
119 surround a list of statements in C.
120 Here, they surround the list of statements
121 making up the function
<TT>main
</TT>.
124 printf(
"Hello, world!\n");
126 is the first statement in the program.
127 It asks that the function
<TT>printf
</TT> be called;
128 <TT>printf
</TT> is a library function which prints formatted output.
129 The parentheses surround
<TT>printf
</TT>'s argument list:
130 the information which is handed to it
131 which it should act on.
132 The semicolon at the end of the line terminates the statement.
133 </p><p>(
<TT>printf
</TT>'s name reflects the fact
134 that C was first developed when Teletypes and other printing terminals
135 were still in widespread use.
136 Today, of course, video displays are far more common.
137 <TT>printf
</TT>'s ``prints''
138 to the
<dfn>standard output
</dfn>,
139 that is, to the default location for program output to go.
141 that's almost always a video screen or a window on that screen.
142 If you do have a printer,
143 you'll typically have to do something extra
144 to get a program to print to it.)
145 </p><p><TT>printf
</TT>'s first
146 (and, in this case, only)
147 argument is the string which it should print.
148 The string, enclosed in double quotes
<TT>""</TT>,
149 consists of the words ``Hello, world!''
150 followed by a special sequence:
<TT>\n
</TT>.
151 In strings, any two-character sequence beginning with the
152 backslash
<TT>\
</TT> represents a single special character.
153 The sequence
<TT>\n
</TT> represents the
154 ``new line'' character,
155 which prints a carriage return or line
156 feed or whatever it takes to end one line of output and move
158 (This program only prints one line of output,
159 but it's still important to terminate it.)
161 line in the
<TT>main
</TT> function is
165 In general, a function may return a value to its caller, and
166 <TT>main
</TT> is no exception.
167 When
<TT>main
</TT> returns
168 (that is, reaches its end and stops functioning),
169 the program is at its end,
170 and the return value from
<TT>main
</TT> tells the operating system
171 (or whatever invoked the program that
<TT>main
</TT> is the main function of)
172 whether it succeeded or not.
173 By convention, a return value of
0 indicates success.
174 </p><p>This program may look so absolutely trivial that it seems as if
175 it's not even worth typing it in and trying to run it,
176 but doing so may be a big
177 (and is certainly a vital)
180 an unfamiliar computer,
181 it can be arbitrarily difficult to figure out how to enter a
182 text file containing program source,
183 or how to compile and link it,
185 or what happened after (if?) it ran.
186 The most experienced C programmers immediately go back to this
187 one, simple program whenever they're trying out a new system
188 or a new way of entering or building programs
189 or a new way of printing output from within programs.
190 As Kernighan and Ritchie say,
191 everything else is comparatively easy.
192 </p><p>How
<em>you
</em> compile and run this (or any) program is a
193 function of the compiler and operating system you're using.
194 The first step is to type it in, exactly as shown;
195 this may involve using a text editor to create a file
196 containing the program text.
197 You'll have to give the file a name, and all C compilers (that
198 I've ever heard of) require that files containing C source end
199 with the extension
<TT>.c
</TT>.
200 So you might place the program text in a file called
<TT>hello.c
</TT>.
201 </p><p>The second step is to compile the program.
202 (Strictly speaking, compilation consists of two steps,
203 compilation proper followed by linking, but we can overlook
204 this distinction at first,
205 especially because the compiler often takes care of
206 initiating the linking step automatically.)
207 On many Unix systems, the command to compile a C program from
208 a source file
<TT>hello.c
</TT> is
212 You would type this command at the Unix shell prompt, and it
214 requests that the
<TT>cc
</TT> (C compiler) program be run,
215 placing its output (i.e. the new executable program it creates)
216 in the file
<TT>hello
</TT>, and taking its input (i.e. the source
217 code to be compiled) from the file
<TT>hello.c
</TT>.
218 </p><p>The third step is to run (execute, invoke) the newly-built
<TT>hello
</TT>
220 Again on a Unix system, this is done simply by typing the
225 Depending on how your system is set up
226 (in particular, on whether the current directory is searched
227 for executables, based on the PATH variable),
232 to indicate that the
<TT>hello
</TT> program is in the current
234 (as opposed to some ``
<TT>bin
</TT>'' directory full of
235 executable programs, elsewhere).
236 </p><p>You may also have your choice of C compilers.
237 On many Unix machines, the
<TT>cc
</TT> command
238 is an older compiler which does not recognize modern,
239 ANSI Standard C syntax.
240 An old compiler will accept the simple programs
241 we'll be starting with,
242 but it will not accept most of our later programs.
243 If you find yourself getting baffling compilation errors on
244 programs which you've typed in exactly as they're shown,
245 it probably indicates that you're using an older compiler.
246 On many machines, another compiler called
248 <TT>gcc
</TT> is available,
249 and you'll want to use it, instead.
250 (Both
<TT>acc
</TT> and
<TT>gcc
</TT> are typically invoked the same
251 as
<TT>cc
</TT>; that is, the above
<TT>cc
</TT> command would instead
252 be typed, say,
<TT>gcc -o hello hello.c
</TT> .)
253 </p><p>(One final caveat about Unix systems: don't name your test programs
254 <TT>test
</TT>, because there's already a standard command called
255 <TT>test
</TT>, and you and the command interpreter will get badly
256 confused if you try to replace the system's
<TT>test
</TT> command
257 with your own, not least because your own almost certainly does
258 something completely different.)
259 </p><p>Under MS-DOS, the compilation procedure is quite similar.
260 The name of the command you type will depend on your compiler
261 (e.g.
<TT>cl
</TT> for the Microsoft C compiler,
262 <TT>tc
</TT> or
<TT>bcc
</TT> for Borland's Turbo C, etc.).
263 You may have to manually perform the second, linking step,
264 perhaps with a command named
<TT>link
</TT> or
<TT>tlink
</TT>.
265 The executable file which the compiler/linker creates will have
266 a name ending in
<TT>.exe
</TT> (or perhaps
<TT>.com
</TT>),
267 but you can still invoke it by typing the base name (e.g.
<TT>hello
</TT>).
268 See your compiler documentation for complete details;
269 one of the manuals should contain a demonstration of how to
270 enter, compile, and run a small program that
271 prints some simple output,
272 just as we're trying to describe here.
273 </p><p>In an integrated or ``visual''
275 progamming environment,
276 such as those on the Macintosh or under various versions of
278 the steps you take to enter, compile, and run a program are
279 somewhat different (and, theoretically, simpler).
280 Typically, there is a way to open a new source window, type
283 and add it to the program
286 If necessary, there will be a way to specify what other source
287 files (or ``modules'') make up the program.
288 Then, there's a button or menu selection which compiles and
289 runs the program, all from within the programming environment.
290 (There will also be a way to create a standalone executable
291 file which you can run from outside the environment.)
292 In a PC-compatible environment, you may have to choose between
293 creating DOS programs or Windows programs.
294 (If you have troubles pertaining to the
<TT>printf
</TT>
295 function, try specifying a target environment of MS-DOS.
296 Supposedly, some compilers which are targeted at Windows
297 environments won't let you call
<TT>printf
</TT>, because until you
298 call some fancier functions to request that a window be
299 created, there's no window for
<TT>printf
</TT> to print to.)
300 Again, check the introductory or tutorial manual that came with
301 the programming package; it should walk you through the steps
302 necessary to get your first program running.
306 <a href=
"sx1.html" rev=precedes
>prev
</a>
307 <a href=
"sx1b.html" rel=precedes
>next
</a>
308 <a href=
"sx1.html" rev=subdocument
>up
</a>
309 <a href=
"top.html">top
</a>
312 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
313 //
<a href=
"copyright.html">Copyright
</a> 1995-
1997
314 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>