ch01 2
ch01 2
Second, it is robust. Over the last twenty-five years C has evolved into a sta-
ble, mature, language, controlled by a formal standard, and guided by a wealth
of practical experience. You can find information about the standard at http://
std.dkuug.dk/JTC1/SC22/WG14/, and purchase a copy of the current version,
ISO/IEC 9899:1999, at http://www.ansi.org.
Third, it is appropriate to a wide range of applications. For example, much of the
Unix operating system is written in C, and so are a wide range of programming and
other software tools. Commercial software houses use it for product development,
and hobbyists use it for their personal computing. And because its lineage stretches
back to Fortran, a wide range of software – old, but perhaps still useful – can be
rewritten in C and used if necessary, with a minimum of translation effort.
Finally, as a procedural language, there is a close mapping between constructs
in the language and the facilities available in the hardware it executes on. Because
of this close correspondence, a great deal of general-purpose programming work is
carried out using C and related languages – programs are efficient when written in C.
Advances both in programming language technology and in hardware have reduced
this link in recent years, but there is still a sense in which C is close to the raw
machine.
There are better languages for particular applications – just as a carpenter has
many saws at their disposal. But if you are going to use only one saw, it needs to be
a general-purpose one, a jack of all trades. And that is the role filled by C.
int
main(int argc, char **argv) {
printf("Hello world!\n");
return 0;
}
The program in the box doesn’t do much, nevertheless, it is always a useful task to
get this program working on any new computer system you encounter, and to also
write the equivalent program in any new language you must master.
There are a number of points to note. First, the text between the /* and */ pair
is discarded by the C system, and is purely for the benefit of any human readers – it
is a comment. Comments can be placed at almost any point of a program, and once
1.3 A FIRST C PROGRAM PAGE 7
The gcc command compiles the C source code in file helloworld.c using the C
compiler distributed by the Free Software Foundation2 , and creates an executable
file called helloworld that contains machine-language instructions corresponding
to the C source program. On a Windows system the executable would have a .exe
filename extension, but in Unix it is conventional to create executables that have no
extension. All of the examples in this book presume a Unix environment, as shown
in the example. The last command executes the compiled program, and the “Hello
world!” output message appears.
Figure 1.2 shows a second C program, and an execution of it. This one reads
a set of numbers, and calculates and prints their sum. It is presented to give you a
feel for what a more substantial program looks like, and you are not expected to have
yet mastered the intricacies of how it was put together (so please don’t panic!). But
with a bit of luck, by reading the code – and the comments – you can identify the
basic building blocks: some variable declarations; a while loop that gets the input
numbers one by one into variable next, and adds them onto a running total sum that
is at first set to the value zero; and a printf that prints out the value of sum.
2
See http://www.gnu.org.