Materials:
A. History
C was developed in tandem with Unix. It's a bit of a chicken and the egg story, since Unix is mostly written in C but it was also the development environment for C.
At first Unix was written in assembly language on a PDP 7. Ken Thompson wrote the language "B" for that computer. B was based on the language BCPL which in turn descended from Algol.
C was developed on early Unix systems and then the nearly the entire Unix system was rewritten in C.
C and Unix became popular together. The fact that Unix was mostly written in C made it fairly easy to port to any machine with a C compiler. When many new computer manufacturers, especially of workstation computers such as Sun and Apollo first came along, it was a natural choice for them to use Unix as the OS since it meant that they could use an "off the shelf" OS which could be adapted to their hardware in a relatively short time.
C was designed from the ground up to be a low-level language -- one in which it was possible to work with data as the computer actually saw it. Thus C has all the atomic data types such as char (which is a byte), int (the machine's typical word size -- usually 16 or 32 bits), etc.
C is a small language. Besides the basic data types, it contains conditional constructs and looping constructs. It does not contain any IO instructions. For example, the writeln instruction is part of the Pascal language -- it has no analog in C. This smallness made porting C to new computer systems fairly easy.
Along with virtually every C compiler can be found a C standard library. It is this library that contains functions to handle IO and other operations required for programming tasks. The basic effect of this is to move much of the system dependency from the compiler to the implementation of the library.
B. Dialects and Standards
"Traditional" C was defined by the book The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie, Prentice-Hall, 1978. Kernighan and Ritchie were both at AT&T Bell Labs) and were both involved in the creation of Unix. From when it was published through the mid-1980s this book defined the core of the C language.
As vendor extensions to C began to appear, it was realized that a standard was needed. These extensions undercut one of C's most important assets -- its portability.
In 1982 an ANSI committee was formed to develop a standard for the C language and it's standard library. ANSI-compliant compilers started showing up by the late 1980s. Most, but not all, C compilers today implement the ANSI standard.
One of the major differences between K&R C (as the traditional dialect was often called) and ANSI-C was that in K&R C there was no way to prototype functions to force type-checking of their arguments and return values.
K&R C: Error not caught by compiler | ANSI-C: Error caught by compiler |
---|---|
main() { int i; ... foo( i ); ... } int foo( x ) float x; { ... } |
int foo( float x ); main( void ) { int i; ... foo( i ); ... } int foo( float x ) { ... } |
A. A C source file consists of a series of declarations and definitions.
type declarations - used to declare new types.
int my_array[20]; struct student { char name[20]; float gpa; };
(my_array is an array consisting of 20 integers; struct student is the name of a record type.)
variable declarations - used to declare variables and (in ANSI-C) as one way of declaring constants. C variables can be initialized when they are declared.
int i = 0;
function declarations/definitions
A function declaration spells out the interface to a function, but does not give its body. It consists of a function prototype followed by a semicolon.
int f( int );
A function definition consists of a function prototype followed by the function body.
int f( int i ) { return i * i; }
Note that C does not distinguish between functions and procedures. The C analog to a Pascal procedure is a function that doesn't return any value. In ANSI-C, this is handled by declaring the "procedure" as a function returning type void.
void p( void );
There is no special provision for a main program block, as in Pascal. Instead, every C program must contain exactly one function whose name is main - that function is the main program. It can occur anywhere within the source file(s).
The C rules for placement of declarations differ in some important ways from those of Pascal.
Declarations can appear in any order or mixture - subject to the requirement that something must be declared before it is used.
Variables and functions (but not types) can be declared more than once, provided the declarations are consistent. However:
Only one declaration for a given variable can initialize it. This is called its defining declaration.
A function can only be defined once.
Any C compound statement (sequence of statements enclosed in braces) can begin with one or more local variable declarations. This includes not only the body of a function, but also any nested compound statements within the body.
C functions cannot contain nested declarations or definitions of other functions. All C functions are declared at "top level".
B. Large C programs are typically written as a collection of separate source files, each declaring related functions and data, which are compiled separately.
To pass information between files, special header files (with names of the form something.h) are used.
For routines in the C-library, the necessary header files are available in a system header file library. For user-defined routines, the user can produce his own header files.
C. A complete C implementation consists of the C compiler proper, plus a C-preprocessor that handles certain pre-processor directives.
Directives to include the text of other files (typically header files) into the compilation at a certain point.
Definitions of symbolic constants and macros.
D. Some examples: HANDOUT - C PROGRAM DEMONSTRATIONS
E. Syntax details: C vs Pascal HANDOUT
A. As was noted earlier, one of the main strengths of the C language is the existence of a large, standardized library of functions. These functions perform a wide variety of tasks, including:
All input-output operations
Mathematical functions
Character string operations
Memory management and other system-related tasks
(On windowed systems) Screen window management.
B. Part of the ANSI standardization effort has been a standardization of this library. This is an important consideration in making C programs truly portable. Unfortunately, though, some system dependencies still exist.
$Id: c.html,v 1.2 2000/01/07 17:03:32 senning Exp $
These notes are based on a set of notes by Prof. R. Bjork, Gordon College and The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie, Prentice-Hall, 1978.