The C Programming Language
COMPILE AND LINK
Prepare your source file with any editor. The name of the file should end with the characters ".c".
The C compiler is called using the command
In Unix, the compilation and linking is often accomplished using a single command.
For example, if we had a program named "score.c" the command
would compile the file "score.c" in the current working directory and create an object file named "score.o". The object file would then be linked to the run-time environment, creating the executable file "a.out". By default, the "score.o" is automatically removed.
To compile a file but not link it, use the "-c" flag. For example,
cc -c score.c
would compile "score.c", creating an object file named "score.o".
You would want to compile a file but not link it if the file was not a complete program, such as a function.
When you want to link the object files, use the "cc" command:
The result would be an executable file named "a.out"
LINKING THE OBJECT FILES AND NAMING THE EXECUTABLE
By default, the executable will be named "a.out". You can specify an alternative name with the "-o" flag. For example, let's say you have several C files, one for the main program ("main.c") and two functions ("func1.c" and "func2.c") called in the main program. To compile the files you would use the following command:
cc -c main.c func1.c func2.c
The result would be object files named "main.o", "func1.o" and "func2.o".
To then link the object files into an executable file named "zoom", you would use the following command:
cc main.o func1.o func2.o -o zoom
C is the language in which all UNIX system programs and most of the UNIX operating system are written. Like UNIX itself, C is simple, elegant and flexible. It also shares the primary disadvantage of UNIX - it is somewhat obscure.
Several books are available which present C "gently" to new and inexperienced programmers:
"Programming in C", Stephan Kochan, Hayden Book Co., 50 Essex St., Rochelle Park, N.J. 07662; revised edition July, 1988
"Learning to Program in C", Thomas Plum, Plum Hall Inc., 1 Spruce Ave., Cardiff, N.J. 08232
"The C Primer", Les Hancock and Morris Krieger,
McGraw-Hill, 1982. Assumes no previous programming
"The C Puzzle Book", Alan R. Feuer, Prentice-Hall, 1982. Full of puzzles to test and exercise your C knowledge; assumes you're reading another book such as the C Primer.
The authoritative source on the language is the original C book. Indeed, the stated purpose of the other books is to prepare readers for this one; experienced programmers may want to start directly with it:
"The C Programming Language", Brian W. Kernighan and Dennis M. Ritchie, second editon published March, 1988. Note that Ritchie wrote the C language itself; this book is straight from the source.
The Kernighan and Ritchie C book is supplemented by sections 2 and 3 of the UNIX Programmer's Manual (Volume 1), which document the rich system library of C subroutines, and by the following sections of Volume 2A:
"The C Programming Language - Reference Manual", which is reprinted from the book but includes an appendix of language updates since the book was published. Section 14 of Volume 2A.
"Lint, a C Program Checker", S. C. Johnson, 1978. Section 15 of Volume 2A.
"UNIX Programming - Second Edition", Brian W. Kernighan and Dennis M. Ritchie, 1978. Section 17 of Volume 2A.
Section 1 of the UNIX Manual Reference Guide (available on line in the "man"system) contains information about the commands relating to the use of C. In particular, you should read the following "man" pages:
cc Using the C compiler. ld Compile and load time options. ar Creating and maintaining archives of object modules.
For example, to read the man page on the C compiler, you would enter the command
For general information about system calls, refer to the introduction to section two of the UNIX Programmer"s Reference Manual (view it online with the command "man 2 intro"). For general information about subroutines, see the introduction to section three of the same manual (or "man 3 intro"). Section three also contains information about standard I/O (the C standard I/O library) under Berkeley Unix.