1.1 Getting Started
The only way to learn a new programming language is by writing programs in it. The first
program to write is the same for all languages:
Print the words
hello, world
This is a big hurdle; to leap over it you have to be able to create the program text somewhere,
compile it successfully, load it, run it, and find out where your output went. With these
mechanical details mastered, everything else is comparatively easy.
In C, the program to print ``hello, world'' is
#include
main()
{
printf("hello, world\n");
}
Just how to run this program depends on the system you are using. As a specific example, on
the UNIX operating system you must create the program in a file whose name ends in ``.c'',
such as hello.c, then compile it with the command
cc hello.c
10
If you haven't botched anything, such as omitting a character or misspelling something, the
compilation will proceed silently, and make an executable file called a.out. If you run a.out
by typing the command
a.out
it will print
hello, world
On other systems, the rules will be different; check with a local expert.
Now, for some explanations about the program itself. A C program, whatever its size, consists
of functions and variables. A function contains statements that specify the computing
operations to be done, and variables store values used during the computation. C functions are
like the subroutines and functions in Fortran or the procedures and functions of Pascal. Our
example is a function named main. Normally you are at liberty to give functions whatever
names you like, but ``main'' is special - your program begins executing at the beginning of