Você está na página 1de 4

This chapter presents an overview of the origins, uses, and philosophy of the C

programming language.
The Origins of the C Language
Dennis Ritchie invented and first implemented the C programming language on
a DEC PDP-11 that used the UNIX operating system. The language is the result of
a
development process that started with an older language called BCPL. Martin Rich
ards
developed BCPL, which influenced Ken Thompson’s invention of a language called B
,
which 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 Nati
onal
Standards Institute) standard that would define the C language. The standardizat
ion
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 (Internat
ional
Standards Organization), and the resulting standard was typically referred to as
ANSI/ISO Standard C, or simply ANSI/ISO C. In 1995, Amendment 1 to the C standar
d
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. This is the version of C that C++ Builder supports.
It must be noted that recently a new standard for C, called C99, has been create
d.
For the most part, it leaves the features of C89 intact and adds a few new ones.
However, C++ Builder does not support the new features added by C99. This is
not surprising because at the time of this writing, no commonly available compil
er
supports C99, and C89 still describes what programmers think of as C. Furthermor
e,
as just explained, it is the C89 version of C that forms the C subset of C++. Be
cause
the version of C supported by C++ and C++ Builder is C89, it is the version of C
described in this book. (The interested reader can find a full description of th
e C99
standard in C: The Complete Reference, 4th Ed. by Herbert Schildt, Berkeley:
Osborne/McGraw-Hill, 2000.)
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 Pa
scal;
nor does it imply that C is similar to, or presents the problems associated with
,
assembly language. The definition of C as a middle-level language means that it
combines elements of high-level languages with the functionalism of assembly
language. Table 1-1 shows how C fits into the spectrum of 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 fac
t,
C code is surprisingly portable. Portability means that it is possible to adapt
software
written for one type of computer to another. For example, if a program written f
or
one type of CPU can be moved easily to another, that program is portable.
All high-level programming languages support the concept of data types. A data
type defines a set of values that a variable can store along with a set of opera
tions that
can be performed on that variable. Common data types are integer, character, and
real.
Although C has several basic built-in data types, it is not a strongly typed lan
guage like
Pascal or Ada. In fact, C will allow almost all type conversions. For example, c
haracter
and integer types may be freely intermixed in most expressions. Traditionally C
performs
no run-time error checking such as array-boundary checking or argument-type
compatibility checking. These checks are the responsibility of the programmer.
A special feature of C is that it allows the direct manipulation of bits, bytes,
words,
and pointers. This makes it well suited for system-level programming, where thes
e
operations are common. Another important aspect of C is that it has only 32 keyw
ords
(5 more were added by C99, but these are not supported by C++), which are the
commands that make up the C language. This is far fewer than most other language
s.
C h a p t e r 1 : A n O v e r v i e w o f C 5
THE FOUNDATION
OF C++
Highest level Ada
Modula-2
Pascal
COBOL
FORTRAN
BASIC
Middle level C#
Java
C++
C
FORTH
Macro-assembly language
Lowest level Assembly language
Table 1-1. C’s Place in the World of Languages
6 B o r l a n d C + + B u i l d e r : T h e C o m p l e t e R e f e r e n c e
A Structured Language
In your previous programming experience, you may have heard the term “block
structured” applied to a computer language. Although the term block-structured
language does not strictly apply to C, C is commonly referred to simply as a str
uctured
language. Technically, a block-structured language permits procedures or functio
ns to
be declared inside other procedures or functions. Since C does not allow the cre
ation
of functions within functions, it cannot formally be called block structured.
The distinguishing feature of a structured language is compartmentalization of c
ode
and data. Compartmentalization is the language’s ability to section off and hide
from
the rest of the program all information and instructions necessary to perform a
specific
task. One way of achieving compartmentalization is to use subroutines that emplo
y
local (temporary) variables. By using local variables, the programmer can write
subroutines so that the events that occur within them cause no side effects in o
ther
parts of the program. This capability makes it very easy for C programs to share
sections of code. If you develop compartmentalized functions, you only need to k
now
what a function does, not how it does it. Remember that excessive use of global
variables (variables known throughout the entire program) may allow bugs to cree
p
into a program by allowing unwanted side effects. (Anyone who has programmed in
traditional BASIC is well aware of this problem!)
The concept of compartmentalization is greatly expanded by C++. Specifically, in
C++, one part of your program can tightly control which other parts of your prog
ram
are allowed access.
A structured language allows a variety of programming possibilities. It directly
supports several loop constructs, such as while, do-while, and for. In a structu
red
language, the use of goto is either prohibited or discouraged and is not the com
mon
form of program control that it is in old-style BASIC or traditional FORTRAN. A
structured language allows you to indent statements and does not require a stric
t
field concept.
Here are some examples of structured and nonstructured languages:
Structured Nonstructured
Pascal FORTRAN
Ada BASIC
C++ COBOL
C
C#
Modula-2
Java
C h a p t e r 1 : A n O v e r v i e w o f C 7
THE FOUNDATION
OF C++
Structured languages are newer; nonstructured languages are older. Today, few
programmers would seriously consider a nonstructured language for new software
development.
New versions of many older languages have attempted to add structured elements.
BASIC is an example. However, the shortcomings of these languages can never be f
ully
mitigated because they were not designed with structured features from the start
.
The main structural component of C is the function—C’s stand-alone subroutine.
In C, functions are the building blocks in which all program activity occurs. Th
ey allow
the separate tasks in a program to be defined and coded separately, thus allowin
g your
programs to be modular. After a function has been created, you can rely on it to
work
properly in various situations, without creating side effects in other parts of
the
program. The fact that you can create stand-alone functions is extremely critica
l in
larger projects where one programmer’s code must not accidentally affect another
’s.
Another way to structure and compartmentalize code in C is to use code blocks. A
code block is a logically connected group of program statements that is treated
as a unit.
In C a code block is created by placing a sequence of statements between opening
and
closing curly braces. In this example,
if(x < 10) {
printf("too low, try again");
reset_counter(-1);
}
the two statements after the if and between the curly braces are both executed i
f x is
less than 10. These two statements together with the braces are a code block. Th
ey are
a logical unit: one of the statements cannot execute without the other. Code blo
cks not
only allow many algorithms to be implemented with clarity, elegance, and efficie
ncy,
but also help the programmer conceptualize the true nature of the routine.
A Programmer’s Language
One might respond to the statement, “C is a programmer’s language,” with the
question, “Aren’t all programming languages for programmers?” The answer is an
unqualified “No!” Consider the classic examples of nonprogrammers‘ languages,
COBOL and BASIC. COBOL was designed to enable nonprogrammers to read
and, presumably, understand a program. BASIC was created essentially to allow
nonprogrammers to program a computer to solve relatively simple problems.
In contrast, C was created, influenced, and field-tested by real working program
mers.
The end result is that C gives the programmer what the programmer wants: few
restrictions, few complaints, block structures, stand-alone functions, and a com
pact set

Você também pode gostar