Case Study: The Design And Development Of C C Language: Motivation
Case Study: The Design and
Development of C
CS386L - Programming Languages
Dr. Greg Lavender
Department of Computer Sciences
The University of Texas at Austin
CS386L: Lecture-02 - Case Study: The Design & Development of C
C Language: motivation
designed as a systems implementation language
for the development of an operating system, compiler, &
utilities
created by a small group of software engineers at Bell
Labs in early 1970s
Ken Thompson, Dennis Richie, Brian Kernighan, et al.
born out of frustration with “big” OS projects and
large complex languages
Multics, PL/I, Algol 68
originally designed for small memory machines (PDP-7,
PDP-11)
design influenced by small machine architectural limitations
word size, small main memory size, small register set, etc.
later ported to mainframes & made “portable”
2/2/04
2
1
CS386L: Lecture-02 - Case Study: The Design & Development of C
C Language: influences
indirect: Fortran, Algol, PL/I, Algol68
direct: BCPL, B, NB (new B)
Ken Thompson invented B
B is C without types
B is derived from BCPL
BCPL invented at MIT by Martin Richards
B “… is BCPL squeezed into 8K bytes of memory…”
“… and filtered through [Ken] Thompson’s brain.”
BCPL, B and C all differ in some aspects of syntax &
semantics
B & C do not allow nested procedure scopes, BCPL does
all support separate compilation and allow “include” files
B is “simpler” than BCPL because of small memory machine
very compact syntax required so compiler could work in one pass
2/2/04
3
CS386L: Lecture-02 - Case Study: The Design & Development of C
C Language: syntax
BCPL used ‘:=‘ for assignment (like Algol)
B dropped the ‘:’ and just used the ‘=‘
how many times have you made the following mistake in
C/C++/Java !!
if (a = b) then … else …
B uses /*…*/ for comments, BCPL uses //
/*…*/ comes from PL/I
note that C++ re-introduced //
Bjarne Stroustrup used BCPL during his PhD research
B declarations begin with ‘auto’ or ‘static’. C
introduced type specifiers, but auto is optional and
assumed in C for local variables
static int x = 0;
int f() { auto int y; … }
2/2/04
4
2
CS386L: Lecture-02 - Case Study: The Design & Development of C
C Language: syntax
generalized assignment operations
BCPL/B/early C: x =+ y;
simplified lexical analysis
later C: x += y; // and the others: -=,*=,/=
pre/post increment operators: ++, --
appeared in B as a more compact syntax for parsing
++x: x = x + 1;
y--: y = y - 1;
Question: what is the meaning of ++x + x++ in C/C++?
TMG - TransMoGrifier
an early top-down recursive descent compiler-
compiler by McClure
lead to the development of lex/yacc in C
2/2/04
5
CS386L: Lecture-02 - Case Study: The Design & Development of C
C Language: semantics
B kept much of BCPL semantics
equivalence of pointers and arrays, pointer
arithmetic
BCPL: let V = vec 10
V!I to index to ith element
B/C: auto V[10]
*(V+i) or V[i] to index to ith element of an array
note: because of equivalence of pointers and arrays, no
auto array bounds checking as in Pascal & Java
has led to tens of thousands of programming errors
strings
BCPL: first byte contains the length
B: last byte contains a special “end of string” character
2/2/04
6
3
CS386L: Lecture-02 - Case Study: The Design & Development of C
C Language: pragmatics
B deficiencies motivated an improved compiler
B generated threaded-code, not native-code
basically, a byte-code interpreter using a stack
B used packed strings causing inefficient string manipulation
overhead in dealing with pointers
“New B” (NB) invented by Richie to be more efficient
introduced int, char & their array types, and pointer types
compiled to native code
C fully equated typed arrays with typed pointers
int a[10]; … x = a[i]; means x = (a+i*sizeof(int)) where a is
pointer to the first memory cell of the array
2/2/04
7
CS386L: Lecture-02 - Case Study: The Design & Development of C
C Language: types
C primarily improved on B by adding type
declarations & type rules
syntax influenced by Algol 68
int i, *pi, **ppi;
int f(), *f(), (*pf)();
int ai[10], *api[10], (*pai)[10];
also structs and unions
2/2/04
8
4
CS386L: Lecture-02 - Case Study: The Design & Development of C
C Language: standardization
C language definition complete in 1973
Unix kernel rewritten in C
new features added from 1973-1980
unsigned, long, union, enums
porting of C compiler to other machines
K&R C book published 1978
de facto language standard for 10 years
Explosion of C compilers in early 1980s
for mainframes, minicomputers, workstations & PCs
ANSI C standardization 1989
key additions:
function prototypes as in C++
const and volatile keywords introduced
but omitted the builtin ‘bool’ primitive as in C++ and Java
bool finally added to the 1999 update to the C Standard
see <stdbool.h>
2/2/04
9
CS386L: Lecture-02 - Case Study: The Design & Development of C
C Language: evolution
variants & descendants
Concurrent C, Objective C, C*, C++
Java, C#
used as a “portable assembly” language
early C++ and Modula-3 and Eiffel all “compile” to C
leverages back-end (code gen) of existing C compilers
but makes debugging more difficult
C compilers generate very efficient code
today
for all kinds of devices
at last count, GNU gcc generated native code for 70+
different instruction set architectures
most systems & networking software is written in C
2/2/04
10
5
CS386L: Lecture-02 - Case Study: The Design & Development of C
C Language: Challenge Problem!!
Write the shortest C program that prints an exact
syntactic replica of itself
you may not just print the original source file
this is an example of self-reference in a language
Douglas Hofstadter, in the book Godel, Escher, Bach, calls this
kind of program a “quine” after W.V.O Quine (1908-2000), a
famous philosopher/logician (see www.wvquine.org)
first correct and shortest solution wins a small prize!
submit your best solution to lavender@cs.utexas.edu
unix% gcc quine.c -o quine
unix% quine > quine.copy
unix% diff quine.c quine.copy
smallest C quine is 64 characters.
You are on your honor to solve the problem with no help from any
source other than your own mind.
challenge problems are not officially homework, but they are
very relevant to the course. For more fun, try writing a quine in
other languages (e.g., Scheme, ML, Java, Perl, …) and compare
them.
2/2/04
11
6