An Overview of C
The
purpose of this chapter is to present an overview of the C programming
language, its origins, its
uses,
and its underlying philosophy. This chapter is mainly for newcomers to C.
A
Brief History of C
C
was invented and first implemented by Dennis Ritchie on a DEC PDP-11 that used
the Unix
operating
system. C is the result of a development process that started with an older
language called
BCPL.
BCPL was developed by Martin Richards, and it influenced a language called B,
which was
invented
by Ken Thompson. B led to the development of C in the 1970s.
For
many years, the de facto standard for C was the version supplied with the Unix
operating
system.
It was first described in The C Programming Language by Brian Kernighan
and Dennis
Ritchie
(Englewood Cliffs, N.J.: Prentice-Hall, 1978). In the summer of 1983 a
committee was
established
to create an ANSI (American National Standards Institute) standard that would
define
the
C language. The standardization process took six years (much longer than anyone
reasonably
expected).
The
ANSI C standard was finally adopted in December 1989, with the first copies
becoming
available
in early 1990. The standard was also adopted by ISO (International Standards
Organization),
and the resulting standard was typically referred to as ANSI/ISO Standard C. In
1995,
Amendment 1 to the C standard was adopted, which, among other things, added
several new
library
functions. The 1989 standard for C, along with Amendment 1, became a base
document for
Standard
C++, defining the C subset of C++. The version of C defined by the 1989
standard is
commonly
referred to as C89.
During
the 1990s, the development of the C++ standard consumed most programmers'
attention.
However,
work on C continued quietly along, with a new standard for C being developed.
The end
result
was the 1999 standard for C, usually referred to as C99. In general, C99
retained nearly all of
the
features of C89. Thus, C is still C! The C99 standardization committee focused
on two main
areas:
the addition of several numeric libraries and the development of some
special-use, but highly
innovative,
new features, such as variable-length arrays and the restrict pointer
qualifier. These
innovations
have once again put C at the forefront of computer language development.
As
explained in the part opener, Part One of this book describes the foundation of
C, which is the
version
defined by the 1989 standard. This is the version of C in widest use, it is
currently accepted
by
all C compilers, and it forms the basis for C++. Thus, if you want to write C
code that can be
compiled
by a legacy compiler, for example, you will want to restrict that code to the
features
described
in Part One. Part Two will examine the features added by C99. C Is a
Middle-Level Language
C
is often called a middle-level computer language. This does not mean
that C is less powerful,
harder
to use, or less developed than a high-level language such as BASIC or Pascal,
nor does it
imply
that C has the cumbersome nature of assembly language (and its associated
troubles). Rather,
C
is thought of as a middle-level language because it combines the best elements
of high-level
languages
with the control and flexibility of assembly language. Table 1-1 shows how C
fits into the
spectrum
of computer languages.
As
a middle-level language, C allows the manipulation of bits, bytes, and
addresses— the basic
elements
with which the computer functions. Despite this fact, C code is also very
portable.
Portability
means
that it is easy to adapt software written for one type of computer or operating
system
to another type. For example, if you can easily convert a program written for
DOS so that it
runs
under Windows 2000, that program is portable.
High
level Ada
Modula-2
Pascal
COBOL
FORTRAN
BASIC
Middle
level Java
C++
C
FORTH
Macro-assembler
Low
level Assembler
A
Tutorial Introduction
Let us begin with a quick introduction in C. Our aim is to show
the essential elements of the language in real
programs, but without getting bogged down in details, rules, and
exceptions. At this point, we are not trying to be
complete or even precise (examples are meant to be correct). We
want to get you as quickly as
possible to the point where you can write useful programs, and to
do that we have to concentrate on the basics:
variables and constants, arithmetic, control flow, functions, and
the rudiments of input and output. We are
intentionally leaving out of this chapter features of C that are
important for writing bigger programs. These include
pointers, structures, most of C's rich set of operators, several
control-flow statements, and the standard library.
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 <stdio.h>
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
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
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 main. This
means
that every program must have a main somewhere.
main will usually call other functions to
help perform its job, some that you wrote, and others from libraries that
are
provided for you. The first line of the program,
#include <stdio.h>
tells
the compiler to include information about the standard input/output library;
the line appears at the beginning
of
many C source files.
One
method of communicating data between functions is for the calling function to
provide a list of values, called
arguments, to the
function it calls. The parentheses after the function name surround the
argument list. In this
example,
main is
defined to be a function that expects no arguments, which is indicated by the
empty list ( ).
#include <stdio.h> include
information about standard library
main() define a
function called main
that
received no argument values
{ statements of
main are enclosed in braces
printf("hello, world\n"); main
calls library function printf
to
print this sequence of characters
} \n represents the newline character
The
statements of a function are enclosed in braces { }. The function main contains only
one statement,
printf("hello, world\n");
A
function is called by naming it, followed by a parenthesized list of arguments,
so this calls the function printf
with
the argument "hello, world\n". printf
is
a library function that prints output, in this case the string
of
characters between the quotes.
A
sequence of characters in double quotes, like "hello,
world\n",
is called a character string or string
constant. For the moment
our only use of character strings will be as arguments for printf
and
other functions.
The
sequence \n in
the string is C notation for the newline character, which when printed
advances the output to
the
left margin on the next line. If you leave out the \n (a worthwhile
experiment), you will find that there is no
line
advance after the output is printed. You must use \n to include a
newline character in the printf argument;
if
you try something like
printf("hello, world
");
the
C compiler will produce an error message.
printf never supplies a newline character
automatically, so several calls may be used to build up an output line
in
stages. Our first program could just as well have been written
#include <stdio.h>
main()
{
printf("hello, ");
printf("world");
printf("\n");
}
to
produce identical output.