Programming in C _ A Tutorial
Bell Laboratories, Murray Hill, N. J.
operating systems at Murray Hill and (in preliminary form) on OS/360 at Holmdel.
C lets you write your programs clearly and simply _
it has decent control flow facilities so your code can be read
straight down the page, without labels or GOTO's;
it lets you write code that is compact without
it encourages modularity and good program organization;
and it provides good data-structuring facilities.
This memorandum is a tutorial to make learning C as painless as possible.
The first part concentrates on
the central features of C;
the second part discusses
those parts of the language which are
useful (usually for getting more efficient
but which are not necessary for the new user.
Details and special cases will be skipped ruthlessly,
and no attempt will be made to cover every language feature.
The order of presentation is hopefully pedagogical
Users who would like the full story should consult the
which should be read for details anyway.
Runtime support is described in
you will have to read one of these to learn how
to compile and run a C program.
We will assume that you are familiar with the mysteries of
text editing, and the like
in the operating system you run on,
and that you have programmed in some language before.
A C program consists of one or more
the functions and subroutines of a Fortran program or the procedures
and perhaps some external data definitions.
is such a function, and in fact all C programs must have a
Execution of the program begins at the first statement of
will usually invoke other functions to perform its job, some
coming from the same program, and others from libraries.
One method of communicating data between functions
The parentheses following the function name surround the argument list;
is a function of no arguments, indicated by (~).
The {} enclose the statements of the function.
Individual statements end with a semicolon
but are otherwise free-format.
is a library function which will format and print
on the terminal (unless some other destination is
A function is invoked by naming it,
followed by a list of arguments in parentheses.
statement as in Fortran or
A Working C Program; Variables; Types and Type Declarations
Here's a bigger program that adds three integers and prints their sum.
printf("sum is %d", sum);
Arithmetic and the assignment statements are much
the same as in Fortran (except for the semicolons)
The format of C programs is quite free.
We can put several statements on a line if we want,
or we can split a statement among several lines if
it seems desirable. The split may be between any of the operators or variables,
in the middle of a name or operator.
spaces, tabs, and newlines should be used freely
\fGint\fR integer (PDP-11: 16 bits; H6070: 36 bits; IBM360: 32 bits)
\fGchar\fR one byte character (PDP-11, IBM360: 8 bits; H6070: 9 bits)
\fGfloat\fR single-precision floating point
\fGdouble\fR double-precision floating point
all of which we will meet shortly.
variables in a C program must be declared,
although this can sometimes be done implicitly by context.
Declarations must precede executable statements.
Variable names have one to eight characters, chosen from A-Z, a-z, 0-9, and \(ul,
and start with a non-digit.
Stylistically, it's much better to use only a single case
and give functions and external variables names that are unique in the first
(Function and external variable names are used by various assemblers, some of which are limited
in the size and case of identifiers they can handle.)
Furthermore, keywords and library functions
may only be recognized in one case.
We have already seen decimal integer constants in
Since C is often used for system programming and bit-manipulation, octal
numbers are an important part of the language.
In C, any number that begins with 0
is an octal integer (and hence can't have
Thus 0777 is an octal constant, with decimal value 511.
A ``character'' is one byte
(an inherently machine-dependent concept).
Most often this is expressed as a
which is one character enclosed in single quotes.
However, it may be any quantity that fits in a byte,
char quest, newline, flags;
The sequence `\\n' is C notation for ``newline character'', which, when printed, skips
the terminal to the beginning of the next line.
Notice that `\\n' represents only a single character.
There are several other ``escapes'' like `\\n' for representing hard-to-get or invisible
`\\\\' for the backslash itself.
are discussed in section 26.
Simple I/O _ getchar, putchar, printf
are the basic I/O library functions in C.
each time it is called, and returns that character
When it reaches the end of whatever file it is reading,
thereafter it returns the character represented by `\\0'
We will see how to use this very shortly.
puts one character out on the standard output
reads one character and writes it back out.
By itself, this isn't very interesting,
but observe that if we put a loop around this,
and add a test for end of file,
we have a complete program for
copying one file to another.
is a more complicated function
for producing formatted output.
We will talk about only the simplest use
uses its first argument as formatting information,
and any successive arguments
as variables to be output.
printf ("hello, world\\n");
the string ``hello, world\\n''
No formatting information, no variables,
so the string is dumped out verbatim.
The newline is necessary to put this out on a line by itself.
More about this shortly.)
printf ("sum is %d\\n", sum);
Within the first argument of
the characters ``%d'' signify that the next argument
in the argument list is to be printed as a
Other useful formatting commands are ``%c'' to print out a single character,
``%s'' to print out an entire string,
and ``%o'' to print a number as octal instead of decimal
printf ("What is the value of %d in octal?", n);
printf (" %s! %d decimal is %o octal\\n", "Right", n, n);
What is the value of 511 in octal?
Right! 511 decimal is 777 octal
Notice that there is no newline at the end of the first
simply put out characters.
No newlines are printed unless you ask for them.
Similarly, on input, characters are read one at a time
Each line is generally terminated by a newline (\\n),
but there is otherwise no concept of record.