C



C is called a high level, compiler language. The aim of any high level
computer language is to provide an easy and natural way of giving a pro-
gramme of instructions to a computer (a computer program). The language
of the raw computer is a stream of numbers called machine code. As you
might expect, the action which results from a single machine code instruc-
tion is very primitive and many thousands of them are required to make a
program which does anything substantial. It is therefore the job of a high
level language to provide a new set of black box instructions, which can be
given to the computer without us needing to see what happens inside them
– and it is the job of a compiler to fill in the details of these “black boxes”

so that the final product is a sequence of instructions in the language of the
computer.
C is one of a large number of high level languages which can be used for
general purpose programming, that is, anything from writing small programs
for personal amusement to writing complex applications. It is unusual in
several ways. Before C, high level languages were criticized by machine
code programmers because they shielded the user from the working details
of the computer, with their black box approach, to such an extent that
the languages become inflexible: in other words, they did not not allow
programmers to use all the facilities which the machine has to offer. C, on
the other hand, was designed to give access to any level of the machine down

to raw machine code and because of this it is perhaps the most flexible of all high level languages.

Surprisingly, programming books often ignore an important role of high
level languages: high level programs are not only a way to express instruc-
tions to the computer, they are also a means of communication among hu-
man beings. They are not merely monologues to the machine, they are a
way to express ideas and a way to solve problems. The C language has
been equipped with features that allow programs to be organized in an easy
and logical way. This is vitally important for writing lengthy programs
because complex problems are only manageable with a clear organization
and program structure. C allows meaningful variable names and meaningful
function names to be used in programs without any loss of efficiency and it
gives a complete freedom of style; it has a set of very flexible loop construc-tions (for, while, do) and

neat ways of making decisions. These provide an

excellent basis for controlling the flow of programs.
Another unusual feature of C is the way it can express ideas concisely.
The richness of a language shapes what it can talk about. C gives us the
apparatus to build neat and compact programs. This sounds, first of all,
either like a great bonus or something a bit suspect. Its conciseness can
be a mixed blessing: the aim is to try to seek a balance between the often
conflicting interests of readability of programs and their conciseness. Because

this side of programming is so often presumed to be understood, we shall
try to develop a style which finds the right balance.
C allows things which are disallowed in other languages: this is no defect,
but a very powerful freedom which, when used with caution, opens up possi-
bilities enormously. It does mean however that there are aspects of C which
can run away with themselves unless some care is taken. The programmer
carries an extra responsibility to write a careful and thoughtful program.
The reward for this care is that fast, efficient programs can be produced.
C tries to make the best of a computer by linking as closely as possi-
ble to the local environment. It is no longer necessary to have to put up
with hopelessly inadequate input/output facilities anymore (a legacy of the
timesharing/mainframe computer era): one can use everything that a com-
puter has to offer. Above all it is flexible. Clearly no language can guarantee
intrinsically good programs: there is always a responsibility on the program-
mer, personally, to ensure that a program is neat, logical and well organized,
but it can give a framework in which it is easy to do so.
The aim of this book is to convey some of the C philosophy in a prac-
tical way and to provide a comprehensive introduction to the language by
appealing to a number of examples and by sticking to a strict structuring
scheme. It is hoped that this will give a flavour of the kind of programming
which C encourages.

Basic ideas about C:-
What to do with a compiler. What can go wrong.
Using a compiler language is not the same as using an interpreted lan-
guage like BASIC or a GNU shell. It differs in a number of ways. To begin
with, a C program has to be created in two stages:
• Firstly, the program is written in the form of a number of text files using
a screen editor. This form of the program is called the source program.
It is not possible to execute this file directly.
• Secondly, the completed source file is passed to a compiler—a program
which generates a new file containing a machine code translation of the
source text. This file is called an object file or executable file. The
executable file is said to have been compiled from the source text.
Compiler languages do not usually contain their own editor, nor do they
have words like ‘RUN’ with which to execute a finished program. You use a
screen editor to create the words of a program (program text) and run the
final program in its compiled form usually by simply typing the name of the
executable file.


A C program is made by running a compiler which takes the typed source
program and converts it into an object file that the computer can execute. A
compiler usually operates in two or more phases (and each phase may have
stages within it). These phases must be executed one after the other. As we

shall see later, this approach provides a flexible way of compiling programs
which are split into many files.
A two-phase compiler works in the following way:

• Phase 1 scans a source program, perhaps generating an intermediate
code (quadruples or pcode) which helps to simplify the grammar of the
language for subsequent processing. It then converts the intermediate
code into a file of object code (though this is usually not executable
yet). A separate object file is built for each separate source file. In the
GNU C compiler, these two stages are run with the command gcc -c;
the output is one or more .o files.
• Phase 2 is a Linker. This program appends standard library code to
the object file so that the code is complete and can “stand alone”. A C
compiler linker suffers the slightly arduous task of linking together all
the functions in the C program. Even at this stage, the compiler can
fail, if it finds that it has a reference to a function which does not exist.
With the GNU C compiler this stage is activated by the command gcc
-o or ld.
To avoid the irritation of typing two or three separate commands (which
are often cumbersome) you will normally find a simple interface for execut-
ing compiler. Traditionally this is an executable program called cc for C
Compiler:
cc filename
gcc filename
On GNU systems, this results in the creation of an executable program
with the default name a.out. To tell the compiler what you would like the
executable program to be called, use the -o option for setting the name of
the object code:
gcc -o program-name filname
For example, to create a program called ‘myprog’ from a file called myprog.c,
write
gcc -o myprog myprog.c

Errors:-
Errors are mistakes which we the programmers make. There are different
kinds of error:
Syntax
Errors in the syntax, or word structure of a program are caught
before you run it, at compilation time by the compiler program.
They are listed all in one go, with the line number, in the text
file, at which the error occurred and a message to say what was
wrong.
For example, suppose you write sin (x) y = ; in a program in-
stead of y = sin (x);, which assigns the value of the sin of ‘x’
to ‘y’. Upon compilation, you would see this error message:

Use of Upper and Lower Case

eg.c: In function ‘main’:
eg.c:12: parse error before ‘y’
(If you compile the program in Emacs, you can jump directly to
the error.)
A program with syntax errors will cause a compiler program
to stop trying to generate machine code and will not create an
executable. However, a compiler will usually not stop at the
first error it encounters but will attempt to continue checking
the syntax of a program right to the last line before aborting,
and it is common to submit a program for compilation only to
receive a long and ungratifying list of errors from the compiler.
It is a shock to everyone using a compiler for the first time how
a single error can throw the compiler off course and result in a
huge and confusing list of non-existent errors, following a single
true culprit. The situation thus looks much worse than it really
is. You’ll get used to this with experience, but it can be very
disheartening.
As a rule, look for the first error, fix that, and then recompile.
Of course, after you have become experienced, you will recognize
when subsequent error messages are due to independent prob-
lems and when they are due to a cascade. But at the beginning,
just look for and fix the first error.
Intention
Errors in goal or purpose (logical errors) occur when you write
a program that works, but does not do what you intend it to
do. You intend to send a letter to all drivers whose licenses
will expire soon; instead, you send a letter to all drivers whose
licenses will expire sometime.
If the compilation of a program is successful, then a new file is created.
This file will contain machine code which can be executed according to the
rules of the computer’s local operating system.
When a programmer wants to make alterations and corrections to a C
program, these have to be made in the source text file itself using an editor;
the program, or the salient parts, must then be recompiled.

Use of Upper and Lower Case:-
One of the reasons why the compiler can fail to produce the executable file
for a program is you have mistyped something, even through the careless
use of upper and lower case characters. The C language is case dependent.
Unlike languages such as Pascal and some versions of BASIC, the C compiler
distinguishes between small letters and capital letters. This is a potential
source of quite trivial errors which can be difficult to spot. If a letter is

typed in the wrong case, the compiler will complain and it will not produce
an executable program.

Declarations
Compiler languages require us to make a list of the names and types of all
variables which are going to be used in a program and provide information
about where they are going to be used. This is called declaring variables. It
serves two purposes: firstly, it provides the compiler with a definitive list of
the variables, enabling it to cross check for errors, and secondly, it informs
the compiler how much space must be reserved for each variable when the
program is run. C supports a variety of variable types (variables which hold
different kinds of data) and allows one type to be converted into another.
Consequently, the type of a variable is of great importance to the compiler.
If you fail to declare a variable, or declare it to be the wrong type, you will
see a compilation error.

What is a compiler?
How is a C program run?
How is a C program compiled usually?
Are upper and lower case equivalent in C?
What the two different kinds of error which can be in a program?

Reserved words and an example:-
C programs are constructed from a set of reserved words which provide
control and from libraries which perform special functions. The basic in-
structions are built up using a reserved set of words, such as ‘main’, ‘for’,
‘if’,‘while’, ‘default’, ‘double’, ‘extern’, ‘for’, and ‘int’, to name just a
few. These words may not be used in just any old way: C demands that
they are used only for giving commands or making statements. You cannot
use ‘default’, for example, as the name of a variable. An attempt to do so
will result in a compilation error.
See undefined [All the Reserved Words], page undefined , for a com-
plete list of the reserverd words.
Words used in included libaries are also, effectively, reserved. If you use
a word which has already been adopted in a library, there will be a conflict
between your choice and the library.
Libraries provide frequently used functionality and, in practice, at least
one library must be included in every program: the so-called C library, of
standard functions. For example, the ‘stdio’ library, which is part of the C
library, provides standard facilities for input to and output from a program.
In fact, most of the facilities which C offers are provided as libraries that
are included in programs as plug-in expansion units. While the features
provided by libraries are not strictly a part of the C language itself, they
are essential and you will never find a version of C without them. After a
library has been included in a program, its functions are defined and you
cannot use their names.

The printf() function:-
One invaluable function provided by the standard input/output library is
called printf or ‘print-formatted’. It provides an superbly versatile way of
printing text. The simplest way to use it is to print out a literal string:
printf (“..some string…”);
Text is easy, but we also want to be able to print out the contents of variables.
These can be inserted into a text string by using a ‘control sequence’ inside
the quotes and listing the variables after the string which get inserted into
the string in place of the control sequence. To print out an integer, the
control sequence %d is used:
printf (“Integer = %d”,someinteger);
The variable someinteger is printed instead of ‘%d’. The printf function
is described in full detail in the relevant chapter, but we’ll need it in many
places before that. The example program below is a complete program. If
you are reading this in Info, you can copy this to a file, compile and execute
it.

/***********************************************************/
/* Short Poem*/
/***********************************************************/
#include <stdio.h>
/***********************************************************/
main ()
{
printf
printf
printf
printf
printf
printf
printf
printf
printf
printf
printf
printf
}
/* Poem */
(“Astronomy is %dderful \n”,1);
(“And interesting %d \n”,2);
(“The ear%d volves around the sun \n”,3);
(“And makes a year %d you \n”,4);
(“The moon affects the sur %d heard \n”,5);
(“By law of phy%d great \n”,6);
(“It %d when the the stars so bright \n”,7);
(“Do nightly scintill%d \n”,8);
(“If watchful providence be%d \n”,9);
(“With good intentions fraught \n”);
(“Should not keep up her watch divine \n”);
(“We soon should come to %d \n”,0);

 Output
Astronomy is 1derful \n”
And interesting 2
The ear3 volves around the sun
And makes a year 4 you
The moon affects the sur 5 heard
By law of phy6d great
It 7 when the the stars so bright
Do nightly scintill8
If watchful providence be9
With good intentions fraught
Should not keep up her watch divine
We soon should come to 0

Write a command to print out the message “Wow big deal”.
Write a command to print out the number 22?
Write two commands to print out “The 3 Wise Men” two different ways.
Why are there only a few reserved command words in C?

Operating systems and environments:-
Where is a C program born? How is it created?
The basic control of a computer rests with its operating system. This
is a layer of software which drives the hardware and provides users with a
comfortable environment in which to work. An operating system has two
main components which are of interest to users: a user interface (often a
command language) and a filing system. The operating system is the route
to all input and output, whether it be to a screen or to files on a disk. A
programming language has to get at this input and output easily so that
programs can send out and receive messages from the user and it has to
be in contact with the operating system in order to do this. In C the link
between these two is very efficient.
Operating systems vary widely but most have a command language or
shell which can be used to type in commands. Recently the tendency has
been to try to eliminate typing completely by providing graphical user in-
terfaces (GUIs) for every purpose. GUIs are good for carrying out simple
procedures like editing, but they are not well suited to giving complicated
instructions to a computer. For that one needs a command language. In the
network version of this book we shall concentrate on Unix shell commands
since they are the most important to programmers. On microcomputers
command languages are usually very similar in concept, though more prim-
itive, with only slightly different words for essentially the same commands.
(This is a slightly superficial view).
When most compiler languages were developed, they were intended to
be run on large mainframe computers which operated on a multi-user, time-
sharing principle and were incapable of interactive communication with the
user. Many compiler languages still have this inadequacy when carried over
to modern computers, but C is an exception, because of its unique design.
Input and output are not actually defined as a fixed, unchanging part of
the C language. Instead there is a standard file which has to be included
in programs and defines the input/output commands that are supported by
the language for a particular computer and operating system. This file is
called a standard C library. (See the next chapter for more information.)
The library is standard in the sense that C has developed a set of functions
which all computers and operating systems must implement, but which are
specially adapted to your system.

The filing system is also a part of input/output. In many operating systems
all routes in and out of the computer are treated by the operating system
as though they were files or data streams (even the keyboard!). C does
this implicitly (it comes from Unix). The file from which C normally gets its

input from is called stdin or standard input file and it is usually the keyboard.
The corresponding route for output is called “stdout” or standard output file
and is usually a monitor screen. Both of these are parts of stdio or standard
input output. The keyboard and the monitor screen are not really files, of
course, they are ‘devices’, (it is not possible to re-read what has been sent
to the monitor”, or write to the keyboard.), but devices are represented by
files with special names, so that the keyboard is treated as a read-only file,
the monitor as a write only file… The advantage of treating devices like this
is that it is not necessary to know how a particular device works, only that
it exists somewhere, connected to the computer, and can be written to or
read from. In other words, it is exactly the same to read or write from a
device as it is to read or write from a file. This is a great simplification of
input/output! The filenames of devices (often given the lofty title ‘pseudo
device names’) depend upon your particular operating system. For instance,
the printer might be called “PRN” or “PRT”. You might have to open it
explicitly as a file. When input is taken solely from the keyboard and output
is always to the screen then these details can just be forgotten.

Filenames:-
The compiler uses a special convention for the file names, so that we do
not confuse their contents. The name of a source program (the code
which you write) is ‘filename.c’. The compiler generates a file of ob-
ject code from this called ‘filename.o’, as yet unlinked. The final pro-
gram, when linked to libraries is called ‘filename ’ on Unix-like operating
systems, and ‘filename.EXE’ on Windows derived systems. The libraries
themselves are also files of object code, typically called ‘liblibraryname.a’
or ‘liblibraryname.so’. Header files are always called ‘libname.h’.
The endings ‘dot something’ (called file extensions) identify the contents
of files for the compiler. The dotted endings mean that the compiler can
generate an executable file with the same name as the original source – just
a different ending. The quad file and the object file are only working files
and should be deleted by the compiler at the end of compilation. The ‘.c’
suffix is to tell the compiler that the file contains a C source program and
similarly the other letters indicate non-source files in a convenient way. To
execute the compiler you type,
cc filename
For example,
cc foo.c

Command Languages and Consoles

In order to do anything with a compiler or an editor you need to know a
little about the command language of the operating system. This means the
instructions which can be given to the system itself rather than the words
which make up a C program. e.g.

In a large operating system (or even a relatively small one) it can be a major
feat of recollection to know all of the commands. Fortunately it is possible
to get by with knowing just handful of the most common ones and having
the system manual around to leaf through when necessary.
Another important object is the ‘panic button’ or program interruption
key. Every system will have its own way of halting or terminating the op-
eration of a program or the execution of a command. Commonly this will
involve two simultaneous key presses, such as CTRL C, CTRL Z or CTRL-D etc.
In GNU/Linux, CTRL-C is used.

Questions
1. What is an operating system for?
2. What is a pseudo-device name?
3. If you had a C source program which you wanted to call ‘accounts’ what
name would you save it under?
4. What would be the name of the file produced by the compiler of the
program in 3?
5. How would this program be run?

Libraries:-

Plug-in C expansions. Header files.
The core of the C language is small and simple. Special functionality
is provided in the form of libraries of ready-made functions. This is what
makes C so portable. Some libraries are provided for you, giving you access
to many special abilities without needing to reinvent the wheel. You can also
make your own, but to do so you need to know how your operating system
builds libraries. We shall return to this later.
Libraries are files of ready-compiled code which we can merge with a C
program at compilation time. Each library comes with a number of asso-
ciated header files which make the functions easier to use. For example,
there are libraries of mathematical functions, string handling functions and
input/output functions and graphics libraries. It is up to every programmer
to make sure that libraries are added at compilation time by typing an op-
tional string to the compiler. For example, to merge with the math library
‘libm.a’ you would type
cc -o program_name prog.c -lm
when you compile the program. The ‘-lm’ means: add in ‘libm’. If we
wanted to add in the socket library ‘libsocket.a’ to do some network pro-
gramming as well, we would type
cc -o program_name prog.c -lm -lsocket
and so on.
Why are these libraries not just included automatically? Because it would
be a waste for the compiler to add on lots of code for maths functions, say,
if they weren’t needed. When library functions are used in programs, the
appropriate library code is included by the compiler, making the resulting
object code often much longer.
Libraries are supplemented by header files which define macros, data
types and external data to be used in conjunction with the libraries. Once a
header file has been included, it has effectively added to the list of reserved
words and commands in the language. You cannot then use the names of
functions or macros which have already been defined in libraries or header
files to mean anything other than what the library specifies.
The most commonly used header file is the standard input/output library
which is called ‘stdio.h’. This belongs to a subset of the standard C library
which deals with file handling. The ‘math.h’ header file belongs to the math-
ematics library ‘libm.a’. Header files for libraries are included by adding to
the source code:

#include header.h
at the top of a program file. For instance:
#include “myheader.h”
includes a personal header file which is in the current directory. Or
#include <stdio.h>
includes a file which lies in a standard directory like ‘/usr/include’.
The #include directive is actually a command to the C preprocessor,
which is dealt with more fully later, See Chapter 12 [Preprocessor], page 71.
Some functions can be used without having to include library files or
special libraries explicitly since every program is always merged with the
standard C library, which is called ‘libc’.
#include <stdio.h>
main ()
{
printf (“C standard I/O file is included\n”);
printf (“Hello world!”);
}
A program wishing to use a mathematical function such as cos would need
to include a mathematics library header file.
#include <stdio.h>
#include <math.h>
main ()
{ double x,y;
y = sin (x);
printf (“Maths library ready”);
}
A particular operating system might require its own special library for
certain operations such as using a mouse or for opening windows in a GUI
environment, for example. These details will be found in the local manual
for a particular C compiler or operating system.
Although there is no limit, in principle, to the number of libraries which
can be included in a program, there may be a practical limit: namely mem-
ory, since every library adds to the size of both source and object code.

Libraries also add to the time it takes to compile a program. Some operat-
ing systems are smarter than others when running programs and can load in
only what they need of the large libraries. Others have to load in everything
before they can run a program at all, so many libraries would slow them
down.
To know what names libraries have in a particular operating system you
have to search through its documentation. Unix users are lucky in having
an online manual which is better than most written ones.

Questions

1. How is a library file incorporated into a C program?
2. Name the most common library file in C.
3. Is it possible to define new functions with the same names as standard
library functions?
4. What is another name for a library file?

Programming style:-

The shape of programs to come.
C is actually a free format language. This means that there are no rules
about how it must be typed, when to start new lines, where to place brackets
or whatever. This has both advantages and dangers. The advantage is that
the user is free to choose a style which best suits him or her and there is
freedom in the way in which a program can be structured. The disadvantage
is that, unless a strict style is adopted, very sloppy programs can be the
result. The reasons for choosing a well structured style are that:
• Long programs are manageable only if programs are properly organized.
• Programs are only understandable if care is taken in choosing the names
of variables and functions.
• It is much easier to find parts of a program if a strict ordering convention
is maintained. Such a scheme becomes increasingly difficult to achieve
with the size and complexity of the problem.
No simple set of rules can ever provide the ultimate solution to writing
good programs. In the end, experience and good judgement are the factors
which decide whether a program is written well or poorly written. The main
goal of any style is to achieve clarity. Previously restrictions of memory
size, power and of particular compilers often forced restrictions upon style,
making programs clustered and difficult. All computers today are equipped
with more than enough memory for their purposes, and have very good
optimizers which can produce faster code than most programmers could
write themselves without help, so there are few good reasons not to make
programs as clear as possible.

The form of a C program

What goes into a C program? What will it look like?
C is made up entirely of building blocks which have a particular ‘shape’
or form. The form is the same everywhere in a program, whether it is the
form of the main program or of a subroutine. A program is made up of
functions, functions are made up of statements and declarations surrounded
by curly braces { }.
The basic building block in a C program is the function. Every C program
is a collection of one or more functions, written in some arbitrary order. One
and only one of these functions in the program must have the name main().
This function is always the starting point of a C program, so the simplest C
program would be just a single function definition:
main ()
{
}
The parentheses ‘()’ which follow the name of the function must be included
even though they apparently serve no purpose at this stage. This is how C
distinguishes functions from ordinary variables.
The function main() does not have to be at the top of a program so a C
program does not necessarily start at line 1. It always starts where main()

is. Also, the function main() cannot be called from any other function in
the program. Only the operating system can call the function main(): this
is how a C program is started.
The next most simple C program is perhaps a program which calls a
function do_nothing and then ends.
/******************************************************/
/**/
/* Program : do nothing*/
/**/
/******************************************************/
main()
{
do_nothing();
}
/******************************************************/
do_nothing()
{
}
/* Function called */
/* Main program */
The program now consists of two functions, one of which is called by the
other. There are several new things to notice about this program. Firstly
the function do_nothing() is called by typing its name followed by the
characteristic ‘()’ brackets and a semi-colon. This is all that is required to
transfer control to the new function. In some languages, words like CALL
or PROC are used, or even a symbol like ‘&’. No such thing is needed in C.
The semi-colon is vital however. All instructions in C must end with a semi-
colon. This is a signal to inform the compiler that the end of a statement
has been reached and that anything which follows is meant to be a part of
another statement. This helps the compiler diagnose errors.
The ‘brace’ characters ‘{’ and ‘}’ mark out a block into which instructions
are written. When the program meets the closing brace ‘}’ it then transfers
back to main() where it meets another ‘}’ brace and the program ends.
This is the simplest way in which control flows between functions in C.
All functions have the same status as far as a program is concerned. The
function main() is treated just as any other function. When a program is
compiled, each function is compiled as a separate entity and then at the end
the linker phase in the compiler attempts to sew them all together.
The examples above are obviously very simple but they illustrate how
control flows in a C program. Here are some more basic elements which we
shall cover.
• comments

The form of a C program





preprocessor commands
functions
declarations
variables
statements

The skeleton plan of a program, shown below, helps to show how the
elements of a C program relate. The following chapters will then expand
upon this as a kind of basic plan.
/****************************************************/
/**/
/* Skeleton program plan*/
/**/
/****************************************************/
#include <stdio.h>
#include <myfile.c>
#define SCREAM
#define NUMBER_OF_BONES
/* Preprocessor defns */
“arghhhhh”
123
/****************************************************/
main ()
{ int a,b;
a=random();
b=function1();
function2(a,b);
}
/****************************************************/
function1 ()
{
….
}
/****************************************************/
function2 (a,b)
int a,b;
{
….
}
/* Purpose */
/* Purpose */
/* Main program & start */
/* declaration */

Neither comments nor preprocessor commands have a special place in this
list: they do not have to be in any one particular place within the program.

Questions
What is a block?
Name the six basic things which make up a C program.
Does a C program start at the beginning? (Where is the beginning?)
What happens when a program comes to a } character? What does this
character signify?
What vital piece of punctuation goes at the end of every simple C
statement?

Annotating programs.

Comments are a way of inserting remarks and reminders into a program
without affecting its content. Comments do not have a fixed place in a
program: the compiler treats them as though they were white space or blank
characters and they are consequently ignored. Programs can contain any
number of comments without losing speed. This is because comments are
stripped out of a source program by the compiler when it converts the source
program into machine code.
Comments are marked out or delimited by the following pairs of charac-
ters:
/* …… comment ……*/
Because a comment is skipped over as though it were a single space, it can
be placed anywhere where spaces are valid characters, even in the middle of a
statement, though this is not to be encouraged. You should try to minimize
the use of comments in a program while trying to maximize the readability
of the program. If there are too many comments you obscure your code and
it is the code which is the main message in a program.

main ()
{
/* This little line
/* This little line
/* This little line
to the next line
/* And so on … */
}
has no effect */
has none */
went all the way down
*/
/* The almost trivial program */

#include <stdio.h>
#define
NOTFINISHED
/* header file */
0
/**********************************************/

Comments:-

/* A bar like the one above can be used to */
/* separate functions visibly in a program */
main ()
{ int i;
do
{
/* Nothing !!! */
}
while (NOTFINISHED);
}
/* declarations */
7.3 Question
1. What happens if a comment is not ended? That is if the programmer
types ‘/*’ .. to start but forgets the ..‘*/’ to close?

Functions:-

Making black boxes. Solving problems. Getting results.
A function is a module or block of program code which deals with a
particular task. Making functions is a way of isolating one block of code from
other independent blocks of code. Functions serve two purposes. They allow
a programmer to say: ‘this piece of code does a specific job which stands by
itself and should not be mixed up with anyting else’, and they make a block
of code reusable since a function can be reused in many different contexts
without repeating parts of the program text.
Functions help us to organize a program in a simple way; in Kernighan
& Ritchie C they are always written in the following form:
identifier (parameter1,parameter2,..)
types of parameters
{ variable declarations
statements..
……
….
}
For example
Pythagoras(x,y,z)
double x,y,z;
{ double d;
d = sqrt(x*x+y*y+z*z);
printf(“The distance to your point was %f\n”,d);
}
In the newer ANSI standard, the same function is written slightly differently:
Pythagoras(double x, double y, double z)
{ double d;
d = sqrt(x*x+y*y+z*z);
printf(“The distance to your point was %f\n”,d);
}
You will probably see both styles in C programs.

30
Chapter 8: Functions
Each function has a name or identifier by which is used to refer to it
in a program. A function can accept a number of parameters or values
which pass information from outside, and consists of a number of statements
and declarations, enclosed by curly braces { }, which make up the doing
part of the object. The declarations and ‘type of parameter’ statements are
formalities which will be described in good time.
The name of a function in C can be anything from a single letter to a
long word. The name of a function must begin with an alphabetic letter
or the underscore ‘_’ character but the other characters in the name can be
chosen from the following groups:
a .. z
A .. Z
0 .. 9
_
(any letter from a to z)
(any letter from A to Z)
(any digit from 0 to 9)
(the underscore character)
This means that sensible names can easily be chosen for functions making a
program easy to read. Here is a real example function which adds together
two integer numbers a and b and prints the result c. All the variables are
chosen to be integers to keep things simple and the result is printed out
using the print-formatted function printf, from the the standard library,
with a “%d” to indicate that it is printing a integer.
Add_Two_Numbers (a,b)
int a,b;
{ int c;
c = a + b;
printf (“%d”,c);
}
/* Add a and b */
Notice the position of the function name and where braces and semi-colons
are placed: they are crucial. The details are quickly learned with practice
and experience.
This function is not much use standing alone. It has to be called from
somewhere. A function is called (i.e. control is passed to the function) by
using its name with the usual brackets () to follow it, along with the values
which are to be passed to the function:
main ()
{ int c,d;
c = 1;

Program Listing
d = 53;
Add_Two_Numbers (c,d);
Add_Two_Numbers (1,2);
}
31
The result of this program would be to print out the number 54 and then
the number 3 and then stop. Here is a simple program which makes use
of some functions in a playful way. The structure diagram shows how this
can be visualized and the significance of the program ‘levels’. The idea is to
illustrate the way in which the functions connect together:
8.1 Structure diagram
Level 0:
main ()
|
Level 1:
DownOne ()
/
/
Level 2:
DownLeft()
\
\
DownRight()
Note: not all functions fit into a tidy hierarchy like these. Some functions
call themselves, while others can be called from anywhere in a program.
Where would you place the printf function in this hierarchy?
8.2 Program Listing
/***********************************************/
/**/
/* Function Snakes & Ladders*/
/**/
/***********************************************/
#include <stdio.h>
/***********************************************/
/* Level 0*/
/***********************************************/
main ()
{
printf (“This is level 0: the main program\n”);

32
printf (“About to go down a level
DownOne ();
printf (“Back at the end of the start!!\n”);
}
/************************************************/
/* Level 1*/
/************************************************/
DownOne ()
/* Branch out! */
\n”);
Chapter 8: Functions
{
printf (“Down here at level 1, all is well\n”);
DownLeft (2);
printf (“Through level 1….\n”);
DownRight (2);
printf (“Going back up a level!\n);
}
/************************************************/
/* Level 2*/
/************************************************/
DownLeft (a)
int a;
{
printf (“This is deepest level %d\n”,a);
printf (“On the left branch of the picture\n”);
printf (“Going up!!”);
}
/************************************************/
DownRight (a)
int a;
{
printf (“And level %d again!\n”,a);
}
/* Right branch */
/* Left branch */
8.3 Functions with values
In other languages and in mathematics a function is understood to be some-
thing which produces a value or a number. That is, the whole function is

Functions with values
33
thought of as having a value. In C it is possible to choose whether or not
a function will have a value. It is possible to make a function hand back a
value to the place at which it was called. Take the following example:
bill = CalculateBill(data…);
The variable bill is assigned to a function CalculateBill() and data are
some data which are passed to the function. This statement makes it look
as though CalculateBill() is a number. When this statement is executed
in a program, control will be passed to the function CalculateBill() and,
when it is done, this function will then hand control back. The value of the
function is assigned to “bill” and the program continues. Functions which
work in this way are said to return a value.
In C, returning a value is a simple matter. Consider the function Calcu-
lateBill() from the statement above:
CalculateBill(starter,main,dessert)
int starter,main,dessert;
{ int total;
total = starter + main + dessert;
return (total);
}
/* Adds up values */
As soon as the return statement is met CalculateBill() stops executing
and assigns the value total to the function. If there were no return state-
ment the program could not know which value it should associate with the
name CalculateBill and so it would not be meaningful to speak of the
function as having one value. Forgetting a return statement can ruin a
program. For instance if CalculateBill had just been:
CalculateBill (starter,main,dessert)
int starter,main,dessert;
{ int total;
total = starter + main + dessert;
}
/* WRONG! */
then the value bill would just be garbage (no predictable value), presuming
that the compiler allowed this to be written at all. On the other hand if the
first version were used (the one which did use the return(total) statement)
and furthermore no assignment were made:
main ()

34
Chapter 8: Functions
{
CalculateBill (1,2,3);
}
then the value of the function would just be discarded, quite legitimately.
This is usually what is done with the input output functions printf() and
scanf() which actually return values. So a function in C can return a value
but it does not have to be used; on the other hand, a value which has not
been returned cannot be used safely.
NOTE : Functions do not have to return integers: you can decide whether
they should return a different data type, or even no value at all. (See next
chapter)
8.4 Breaking out early
Suppose that a program is in the middle of some awkward process in a
function which is not main(), perhaps two or three loops working together,
for example, and suddenly the function finds its answer. This is where the
beauty of the return statement becomes clear. The program can simply call
return(value) anywhere in the function and control will jump out of any
number of loops or whatever and pass the value back to the calling statement
without having to finish the function up to the closing brace }.
myfunction (a,b)
int a,b;
{
while (a < b)
{
if (a > b)
{
return (b);
}
a = a + 1;
}
}
/* breaking out of functions early */
The example shows this. The function is entered with some values for a and
b and, assuming that a is less than b, it starts to execute one of C’s loops
called while. In that loop, is a single if statement and a statement which
increases a by one on each loop. If a becomes bigger than b at any point
the return(b) statement gets executed and the function myfunction quits,
without having to arrive at the end brace ‘}’, and passes the value of b back
to the place it was called.

Questions
35
8.5 The exit() function
The function called exit() can be used to terminate a program at any point,
no matter how many levels of function calls have been made. This is called
with a return code, like this:
#define CODE
exit (CODE);
0
This function also calls a number of other functions which perform tidy-up
duties such as closing open files etc.
8.6 Functions and Types
All the variables and values used up to now have been integers. But what
happens if a function is required to return a different kind of value such as
a character? A statement like:
bill = CalculateBill (a,b,c);
can only make sense if the variable bill and the value of the func-
tion CalculateBill() are the same kind of object: in other words if
CalculatBill() returns a floating point number, then bill cannot be a
character! Both sides of an assignment must match.
In fact this is done by declaring functions to return a particular type of
data. So far no declarations have been needed because C assumes that all
values are integers unless you specifically choose something different. Dec-
larations are covered in the next section.
8.7 Questions
1. Write a function which takes two values a and b and returns the value
of (a*b).
2. Is there anything wrong with a function which returns no value?
3. What happens if a function returns a value but it is not assigned to
anything?
4. What happens if a function is assigned to an object but that function
returns no value?
5. How can a function be made to quit early?

36
Chapter 8: Functions

Variables, Types and Declarations
37
9 Variables, Types and Declarations
Storing data. Descriminating types. Declaring data.
A variable is a seqeuence of program code with a name (also called its
identifier ). A name or identifier in C can be anything from a single letter to
a word. The name of a variable must begin with an alphabetic letter or the
underscore ‘_’ character but the other characters in the name can be chosen
from the following groups:
a .. z
A .. Z
0 .. 9
_
(any letter from a to z)
(any letter from A to Z)
(any digit from 0 to 9)
(the underscore character)
Some examples of valid variable names are:
a
total
Out_of_Memory
VAR
integer
etc…
In C variables do not only have names: they also have types. The type
of a variable conveys to the the compiler what sort of data will be stored
in it. In BASIC and in some older, largely obsolete languages, like PL/1, a
special naming convention is used to determine the sort of data which can
be held in particular variables. e.g. the dollar symbol ‘$’ is commonly used
in BASIC to mean that a variable is a string and the percentage ‘%’ symbol
is used to indicate an integer. No such convention exists in C. Instead we
specify the types of variables in their declarations. This serves two purposes:
• It gives a compiler precise information about the amount of memory
that will have to be given over to a variable when a program is finally
run and what sort of arithmetic will have to be used on it (e.g. integer
only or floating point or none).
• It provides the compiler with a list of the variables in a convenient place
so that it can cross check names and types for any errors.
There is a lot of different possible types in C. In fact it is possible for
us to define our own, but there is no need to do this right away: there are
some basic types which are provided by C ready for use. The names of these
types are all reserved words in C and they are summarized as follows:
char
short
int
A single ASCII character
A short integer (usually 16-bits)
A standard integer (usually 32-bits)
short int A short integer

38
long
long int
float
long float
A long integer
Chapter 9: Variables, Types and Declarations
A long integer (usually 32-bits, but increasingly 64 bits)
A floating point or real number (short)
a long floating point number
double
void
enum
A long floating point number
Discussed in a later chapter.
Discussed in a later chapter.
volatile Discussed in a later chapter.
There is some repetition in these words. In addition to the above, the word
unsigned can also be placed in front of any of these types. Unsigned means
that only positive or zero values can be used. (i.e. there is no minus sign).
The advantage of using this kind of variable is that storing a minus sign
takes up some memory, so that if no minus sign is present, larger numbers
can be stored in the same kind of variable. The ANSI standard also allows
the word signed to be placed in front of any of these types, so indicate
the opposite of unsigned. On some systems variables are signed by default,
whereas on others they are not.
9.1 Declarations
To declare a variable in a C program one writes the type followed by a list
of variable names which are to be treated as being that type:
typename variablename1,..,..,variablenameN ;
For example:
int i,j;
char ch;
double x,y,z,fred;
unsigned long int Name_of_Variable;
Failing to declare a variable is more risky than passing through customs
and failing to declare your six tonnes of Swiss chocolate. A compiler is
markedly more efficient than a customs officer: it will catch a missing decla-
ration every time and will terminate a compiling session whilst complaining
bitterly, often with a host of messages, one for each use of the undeclared
variable.
9.2 Where to declare things
There are two kinds of place in which declarations can be made, See Chap-
ter 11 [Scope], page 65. For now it will do to simply state what these places
are.

Declarations and Initialization
39
1. One place is outside all of the functions. That is, in the space between
function definitions. (After the #include lines, for example.) Variables
declared here are called global variables. There are also called static
and external variables in special cases.)
#include <stdio.h>
int globalinteger;
float global_floating_point;
main ()
{
}
/* Here! outside {} */
2. The other place where declarations can be made is following the opening
brace, {}, of a block. Any block will do, as long as the declaration follows
immediately after the opening brace. Variables of this kind only work
inside their braces {} and are often called local variables. Another name
for them is automatic variables.
main ()
{ int a;
float x,y,z;
/* statements */
}
or
function ()
{ int i;
/* …. */
while (i < 10)
{ char ch;
int g;
/* … */
}
}

40
Chapter 9: Variables, Types and Declarations
9.3 Declarations and Initialization
When a variable is declared in C, the language allows a neat piece of syntax
which means that variables can be declared and assigned a value in one go.
This is no more efficient than doing it in two stages, but it is sometimes
tidier. The following:
int i = 0;
char ch = ’a’;
are equivalent to the more longwinded
int i;
char ch;
i = 0;
ch = ’a’;
This is called initialization of the variables. C always allows the programmer
to write declarations/initializers in this way, but it is not always desirable to
do so. If there are just one or two declarations then this initialization method
can make a program neat and tidy. If there are many, then it is better to
initialize separately, as in the second case. A lot means when it starts to
look as though there are too many. It makes no odds to the compiler, nor
(ideally) to the final code whether the first or second method is used. It is
only for tidiness that this is allowed.
9.4 Individual Types
9.4.1 char
A character type is a variable which can store a single ASCII character.
Groups of char form strings. In C single characters are written enclosed by
single quotes, e.g. ’c’! (This is in contrast to strings of many characters
which use double quotes, e.g. “string”) For instance, if ch is the name of
a character:
char ch;
ch = ’a’;
would give ch the value of the character a. The same effect can also be
achieved by writing:
char ch = ’a’;

Listing
41
A character can be any ASCII character, printable or not printable from
values -128 to 127. (But only 0 to 127 are used.) Control characters i.e.
non printable characters are put into programs by using a backslash \ and
a special character or number. The characters and their meanings are:
‘\b’
‘\f’
‘\n’
‘\r’
‘\t’
‘\v’
‘\”’
‘\’’
‘\\’
‘\ddd ’
backspace BS
form feed FF (also clear screen)
new line NL (like pressing return)
carriage return CR (cursor to start of line)
horizontal tab HT
vertical tab (not all versions)
double quotes (not all versions)
single quote character ’
backslash character \
character ddd where ddd is an ASCII code given in octal or
base 8, See undefined [Character Conversion Table], page un-
defined .
character ddd where ddd is an ASCII code given in hexadeci-
mal or base 16, See undefined [Character Conversion Table],
page undefined .
‘\xddd ’
9.4.2 Listing
/***************************************************/
/**/
/* Special Characters*/
/**/
/***************************************************/
#include <stdio.h>
main ()
{
printf (“Beep! \7 \n”);
printf (“ch = \’a\’ \n”);
printf (” <- Start of this line!! \r”);
}
The output of this program is:
Beep! (and the BELL sound )
ch = ’a’
<- Start of this line!!

42
Chapter 9: Variables, Types and Declarations
and the text cursor is left where the arrow points. It is also possible to have
the type:
unsigned char
This admits ASCII values from 0 to 255, rather than -128 to 127.
9.4.3 Integers
9.5 Whole numbers
There are five integer types in C and they are called char, int, long, long
long and short. The difference between these is the size of the integer
which either can hold and the amount of storage required for them. The
sizes of these objects depend on the operating system of the computer. Even
different flavours of Unix can have varying sizes for these objects. Usually,
the two to remember are int and short. int means a ‘normal’ integer and
short means a ‘short’ one, not that that tells us much. On a typical 32 bit
microcomputer the size of these integers is the following:
Type
Bits
Possible Values
-32768 to 32767
0 to 65535
-2147483648 to 2147483647
(ditto)
0 to 4294967295
-9e18 to + 8e18
short16
unsigned short 16
int
long
unsigned int
long long
32
32
32
64
Increasingly though, 64 bit operating systems are appearing and long inte-
gers are 64 bits long. You should always check these values. Some mainframe
operating systems are completely 64 bit, e.g. Unicos has no 32 bit values.
Variables are declared in the usual way:
int i,j;
i = j = 0;
or
short i=0,j=0;
9.5.1 Floating Point
There are also long and short floating point numbers in C. All the mathe-
matical functions which C can use require double or long float arguments

Assigning variables to one another
43
so it is common to use the type float for storage only of small floating point
numbers and to use double elsewhere. (This not always true since the C
‘cast’ operator allows temporary conversions to be made.) On a typical 32
bit implementation the different types would be organized as follows:
Type
float
double
long float
long double
Bits
32
64
32
???
Possible Values
+/- 10E-37 to +/- 10E38
+/- 10E-307 to +/- 10E308
(ditto)
Typical declarations:
float x,y,z;
x = 0.1;
y = 2.456E5
z = 0;
double bignum,smallnum;
bignum = 2.36E208;
smallnum = 3.2E-300;
9.6 Choosing Variables
The sort of procedure that you would adopt when choosing variable names
is something like the following:
• Decide what a variable is for and what type it needs to be.
• Choose a sensible name for the variable.
• Decide where the variable is allowed to exist.
• Declare that name to be a variable of the chosen type.
Some local variables are only used temporarily, for controlling loops for
instance. It is common to give these short names (single characters). A good
habit to adopt is to keep to a consistent practice when using these variables.
A common one, for instance is to use the letters:
int i,j,k;
to be integer type variables used for counting. (There is not particular reason
why this should be; it is just common practice.) Other integer values should
have more meaningful names. Similarly names like:
double x,y,z;
tend to make one think of floating point numbers.

44
Chapter 9: Variables, Types and Declarations
9.7 Assigning variables to one another
Variables can be assigned to numbers:
var = 10;
and assigned to each other:
var1 = var2;
In either case the objects on either side of the = symbol must be of the same
type. It is possible (though not usually sensible) to assign a floating point
number to a character for instance. So
int a, b = 1;
a = b;
is a valid statement, and:
float x = 1.4;
char ch;
ch = x;
is a valid statement, since the truncated value 1 can be assigned to ch. This
is a questionable practice though. It is unclear why anyone would choose to
do this. Numerical values and characters will interconvert because characters
are stored by their ASCII codes (which are integers!) Thus the following will
work:
int i;
char ch = ’A’;
i = ch;
printf (“The ASCII code of %c is %d”,ch,i);
The result of this would be:
The ASCII code of A is 65
9.8 Types and The Cast Operator
It is worth mentioning briefly a very valuable operator in C: it is called the
cast operator and its function is to convert one type of value into another.
For instance it would convert a character into an integer:

Types and The Cast Operator
45
int i;
char ch = ’\n’;
i = (int) ch;
The value of the integer would be the ASCII code of the character. This
is the only integer which it would make any sense to talk about in connec-
tion with the character. Similarly floating point and integer types can be
interconverted:
float x = 3.3;
int i;
i = (int) x;

46
Chapter 9: Variables, Types and Declarations
The value of i would be 3 because an integer cannot represent decimal points,
so the cast operator rounds the number. There is no such problem the other
way around.
float x;
int i = 12;
x = (float) i;
The general form of the cast operator is therefore:
(type ) variable
It does not always make sense to convert types. This will be seen particularly
with regard to structures and unions. Cast operators crop up in many areas
of C. This is not the last time they will have to be explained.

Storage class static and extern
47
/***************************************************/
/**/
/* Demo of Cast operator*/
/**/
/***************************************************/
#include <stdio.h>
main ()
{ float x;
int i;
char ch;
x = 2.345;
i = (int) x;
ch = (char) x;
printf (“From float x =%f i =%d ch =%c\n”,x,i,ch);
i = 45;
x = (float) i;
ch = (char) i;
printf (“From int i=%d x=%f ch=%c\n”,i,x,ch);
ch = ’*’;
i = (int) ch;
x = (float) ch;
printf (“From char ch=%c i=%d x=%f\n”,ch,i,x);
}
/* Use int float and char */
9.9 Storage class static and extern
Sometimes C programs are written in more than one text file. If this is the
case then, on occasion, it will be necessary to get at variables which were
defined in another file. If the word extern is placed in front of a variable
then it can be referenced across files:
File 1
File 2
int i;
main ()
{
extern int i;
}
{
}
function ()

48
Chapter 9: Variables, Types and Declarations
In this example, the function main() in file 1 can use the variable i from
the function main in file 2.
Another class is called static. The name static is given to variables
which can hold their values between calls of a function: they are allocated
once and once only and their values are preserved between any number of
function calls. Space is allocated for static variables in the program code
itself and it is never disposed of unless the whole program is. NOTE: Every
global variable, defined outside functions has the type static automatically.
The opposite of static is auto.
9.10 Functions, Types and Declarations
Functions do not always have to return values which are integers despite the
fact that this has been exclusively the case up to now. Unless something
special is done to force a function to return a different kind of value C will
always assume that the type of a function is int.
If you want this to be different, then a function has to be declared to be
a certain type, just as variables have to be. There are two places where this
must be done:
• The name of the function must be declared a certain type where the
function is declared. e.g. a function which returns a float value must be
declared as:
float function1 ()
{
return (1.229);
}
A function which returns a character:
char function2 ()
{
return (’*’);
}
• As well as declaring a function’s identifier to be a certain type in the
function definition, it must (irritatingly) be declared in the function in
which it is called too! The reasons for this are related to the way in
which C is compiled. So, if the two functions above were called from
main(), they would have to declared in the variables section as:
main ()
{ char ch, function2 ();
float x, function1 ();

Questions
x = function1 ();
ch = function2 ();
}
49
If a function whose type is not integer is not declared like this, then
compilation errors will result! Notice also that the function must be
declared inside every function which calls it, not just main().
9.11 Questions
1. What is an identifier?
2. Say which of the following are valid C identifiers:
1. Ralph23
2. 80shillings
3. mission_control
4. A%
5. A$
6. _off
3. Write a statement to declare two integers called i and j.
4. What is the difference between the types floa and double.
5. What is the difference between the types int and unsigned int?
6. Write a statement which assigns the value 67 to the integer variable “I”.
7. What type does a C function return by default?
8. If we want to declare a function to return long float, it must be done
in, at least, two places. Where are these?
9. Write a statement, using the cast operator, to print out the integer part
of the number 23.1256.
10. Is it possible to have an automatic global variable?

50
Chapter 9: Variables, Types and Declarations

Declaring Parameters
51
10 Parameters and Functions
Ways in and out of functions.
Not all functions will be as simple as the ones which have been given so
far. Functions are most useful if they can be given information to work with
and if they can reach variables and data which are defined outside of them.
Examples of this have already been seen in a limited way. For instance the
function CalculateBill accepted three values a,b and c.
CalculateBill (a,b,c)
int a,b,c;
{ int total;
total = a + b + c;
return total;
}
When variable values are handed to a function, by writing them inside a
functions brackets like this, the function is said to accept parameters. In
mathematics a parameter is a variable which controls the behaviour of some-
thing. In C it is a variable which carries some special information. In
CalculateBill the “behaviour” is the addition process. In other words,
the value of total depends upon the starting values of a,b and c.
Parameters are about communication between different functions in a
program. They are like messengers which pass information to and from
different places. They provide a way of getting information into a function,
but they can also be used to hand information back. Parameters are usually
split into two categories: value parameters and variable parameters. Value
parameters are one-way communication carrying information into a function
from somewhere outside. Variable parameters are two-way.
10.1 Declaring Parameters
A function was defined by code which looks like this:
identifier (parameters…)
types of parameters
{
}

52
Chapter 10: Parameters and Functions
Parameters, like variables and functions, also have types which must be
declared. For instance:
function1 (i,j,x,y)
int i,j;
float x,y;
{
}
or
char function2 (x,ch)
double x;
char ch;
{ char ch2 = ’*’;
return (ch2);
}
Notice that they are declared outside the block braces.
10.2 Value Parameters
A value parameter is the most common kind of parameter. All of the ex-
amples up to know have been examples of value parameters. When a value
parameter is passes information to a function its value is copied to a new
place which is completely isolated from the place that the information came
from. An example helps to show this. Consider a function which is called
from main() whose purpose is to add together two numbers and to print out
the result.
#include <stdio.h>
main ()
{
add (1,4);
}
/*******************************************/
add (a,b)
int a,b;

Value Parameters
{
printf (“%d”, a+b);
}
53
When this program is run, two new variables are automatically created by
the language, called a and b. The value 1 is copied into a and the value 4
is copied into b. Obviously if a and b were given new values in the function
add() then this could not change the values 1 and 4 in main(), because 1

54
Chapter 10: Parameters and Functions
is always 1 and 4 is always 4. They are constants. However if instead the
program had been:

Value Parameters
55
main ()
{ int a = 1, b = 4;
add (a,b);
}
/**************************************/
add (a,b)
int a,b;
{
printf (“%d”, a+b);
}
then it is less clear what will happen. In fact exactly the same thing happens:
• When add() is called from main() two new variables a and b are created
by the language (which have nothing to do with the variables a and b
in main() and are completely isolated from them).
• The value of a in main() is copied into the value of a in add().
• The value of b in main() is copied into the value of b in add().
Now, any reference to a and b within the function add() refers only to the
two parameters of add and not to the variables with the same names which
appeared in main(). This means that if a and b are altered in add() they
will not affect a and b in main(). More advanced computing texts have
names for the old and they new a and b:
Actual Parameters
These are the original values which were handed over to a func-
tion. Another name for this is an argument.
Formal Parameters
These are the copies which work inside the function which was
called.
Here are some points about value parameters.
• The names of formal parameters can be anything at all. They do not
have to be the same as the actual parameters. So in the example above
it would be equally valid to write:
#include <stdio.h>
main ()
{ int a = 1, b = 4;

56
Chapter 10: Parameters and Functions
add (a,b);
}
/*******************************************/
add (i,j)
int i,j;
{
printf (“%d”, i+j);
}
In this case the value of a in main() would be copied to the value of i
in add() and the value of b in main() would be copied to the value of
j in add().
• The parameters ought to match by datatype when taken in an ordered
sequence. It is possible to copy a floating point number into a character
formal parameter, causing yourself problems which are hard to diagnose.
Some compilers will spot this if it is done accidentally and will flag it
as an error. e.g.
main ()
{
function (’*’,1.0);
}
/********************************/
function (ch,i)
char ch;
int i;
{
}
is probably wrong because 1.0 is a floating point value, not an integer.
• The parameters ought to, but need not match in number! This sur-
prising fact is important because programs can go wrong if a formal
parameter was missed out. ANSI C has a way of checking this by func-
tion ‘prototyping’, but in Kernighan & Ritchie C there is no way to
check this. If the number of actual parameters is more than the number
of formal parameters and all of the parameters match in type then the
extra values are just discarded. If the number of actual parameters is
less than the number of formal parameters, then the compiler will assign

Example Listing
57
some unknown value to the formal parameters. This will probably be
garbage.
• Our use of variables as parameters should not leave you with the impres-
sion that we can only use variables as parameters. In fact, we can send
any literal value, or expression with an appropriate type to a function.
For example,
sin(3.41415);
cos(a+b*2.0);
strlen(“The length of this string”);
10.3 Functions as actual parameters
The value returned by a function can be used directly as a value parameter.
It does not have to be assigned to a variable first. For instance:
main ()
{
PrintOut (SomeValue());
}
/*********************************************/
PrintOut (a)
int a;
{
printf (“%d”,a);
}
/**********************************************/
SomeValue ()
{
return (42);
}
/* Return an arbitrary no */
/* Print the value */
This often gives a concise way of passing a value to a function.
10.4 Example Listing
/**************************************************/
/**/
/* Value Parameters*/
/**/
/**************************************************/

58
Chapter 10: Parameters and Functions
/* Toying with value parameters */
#include <stdio.h>
/**************************************************/
/* Level 0*/
/**************************************************/
main ()
/* Example of value parameters */
{ int i,j;
double x,x_plus_one();
char ch;
i = 0;
x = 0;
printf (” %f”, x_plus_one(x));
printf (” %f”, x);
j = resultof (i);
printf (” %d”,j);
}
/***************************************************/
/* level 1*/
/***************************************************/
double x_plus_one(x)
double x;
{
x = x + 1;
return (x);
}
/****************************************************/
resultof (j)
int j;
{
return (2*j + 3);
}
/* Work out some result */
/* Add one to x ! */
/* why not… */
10.5 Example Listing

Example Listing
/******************************************************/
/**/
/* Program : More Value Parameters*/
/**/
/******************************************************/
/* Print out mock exam results etc */
#include <stdio.h>
/******************************************************/
main ()
/* Print out exam results */
59
{ int pupil1,pupil2,pupil3;
int ppr1,ppr2,ppr3;
float pen1,pen2,pen3;
pupil1 = 87;
pupil2 = 45;
pupil3 = 12;
ppr1 = 200;
ppr2 = 230;
ppr3 = 10;
pen1 = 1;
pen2 = 2;
pen3 = 20;
analyse (pupil1,pupil2,pupil3,ppr1,ppr2,
ppr3,pen1,pen2,pen3);
}
/*******************************************************/
analyse (p1,p2,p3,w1,w2,w3,b1,b2,b3)
int p1,p2,p3,w1,w2,w3;
float b1,b2,b3;
{
printf (“Pupil 1 scored %d percent\n”,p1);
printf (“Pupil 2 scored %d percent\n”,p2);
printf (“Pupil 3 scored %d percent\n”,p3);
printf (“However: \n”);
printf (“Pupil1 wrote %d sides of paper\n”,w1);
printf (“Pupil2 wrote %d sides\n”,w2);
printf (“Pupil3 wrote %d sides\n”,w3);

60
Chapter 10: Parameters and Functions
if (w2 > w1)
{
printf (“Which just shows that quantity”);
printf (” does not imply quality\n”);
}
printf (“Pupil1 used %f biros\n”,b1);
printf (“Pupil2 used %f \n”,b2);
printf (“Pupil3 used %f \n”,b3);
printf (“Total paper used = %d”, total(w1,w2,w3));
}
/*****************************************************/
total (a,b,c)
int a,b,c;
{
return (a + b + c);
}
/* add up total */
10.6 Variable Parameters
(As a first time reader you may wish to omit this section until you have read
about Pointers and Operators.)
One way to hand information back is to use the return statement. This
function is slightly limited however in that it can only hand the value of one
variable back at a time. There is another way of handing back values which is
less restrictive, but more awkward than this. This is by using a special kind

Variable Parameters
61
of parameter, often called a variable parameter. It is most easily explained
with the aid of an example:
#include <stdio.h>
main ()
{ int i,j;
GetValues (&i,&j);

62
printf (“i = %d and j = %d”,i,j)
}
Chapter 10: Parameters and Functions
/************************************/
GetValues (p,q)
int *p,*q;
{
*p = 10;
*q = 20;
}
To understand fully what is going on in this program requires a knowledge
of pointers and operators, which are covered in later sections, but a brief
explanation can be given here, so that the method can be used.
There are two new things to notice about this program: the symbols ‘&’
and ‘*’. The ampersand ‘&’ symbol should be read as “the address of..”.
The star ‘*’ symbol should be read as “the contents of the address…”. This
is easily confused with the multiplication symbol (which is identical). The
difference is only in the context in which the symbol is used. Fortunately
this is not ambiguous since multiplication always takes place between two
numbers or variables, whereas the “contents of a pointer” applies only to a
single variable and the star precedes the variable name.
So, in the program above, it is not the variables themselves which are
being passed to the procedure but the addresses of the the variables. In other
words, information about where the variables are stored in the memory is
passed to the function GetValues(). These addresses are copied into two
new variables p and q, which are said to be pointers to i and j. So, with
variable parameters, the function does not receive a copy of the variables
themselves, but information about how to get at the original variable which
was passed. This information can be used to alter the “actual parameters”
directly and this is done with the ‘*’ operator.
*p = 10;
means: Make the contents of the address held in p equal to 10. Recall that
the address held in p is the address of the variable i, so this actually reads:
make i equal to 10. Similarly:
*q = 20;
means make the contents of the address held in q equal to 20. Other oper-
ations are also possible (and these are detailed in the section on pointers)
such as finding out the value of i and putting it into a new variable, say, a:

Questions
int a;
a = *p;
/* is equivalent to a = i */
63
Notice that the * symbol is required in the declaration of these parameters.
10.7 Example Listing
/**************************************************/
/**/
/* Program : Variable Parameters*/
/**/
/**************************************************/
/* Scale some measurements on a drawing, say */
#include <stdio.h>
/**************************************************/
main ()
{ int height,width;
height = 4;
width = 5;
ScaleDimensions (&height,&width);
printf (“Scaled height = %d\n”,height);
printf (“Scaled width = %d\n”,width);
}
/****************************************************/
ScaleDimensions (h,w)
int *h, *w;
{ int hscale = 3;
int wscale = 1;
*h = *h * hscale;
*w = *w * wscale;
}
/* scale factors */
/* return scaled values */
/* Scale measurements*/
10.8 Questions
1. Name two ways that values and results can be handed back from a

64
Chapter 10: Parameters and Functions
function.
Where are parameters declared?
Can a function be used directly as a value parameter?
Does it mean anything to use a function directly as a variable parame-
ter?
What do the symbols * and & mean, when they are placed in front of
an identifier?
Do actual and formal parameters need to have the same names?
2.
3.
4.
5.
6.

Local Variables
65
11 Scope : Local And Global
Where a program’s fingers can’t reach.
From the computer’s point of view, a C program is nothing more than
a collection of functions and declarations. Functions can be thought of as
sealed capsules of program code which float on a background of white space,
and are connected together by means of function calls. White space is the
name given to the white of an imaginary piece of paper upon which a pro-
gram is written, in other words the spaces and new line characters which
are invisible to the eye. The global white space is only the gaps between
functions, not the gaps inside functions. Thinking of functions as sealed
capsules is a useful way of understanding the difference between local and
global objects and the whole idea of scope in a program.
Another analogy is to think of what goes on in a function as being like
watching a reality on television. You cannot go in and change the TV reality,
only observe the output, but the television show draws its information from
the world around it. You can send a parameter (e.g. switch channels) to
make some choices. A function called by a function, is like seeing someone
watching a televsion, in a television show.
11.1 Global Variables
Global variables are declared in the white space between functions. If every
function is a ship floating in this sea of white space, then global variables
(data storage areas which also float in this sea) can enter any ship and
also enter anything inside any ship (See the diagram). Global variables are
available everywhere;. they are created when a program is started and are
not destroyed until a program is stopped. They can be used anywhere in a
program: there is no restriction about where they can be used, in principle.
11.2 Local Variables
Local variables are more interesting. They can not enter just any region of
the program because they are trapped inside blocks. To use the ship analogy:
if it is imagined that on board every ship (which means inside every function)
there is a large swimming pool with many toy ships floating inside, then local
variables will work anywhere in the swimming pool (inside any of the toys
ships, but can not get out of the large ship into the wide beyond. The
swimming pool is just like a smaller sea, but one which is restricted to being
inside a particular function. Every function has its own swimming pool! The
idea can be taken further too. What about swimming pools onboard the toy
ships? (Meaning functions or blocks inside the functions!
/* Global white space “sea” */

66
Chapter 11: Scope : Local And Global
function ()
{
/* On board ship */
{
/* On board a toy ship */
}
}

Local Variables
67
The same rules apply for the toy ships. Variables can reach anywhere
inside them but they cannot get out. They cannot escape their block braces
{}. Whenever a pair of block braces is written into a program it is possible
to make variable declarations inside the opening brace. Like this:
{ int locali;

68
char localch;
/* statements */
}
Chapter 11: Scope : Local And Global
These variables do not exist outside the braces. They are only created when
the opening brace is encountered and they are destroyed when the closing
brace is executed, or when control jumps out of the block. Because they only
work in this local area of a program, they are called local variables. It is a
matter of style and efficiency to use local variables when it does not matter
whether variables are preserved outside of a particular block, because the
system automatically allocates and disposes of them. The programmer does
not have to think about this.
Where a variable is and is not defined is called the scope of that variable.
It tells a programmer what a variables horizons are!
11.3 Communication : parameters
If functions were sealed capsules and no local variables could ever commu-
nicate with other parts of the program, then functions would not be very
useful. This is why parameters are allowed. Parameters are a way of handing
local variables to other functions without letting them out! Value parame-
ters (see last section) make copies of local variables without actually using
them. The copied parameter is then a local variable in another function. In
other words, it can’t get out of the function to which is it passed … unless
it is passed on as another parameter.
11.4 Example Listing
Notice about the example that if there are two variables of the same name,
which are both allowed to be in the same place (c in the example below)
then the more local one wins. That is, the last variable to be defined takes
priority. (Technically adept readers will realize that this is because it was
the last one onto the variable stack.)
/***************************************************************/
/**/
/* SCOPE : THE CLLLED CAPSULES*/
/**/
/***************************************************************/
#include <stdio.h>
/***************************************************************/
main ()

Style Note
{ int a = 1, b = 2, c = 3;
if (a == 1)
{ int c;
c = a + b;
printf (“%d”,c);
}
handdown (a,b);
printf (“%d”,c);
}
/**************************************************************/
handdown (a,b)
int a,b;
{

}
/* Some function */
69
11.5 Style Note
Some programmers complain about the use of global variables in a program.
One complaint is that it is difficult to see what information is being passed
to a function unless all that information is passed as parameters. Sometimes
global variables are very useful however, and this problem need not be crip-
pling. A way to make this clear is to write global variables in capital letters
only, while writing the rest of the variables in mainly small letters..
int GLOBALINTEGER;
….
{ int local integer;
}
This allows global variables to be spotted easily. Another reason for restrict-
ing the use of global variables is that it is easier to debug a program if only
local variables are used. The reason is that once a function capsule is tested
and sealed it can be guaranteed to work in all cases, provided it is not af-
fected by any other functions from outside. Global variables punch holes in
the sealed function capsules because they allow bugs from other functions
to creep into tried and tested ones. An alert and careful programmer can
usually control this without difficulty.

70
Chapter 11: Scope : Local And Global
The following guidelines may help the reader to decide whether to use
local or global data:
• Always think of using a local variable first. Is it impractical? Yes, if it
means passing dozens of parameters to functions, or reproducing a lot
of variables. Global variables will sometimes tidy up a program.
• Local variables make the flow of data in a program clearer and they
reduce the amount of memory used by the program when they are not
in use.
• The preference in this book is to use local variables for all work, ex-
cept where a program centres around a single data structure. If a data
structure is the main reason for a program’s existence, it is nearly always
defined globally.
11.6 Scope and Style
All the programs in this book, which are longer than a couple of lines, are
written in an unusual way: with a levelled structure There are several good
reasons for this. One is that the sealed capsules are shown to be sealed, by
using a comment bar between each function.
/**************************************/
Another good reason is that any function hands parameters down by only
one level at a time and that any return() statement hands values up a
single level. The global variables are kept to a single place at the head of
each program so that they can be seen to reach into everything.
The diagram shows how the splitting of levels implies something about
the scope of variables and the handing of parameters.
11.7 Questions
1. What is a global variable?
2. What is a local variable?
3. What is meant by calling a block (enclosed by braces {} ) a “sealed
capsule”?
4. Do parameters make functions leaky? i.e. Do they spoil them by letting
the variables leak out into other functions?
5. Write a program which declares 4 variables. Two integer variables called
number_of_hats,counter which are GLOBAL and two float variables
called x_coord,y_coord which are LOCAL inside the function main().
Then add another function called another() and pass x_coord,y_coord
to this function. How many different storage spaces are used when this
program runs? (Hint: are x_coord,y_coord and their copies the same?)

Preprocessor Commands
71
12 Preprocessor Commands
Making programming versatile.
C is unusual in that it has a pre-processor. This comes from its Unix
origins. As its name might suggest, the preprocessor is a phase which occurs
prior to compilation of a program. The preprocessor has two main uses: it
allows external files, such as header files, to be included and it allows macros
to be defined. This useful feature traditionally allowed constant values to be
defined in Kernighan and Ritchie C, which had no constants in the language.
Pre-processor commands are distinguished by the hash (number) symbol
‘#’. One example of this has already been encountered for the standard
header file ‘stdio.h’.
#include <stdio.h>
is a command which tells the preprocessor to treat the file ‘stdio.h’ as if it
were the actually part of the program text, in other words to include it as
part of the program to be compiled.
Macros are words which can be defined to stand in place of something
complicated: they are a way of reducing the amount of typing in a program
and a way of making long ungainly pieces of code into short words. For
example, the simplest use of macros is to give constant values meaningful
names: e.g.
#define TELEPHNUM 720663
This allows us to use the word TELEPHNUM in the program to mean the
number 720663. In this particular case, the word is clearly not any shorter
than the number it will replace, but it is more meaningful and would make
a program read more naturally than if the raw number were used. For
instance, a program which deals with several different fixed numbers like a
telephone number, a postcode and a street number could write:
printf(“%d %d %d”,TELEPHNUM,postcode,streetnum);
instead of
printf(“%d %d %d”,720663,345,14);
Using the macros instead makes the actions much clearer and allows the
programmer to forget about what the numbers actually are. It also means
that a program is easy to alter because to change a telephone number, or
whatever, it is only necessary to change the definition, not to retype the
number in every single instance.

72
Chapter 12: Preprocessor Commands
The important feature of macros is that they are not merely numerical
constants which are referenced at compile time, but are strings which are
physically replaced before compilation by the preprocessor! This means that
almost anything can be defined:
#define SUM
1 + 2 + 3 + 4
would allow SUM to be used instead of 1+2+3+4. Or
#define STRING “Mary had a little lamb…”
would allow a commonly used string to be called by the identifier “string”
instead of typing it out afresh each time. The idea of a define statement
then is:
#define macroname
definition on rest of line
Macros cannot define more than a single line to be substituted into a
program but they can be used anywhere, except inside strings. (Anything
enclosed in string quotes is assumed to be complete and untouchable by the
compiler.) Some macros are defined already in the file ‘stdio.h’ such as:
EOF
NULL
The end of file character (= -1 for instance)
The null character (zero) = 0
12.1 Macro Functions
A more advanced use of macros is also permitted by the preprocessor. This
involves macros which accept parameters and hand back values. This works
by defining a macro with some dummy parameter, say x. For example: a
macro which is usually defined in one of the standard libraries is abs() which
means the absolute or unsigned value of a number. It is defined below:
#define ABS(x) ((x) < 0) ? -(x) : (x)
The result of this is to give the positive (or unsigned) part of any number
or variable. This would be no problem for a function which could accept
parameters, and it is, in fact, no problem for macros. Macros can also be
made to take parameters. Consider the ABS() example. If a programmer
were to write ABS(4) then the preprocessor would substitute 4 for x. If a
program read ABS(i) then the preprocessor would substitute i for x and so
on. (There is no reason why macros can’t take more than one parameter
too. The programmer just includes two dummy parameters with different
names. See the example listing below.) Notice that this definition uses a
curious operator which belongs to C:
<test > ? <true result > : <false result >

Example Listing
73
This is like a compact way of writing an ‘if..then..else’ statement, ideal
for macros. But it is also slightly different: it is an expression which returns
a value, where as an ‘if..then..else’ is a statement with no value. Firstly
the test is made. If the test is true then the first statement is carried out,
otherwise the second is carried out. As a memory aid, it could be read as:
if <test > then <true result > else <false result >
(Do not be confused by the above statement which is meant to show what a
programmer might think. It is not a valid C statement.) C can usually pro-
duce much more efficient code for this construction than for a corresponding
if-else statement.
12.2 When and when not to use macros with
parameters
It is tempting to forget about the distinction between macros and functions,
thinking that it can be ignored. To some extent this is true for absolute
beginners, but it is not a good idea to hold on to. It should always be
remembered that macros are substituted whole at every place where they
are used in a program: this is potentially a very large amount of repetition
of code. The advantage of a macro, however, is speed. No time is taken up
in passing control over to a new function, because control never leaves the
home function when a macro is used: it just makes the function a bit longer.
There is a limitation with macros though. Function calls cannot be used as
their parameters, such as:
ABS(function())
has no meaning. Only variables or number constants will be substituted.
Macros are also severely restricted in complexity by the limitations of the
preprocessor. It is simply not viable to copy complicated sequences of code
all over programs.
Choosing between functions and macros is a matter of personal judge-
ment. No simple rules can be given. In the end (as with all programming
choices) it is experience which counts towards the final ends. Functions are
easier to debug than macros, since they allow us to single step through the
code. Errors in macros are very hard to find, and can be very confusing.
12.3 Example Listing
/************************************************************/
/**/
/* MACRO DEMONSTRATION*/
/**/
/************************************************************/

74
Chapter 12: Preprocessor Commands
#include <stdio.h>
#define
#define
#define
#define
#define
#define
#define
STRING1
STRING2
EXPRESSION
EXPR2
ABS(x)
MAX(a,b)
BIGGEST(a,b,c)
“A macro definition\n”
“must be all on one line!!\n”
1 + 2 + 3 + 4
EXPRESSION + 10
((x) < 0) ? -(x) : (x)
(a < b) ? (b) : (a)
(MAX(a,b) < c) ? (c) : (MAX(a,b))
/************************************************************/
main ()
{
printf
printf
printf
printf
printf
printf
}
/* No #definitions inside functions! */
(STRING1);
(STRING2);
(“%d\n”,EXPRESSION);
(“%d\n”,EXPR2);
(“%d\n”,ABS(-5));
(“Biggest of 1 2 and 3 is %d”,BIGGEST(1,2,3));
12.4 Note about #include
When an include statement is written into a program, it is a sign that a
compiler should merge another file of C programming with the current one.
However, the #include statement is itself valid C, so this means that a file
which is included may contain #includes itself. The includes are then said
to be “nested”. This often makes includes simpler.
12.5 Other Preprocessor commands
This section lies somewhat outside the main development of the book. You
might wish to omit it on a first reading.
There are a handful more preprocessor commands which can largely be
ignored by the beginner. They are commonly used in “include” files to make
sure that things are not defined twice.
NOTE : ‘true’ has any non zero value in C. ‘false’ is zero.
#undef
#if
This undefines a macro, leaving the name free.
This is followed by some expression on the same line. It allows
conditional compilation. It is an advanced feature which can be

Example
75
used to say: only compile the code between ‘#if’ and ‘#endif’
if the value following ‘#if’ is true, else leave out that code alto-
gether. This is different from not executing code—the code will
not even be compiled.
#ifdef
#ifndef
#else
#endif
#line
This is followed by a macro name. If that macro is defined then
this is true.
This is followed by a macro name. If that name is not defined
then this is true.
This is part of an #if, #ifdef, #ifndef preprocessor statement.
This marks the end of a preprocessor statement.
Has the form:
#line constant ‘filename’
This is for debugging mainly. This statement causes the com-
piler to believe that the next line is line number (constant) and
is part of the file (filename).
#error
This is a part of the proposed ANSI standard. It is intended for
debugging. It forces the compiler to abort compilation.
12.6 Example
/***********************************************************/
/* To compile or not to compile*/
/***********************************************************/
#define SOMEDEFINITION 6546
#define CHOICE 1/* Choose this before compiling */
/***********************************************************/
#if (CHOICE == 1)
#define OPTIONSTRING “The programmer selected this”
#define DITTO”instead of ….”
#else
#define OPTIONSTRING “The alternative”
#define DITTO”i.e. This! “
#endif
/***********************************************************/
#ifdef SOMEDEFINITION

76
Chapter 12: Preprocessor Commands
#define WHATEVER “Something was defined!”
#else
#define WHATEVER “Nothing was defined”
#endif
/************************************************************/
main ()
{
printf (OPTIONSTRING);
printf (DITTO);
}
12.7 Questions
1. Define a macro called “birthday” which describes the day of the month
upon which your birthday falls.
2. Write an instruction to the preprocessor to include to maths library
‘math.h’.
3. A macro is always a number. True or false?
4. A macro is always a constant. True or false?

Pointers
77
13 Pointers
Making maps of data.
You have a map (a plan) of the computer’s memory. You need to find
that essential piece of information which is stored at some unknown location.
How will you find it? You need a pointer!
A pointers is a special type of variable which holds the address or location
of another variable. Pointers point to these locations by keeping a record
of the spot at which they were stored. Pointers to variables are found by
recording the address at which a variable is stored. It is always possible to
find the address of a piece of storage in C using the special ‘&’ operator. For
instance: if location were a float type variable, it would be easy to find a
pointer to it called location_ptr.
float location;
float *location_ptr,*address;
location_ptr = &(location);
or
address = &(location);
The declarations of pointers look a little strange at first. The star ‘*’ symbol
which stands in front of the variable name is C’s way of declaring that
variable to be a pointer. The four lines above make two identical pointers to
a floating point variable called location, one of them is called location_
ptr and the other is called address. The point is that a pointer is just a
place to keep a record of the address of a variable, so they are really the
same thing.
A pointer is a bundle of information that has two parts. One part is the
address of the beginning of the segment of memory that holds whatever is
pointed to. The other part is the type of value that the pointer points to
the beginning of. This tells the computer how much of the memory after the
beginning to read and how to interpret it. Thus, if the pointer is of a type
int, the segment of memory returned will be four bytes long (32 bits) and
be interpreted as an integer. In the case of a function, the type is the type
of value that the function will return, although the address is the address of
the beginning of the function executable.
If, like some modern day programmers, you believe in sanctity of high
level languages, it is probably a source of wonder why anyone Would ever
want to know the address of these variables. Having gone to the trouble to
design a high level language, like C, in which variables can be given elegant
and meaningful names: it seems like a step in the backward direction to

78
Chapter 13: Pointers
want to be able to find out the exact number of the memory location at
which it is stored! The whole point of variables, after all, is that it is not
necessary to know exactly where information is really stored. This is not
quite fair though. It is certainly rare indeed when we should want to know
the actual number of the memory location at which something is stored.
That would really make the idea of a high level language a bit pointless.
The idea behind pointers is that a high level programmer can now find out
the exact location of a variable without ever having to know the actual
number involved. Remember:
A pointer is a variable which holds the address of the storage location for
another given variable.
C provides two operators ‘&’ and ‘*’ which allow pointers to be used in
many versatile ways.
13.1 ‘&’ and ‘*’
The ‘&’ and ‘*’ operators have already been used once to hand back values
to variable parameters, See Section 10.2 [Value parameters], page 52. They
can be read in a program to have the following meanings:
&
*
‘*’
The address of…
The contents of the address held in…
The contents of the location pointed to by…
Another way of saying the second of these is:
This reinforces the idea that pointers reach out an imaginary hand and point
to some location in the memory and it is more usual to speak of pointers
in this way. The two operators ‘*’ and ‘&’ are always written in front of a
variable, clinging on, so that they refer, without doubt, to that one variable.
For instance:
‘&x’
‘*ptr’
The address at which the variable ‘x’ is stored.
The contents of the variable which is pointed to by ptr.
The following example might help to clarify the way in which they are used:
int somevar;
int *ptr_to_somevar;
somevar = 42;
ptr_to_somevar = &(somevar);
printf (“%d”,*ptr_to_somevar);
*ptr_to_somevar = 56;
/* 1 */
/* 2 */
/* 3 */
/* 4 */
/* 5 */
/* 6 */
The key to these statements is as follows:

Uses for Pointers
79
1. Declare an int type variable called somevar.
2. Declare a pointer to an int type called ptr_to_somevar. The ‘*’ which
stands in front of ptr_to_somevar is the way C declares ptr_to_
somevar as a pointer to an integer, rather than an integer.
3. Let somevar take the value 42.
4. This gives a value to ptr_to_somevar. The value is the address of
the variable somevar. Notice that only at this stage does is become a
pointer to the particular variable somevar. Before this, its fate is quite
open. The declaration (2) merely makes it a pointer which can point to
any integer variable which is around.
5. Print out “the contents of the location pointed to by ptr_to_somevar”
in other words somevar itself. So this will be just 42.
6. Let the contents of the location pointed to by ptr_to_somevar be 56.
This is the same as the more direct statement:
somevar = 56;
13.2 Uses for Pointers
It is possible to have pointers which point to any type of data whatsoever.
They are always declared with the ‘*’ symbol. Some examples are given
below.

80
Chapter 13: Pointers
int i,*ip;
char ch,*chp;
short s,*sp;
float x,*xp;
double y,*yp;
Pointers are extremely important objects in C. They are far more important
in C than in, say, Pascal or BASIC (PEEK,POKE are like pointers). In partic-
ular they are vital when using data structures like strings or arrays or linked
lists. We shall meet these objects in later chapters.
One example of the use of pointers is the C input function, which is
called scanf(). It is looked at in detail in the next section. scanf() is
for getting information from the keyboard. It is a bit like the reverse of
printf(), except that it uses pointers to variables, not variables themselves.
For example: to read an integer:
int i;
scanf (“%d”,&i);
or
int *i;
scanf (“%d”,i);
The ‘&’ sign or the ‘*’ sign is vital. If it is forgotten, scanf will probably
corrupt a program. This is one reason why this important function has been
ignored up to now.
Assembly language programmers might argue that there are occasions on
which it would be nice to know the actual address of a variable as a number.
One reason why one might want to know this would be for debugging. It is
not often a useful thing to do, but it is not inconceivable that in developing
some program a programmer would want to know the actual address. The
‘&’ operator is flexible enough to allow this to be found. It could be printed
out as an integer:
type *ptr:
printf (“Address = %d”,(int) ptr);

Example Listing
81
13.3 Pointers and Initialization
Something to be wary of with pointer variables is the way that they are
initialized. It is incorrect, logically, to initialize pointers in a declaration.
A compiler will probably not prevent this however because there is nothing
incorrect about it as far as syntax is concerned.
Think about what happens when the following statement is written. This
statement is really talking about two different storage places in the memory:
int *a = 2;
First of all, what is declared is a pointer, so space for a ‘pointer to int’
is allocated by the program and to start off with that space will contain
garbage (random numbers), because no statement like
a = &someint;
has yet been encountered which would give it a value. It will then attempt
to fill the contents of some variable, pointed to by a, with the value 2.
This is doomed to faliure. a only contains garbage so the 2 could be stored
anywhere. There may not even be a variable at the place in the memory
which a points to. Nothing has been said about that yet. This kind of
initialization cannot possibly work and will most likely crash the program
or corrupt some other data.
13.4 Example Listing
/**********************************************/
/**/
/* Swapping Pointers*/
/**/
/**********************************************/
/* Program swaps the variables which a,b */
/* point to. Not pointless really !*/
#include <stdio.h>
main ()
{ int *a,*b,*c;
int A,B;
A = 12;
B = 9;
a = &A;
/* Declr ptrs */
/* Declare storage */
/* Initialize storage */
/* Initialize pointers */

82
b = &B;
printf (“%d %d\n”,*a,*b);
c = a;
a = b;
b = c;
printf (“%d %d\n”,*a,*b);
}
/* swap pointers */
Chapter 13: Pointers

Types, Casts and Pointers
83
13.5 Types, Casts and Pointers
It is tempting but incorrect to think that a pointer to an integer is the
same kind of object as a pointer to a floating point object or any other
type for that matter. This is not necessarily the case. Compilers distinguish
between pointers to different kinds of objects. There are occasions however

84
Chapter 13: Pointers
when it is actually necessary to convert one kind of pointer into another.
This might happen with a type of variable called “unions” or even functions
which allocate storage for special uses. These objects are met later on in this
book. When this situation comes about, the cast operator has to be used
to make sure that pointers have compatible types when they are assigned
to one another. The cast operator for variables, See undefined [The Cast
Operator], page undefined , is written in front of a variable to force it to be
a particular type:
(type )
variable
For pointers it is:
(type *) pointer
Look at the following statement:
char *ch;
int *i;
i = (int *) ch;
This copies the value of the pointer ch to the pointer i. The cast operator
makes sure that the pointers are in step and not talking at cross purposes.
The reason that pointers have to be ‘cast’ into shape is a bit subtle and
depends upon particular computers. In practice it may not actually do
anything, but it is a necessary part of the syntax of C.
Pointer casting is discussed in greater detail in the chapter on Structures
and Unions.
13.6 Pointers to functions
This section is somewhat outside of the main development of the book. You
might want to omit it on first reading.
Let’s now consider pointers to functions as opposed to variables. This
is an advanced feature which should be used with more than a little care.
The idea behind pointers to functions is that you can pass a function as a
parameter to another function! This seems like a bizarre notion at first but
in fact it makes perfect sense.
Pointers to functions enable you to tell any function which sub-ordinate
function it should use to do its job. That means that you can plug in a new
function in place of an old one just by passing a different parameter value to
the function. You do not have to rewrite any code. In machine code circles
this is sometimes called indirection or vectoring.

Calling a function by pointer
85
When we come to look at arrays, we’ll find that a pointer to the start
of an array can be found by using the name of the array itself without the
square brackets []. For functions, the name of the function without the
round brackets works as a pointer to the start of the function, as long as
the compiler understands that the name represents the function and not a
variable with the same name. So—to pass a function as a parameter to
another function you would write
function1(function2);
If you try this as it stands, a stream of compilation errors will be the result.
The reason is that you must declare function2() explicitly like this:
int function2();
If the function returns a different type then clearly the declaration will be
different but the form will be the same. The declaration can be placed
together with other declarations. It is not important whether the variable
is declared locally or globally, since a function is a global object regardless.
What is important is that we declare specifically a pointer to a function
which returns a type (even if it is void). The function which accepts a
function pointer as an argument looks like this:
function1 (a)
int (*a)();
{ int i;
i = (*a)(parameters );
}
This declares the formal parameter a to be a pointer to a function returning
a value of type int. Similarly if you want to declare a pointer to a function
to a general type typename with the name fnptr, you would do it like this:
typename (*fnptr)();
13.7 Calling a function by pointer
Given a pointer to a function how do we call the function? The syntax is
this:
variable = (*fnptr)(parameters );
An example let us look at a function which takes an integer and returns
a character.
int i;
char ch, function();
Normally this function is called using the statement:

86
Chapter 13: Pointers
ch = function(i);
but we can also do the same thing with a pointer to the function. First
define
char function();
char (*fnptr)();
fnptr = function;
then call the function with
ch = (*fnptr)(i);
A pointer to a function can be used to provide a kind of plug-in interface
to a logical device, i.e. a way of choosing the right function for the job.
void printer(),textscreen(),windows();
switch (choice)
{
case 1: fnptr = printer;
break;
case 2: fnptr = textscreen;
break;
case 3: fnptr = windows;
}
Output(data,fnptr);
This is the basis of ‘polymorphism’ found in object oriented languages:
a choice of a logical (virtual) function based on some abstract label (the
choice). The C++ language provides an abstract form of this with a more
advanced syntax, but this is the essence of virtual function methods in object
oriented languages.
BEWARE! A pointer to a function is an automatic local variable. Local
variables are never initialized by the compiler in C. If you inadvertently
forget to initialize the pointer to a function, you will come quickly to grief.
Make sure that your pointers are assigned before you use them!
13.8 Questions
1. What is a pointer?
2. How is a variable declared to be a pointer?
3. What data types can pointers “point to”?

Questions
87
4. Write a statement which converts a pointer to a character into a pointer
to a double type. (This is not as pointless as it seems. It is useful in
dealing with unions and memory allocation functions.)
5. Why is it incorrect to declare: float *number = 2.65; ?

88
Chapter 13: Pointers

Standard Output and Standard Input
89
14 Standard Output and Standard Input
Talking to the user.
Getting information in and out of a computer is the most important thing
that a program can do. Without input and output computers would be quite
useless.
C treats all its output as though it were reading or writing to different
files. A file is really just an abtraction: a place where information comes
from or can be sent to. Some files can only be read, some can only be written
to, others can be both read from and written to. In other situations files are
called I/O streams.

90
Chapter 14: Standard Output and Standard Input
C has three files (also called streams) which are always open and ready for
use. They are called stdin, stdout and stderr, meaning standard input and
standard output and standard error file. Stdin is the input which usually
arrives from the keyboard of a computer. stdout is usually the screen. stderr
is the route by which all error messages pass: usually the screen. This is
only ‘usually’ because the situation can be altered. In fact what happens is
that these files are just handed over to the local operating system to deal
with and it chooses what to do with them. Usually this means the keyboard
and the screen, but it can also be redirected to a printer or to a disk file or
to a modem etc.. depending upon how the user ran the program.
The keyboard and screen are referred to as the standard input/output files
because this is what most people use, most of the time. Also the programmer
never has to open or close these, because C does it automatically. The C
library functions covered by ‘stdio.h’ provides some methods for working
with stdin and stdout. They are simplified versions of the functions that
can be used on any kind of file, See undefined [Files and Devices], page un-
defined . In order of importance, they are:
printf ()
scanf ()
getchar()
putchar()
gets()
puts()
14.1 printf
The printf function has been used widely up to now for output because it
provides a neat and easy way of printing text and numbers to stdout (the
screen). Its name is meant to signify formatted printing because it gives
the user control over how text and numerical data are to be laid out on the
screen. Making text look good on screen is important in programming. C
makes this easy by allowing you to decide how the text will be printed in
the available space. The printf function has general form:
printf (“string…”,variables,numbers )
It contains a string (which is not optional) and it contains any number of
parameters to follow: one for each blank field in the string.
The blank fields are control sequences which one can put into the string
to be filled in with numbers or the contents of variables before the final
result is printed out. These fields are introduced by using a ‘%’ character,
followed by some coded information, which says something about the size of
the blank space and the type of number or string which will be filled into
that space. Often the string is called the control string because it contains
these control characters.

printf
91
The simplest use of printf is to just print out a string with no blank
fields to be filled:
printf (“A pretty ordinary string..”);
printf (“Testing 1,2,3…”);
The next simplest case that has been used before now is to print out a
single integer number:
int number = 42;
printf (“%d”,number);
The two can be combined:
int number = 42;
printf (“Some number = %d”,number);
The result of this last example is to print out the following on the screen:
Some number = 42
The text cursor is left pointing to the character just after the 2. Notice the
way that %d is swapped for the number 42. %d defines a field which is filled
in with the value of the variable.
There are other kinds of data than integers though. Any kind of variable
can be printed out with printf. %d is called a conversion character for
integers because it tells the compiler to treat the variable to be filled into it
as an integer. So it better had be an integer or things will go wrong! Other
characters are used for other kinds of data. Here is a list if the different
letters for printf.
d
u
x
o
s
c
f
e
g
signed denary integer
unsigned denary integer
hexadecimal integer
octal integer
string
single character
fixed decimal floating point
scientific notation floating point
use f or e, whichever is shorter

92
Chapter 14: Standard Output and Standard Input
The best way to learn these is to experiment with different conversion
characters. The example program and its output below give some impression
of how they work:
14.2 Example Listing
/*******************************************************/
/**/
/* printf Conversion Characters and Types*/
/**/
/*******************************************************/
#include <stdio.h>
main ()
{ int i = -10;
unsigned int ui = 10;
float x = 3.56;
double y = 3.52;
char ch = ’z’;
char *string_ptr = “any old string”;
printf (“signed integer %d\n”, i);
printf (“unsigned integer %u\n”,ui);
printf (“This is wrong! %u”,i);
printf (“See what happens when you get the “);
printf (“character wrong!”);
printf (“Hexadecimal %x %x\n”,i,ui);
printf (“Octal %o %o\n”,i,ui);
printf (“Float and double %f %f\n”,x,y);
printf (“ditto%e %e\n”,x,y);
printf (“ditto%g %g\n”,x,y);
printf (“single character %c\n”,ch);
printf (“whole string -> %s”,string_ptr);
}
14.3 Output
signed integer -10
unsigned integer 10
This is wrong! 10See what happens when you get the character wrong!Hexadecimal FFFFFFF6 A
Octal 37777777766 12
Float and double 3.560000 3.520000
ditto3.560000E+00 3.520000E+00

Formatting with printf
ditto3.560000 3.520000
single character z
whole string -> any old string
93
14.4 Formatting with printf
The example program above does not produce a very neat layout on the
screen. The conversion specifiers in the printf string can be extended to give
more information. The ‘%’ and the character type act like brackets around
the extra information. e.g.
%-10.3f
is an extended version of ‘%f’, which carries some more information. That
extra information takes the form:
% [-] [fwidth ] [.p ] X
where the each bracket is used to denote that the item is optional and the
symbols inside them stand for the following.
[fwidth ] This is a number which specifies the field width of this “blank
field”. In other words, how wide a space will be made in the
string for the object concerned? In fact it is the minimum field
width because if data need more room than is written here they
will spill out of their box of fixed size. If the size is bigger than
the object to be printed, the rest of the field will be filled out
with spaces.
[-]
If this included the output will be left justified. This means it
will be aligned with the left hand margin of the field created with
[fwidth ]. Normally all numbers are right justified, or aligned
with the right hand margin of the field “box”.
[.p ]
This has different meanings depending on the object which is
to be printed. For a floating point type (float or double) p
specifies the number of decimal places after the point which are
to be printed. For a string it specifies how many characters are
to be printed.
Some valid format specifiers are written below here.
%10d
%2.2f
%25.21s
%2.6f
The table below helps to show the effect of changing these format controls.
The width of a field is draw in by using the | bars.
Object to
be printed
42
Control Spec.
Actual Output
%6d
|
42|

94
42
324
-1
-1
’z’
’z’
2.71828
2.71828
2.71828
2.71828
2.718
2.718
2.71828
2.71828
2.71828
“printf”
“printf”
“printf”
“printf”
“printf”
“printf”
Chapter 14: Standard Output and Standard Input
%-6d
%10d
%-10d
%1d
%3c
%-3c
%10f
%10.2f
%-10.2f
%2.4f
%.4f
%10.5f
%10e
%10.2e
%10.2g
%s
%10s
%2s
%5.3s
%-5.3s
%.3s
|42|
|324|
|-1|
|-1|(overspill)
| z|
|z |
|2.71828|
|2.71|
|2.71|
|2.7182|(overspill)
|2.7180|
|2.71800|
|2.71828e+00|
| 2.17e+00|
|2.71|
|printf|
|printf|
|printf|(overspill)
| pri|
|pri |
|pri|
14.5 Example Listing
/***********************************************/
/**/
/* Multiplication Table*/
/**/
/***********************************************/
#include <stdio.h>
main ()
{ int i,j;
/* Printing in columns */
for (i = 1; i <= 10; i++)
{
for (j = 1; j <= 10; j++)
{
printf (“%5d”,i * j);
}
printf (“\n”);
}
}

Questions
95
14.6 Output
1
2
3
4
5
6
7
8
9
10
2
4
6
8
10
12
14
16
18
20
3
6
9
12
15
18
21
24
27
30
4
8
12
16
20
24
28
32
36
40
5
10
15
20
25
30
35
40
45
50
6
12
18
24
30
36
42
48
54
60
7
14
21
28
35
42
49
56
63
70
8
16
24
32
40
48
56
64
72
80
9
18
27
36
45
54
63
72
81
90
10
20
30
40
50
60
70
80
90
100
14.7 Special Control Characters
Control characters are invisible on the screen. They have special purposes
usually to do with cursor movement. They are written into an ordinary
string by typing a backslash character \ followed by some other character.
These characters are listed below.
\b
\f
\n
\r
\t
\v
\”
\’
\\
\ddd
backspace BS
form feed FF (also clear screen)
new line NL (like pressing return)
carriage return CR (cursor to start of line)
horizontal tab HT
vertical tab
double quote
single quote character ’
backslash character ‘\’
character ddd where ddd is an ASCII code given in octal or
base 8, See undefined [Character Conversion Table], page un-
defined .
character ddd where ddd is an ASCII code given in hexadeci-
mal or base 16, See undefined [Character Conversion Table],
page undefined .
\xddd

96
Chapter 14: Standard Output and Standard Input
14.8 Questions
1. Write a program which simply prints out: ‘6.23e+00’
2. Investigate what happens when you type the wrong conversion specifier
in a program. e.g. try printing an integer with ‘%f’ or a floating point
number with ‘%c’. This is bound to go wrong – but how will it go wrong?
3. What is wrong with the following statements?
1. printf (x);
2. printf (“%d”);
3. printf ();
4. printf (“Number = %d”);
Hint: if you don’t know, try them in a program!
14.9 scanf
scanf is the input function which gets formatted input from the file stdin
(the keyboard). This is a very versatile function but it is also very easy to
go wrong with. In fact it is probably the most difficult to understand of all
the C standard library functions.
Remember that C treats its keyboard input as a file. This makes quite a
difference to the way that scanf works. The actual mechanics of scanf are
very similar to those of printf in reverse
scanf (“string…”,pointers);
with one important exception: namely that it is not variables which are
listed after the control string, but pointers to variables. Here are some valid
uses of scanf:
int i;
char ch;
float x;
scanf (“%d %c %f”, &i, &ch, &x);
Notice the ‘&’ characters which make the arguments pointers. Also notice
the conversion specifiers which tell scanf what types of data it is going to
read. The other possibility is that a program might already have pointers to
a particular set of variables in that case the ‘&’ is not needed. For instance:
function (i,ch,x)
int *i;
char *ch;
float *x;
{

How does scanf see the input?
scanf (“%d %c %f”, i, ch, x);
}
97
In this case it would actually be wrong to write the ampersand ‘&’ symbol.
14.10 Conversion characters
The conversion characters for scanf are not identical to those for printf and
it is much more important to be precise and totally correct with these than
it is with printf.
d
ld
x
o
h
f
lf
e
le
c
s
denary integer (int or long int)
long decimal integer
hexadecimal integer
octal integer
short integer
float type
long float or double
float type
double
single character
character string
The difference between short integer and long integer can make or break
a program. If it is found that a program’s input seems to be behaving
strangely, check these carefully. (See the section on Errors and Debugging
for more about this.)
14.11 How does scanf see the input?
When scanf is called in a program it checks to see what is in the input
file, that is, it checks to see what the user has typed in at the keyboard.
Keyboard input is usually buffered. This means that the characters are held
in a kind of waiting bay in the memory until they are read. The buffer can
be thought of as a part of the input file stdin, holding some characters which
can be scanned though. If the buffer has some characters in it, scanf will
start to look through these; if not, it will wait for some characters to be put
into the buffer.
There is an important point here: although scanf will start scanning
through characters as soon as they are in the buffer, the operating system
often sees to it that scanf doesn’t get to know about any of the characters
until the user has pressed the RETURN or ENTER key on the computer or

98
Chapter 14: Standard Output and Standard Input
terminal. If the buffer is empty scanf will wait for some characters to be
put into it.
To understand how scanf works, it is useful to think of the input as
coming in ‘lines’. A line is a bunch of characters ending in a newline character
‘\n’. This can be represented by a box like the one below:
————————————–
| some…chars.738/.|’\n’|
————————————–
As far as scanf is concerned, the input is entirely made out of a stream
of characters. If the programmer says that an integer is to be expected by
using the ‘%d’ conversion specifier then scanf will try to make sense of the
characters as an integer. In other words, it will look for some characters
which make up a valid integer, such as a group of numbers all between 0 and
9. If the user says that floating point type is expected then it will look for
a number which may or may not have a decimal point in it. If the user just
wants a character then any character will do!
14.12 First account of scanf
Consider the example which was give above.
int i;
char ch;
float x;
scanf (“%d %c %f”, &i, &ch, &x);
Here is a simplified, ideal view of what happens. scanf looks at the control
string and finds that the first conversion specifier is ‘%d’ which means an
integer. It then tries to find some characters which fit the description of an
integer in the input file. It skips over any white space characters (spaces,
newlines) which do not constitute a valid integer until it matches one. Once
it has matched the integer and placed its value in the variable i it carries
on and looks at the next conversion specifier ‘%c’ which means a character.
It takes the next character and places it in ch. Finally it looks at the last
conversion specifier ‘%f’ which means a floating point number and finds some
characters which fit the description of a floating point number. It passes the
value onto the variable x and then quits.
This brief account of scanf does not tell the whole story by a long way. It
assumes that all the characters were successfully found and that everything
went smoothly: something which seldom happens in practice!

Keeping scanf under control
99
14.13 The dangerous function
What happens if scanf doesn’t find an integer or a float type? The answer is
that it will quit at the first item it fails to match, leaving that character and
the rest of the input line still to be read in the file. At the first character it
meets which does not fit in with the conversion string’s interpretation scanf
aborts and control passes to the next C statement. This is why scanf is a
‘dangerous’ function: because it can quit in the middle of a task and leave
a lot of surplus data around in the input file. These surplus data simply
wait in the input file until the next scanf is brought into operation, where
they can also cause it to quit. It is not safe, therefore, to use scanf by itself:
without some check that it is working successfully.
scanf is also dangerous for the opposite reason: what happens if scanf
doesn’t use up all the characters in the input line before it satisfies its needs?
Again the answer is that it quits and leaves the extra characters in the input
file stdin for the next scanf to read, exactly where it left off. So if the
program was meant to read data from the input and couldn’t, it leaves a
mess for something else to trip over. scanf can get out of step with its input
if the user types something even slightly out of line. It should be used with
caution…
14.14 Keeping scanf under control
scanf may be dangerous for sloppy programs which do not check their input
carefully, but it is easily tamed by using it as just a part of a more sophisti-
cated input routine and sometimes even more simply with the aid of a very
short function which can be incorporated into any program:
skipgarb()
/* skip garbage corrupting scanf */
{
while (getchar() != ’\n’)
{
}
}
The action of this function is simply to skip to the end of the input line
so that there are no characters left in the input. It cannot stop scanf from
getting out of step before the end of a line because no function can stop
the user from typing in nonsense! So to get a single integer, for instance, a
program could try:
int i;
scanf(“%d”,&i);
skipgarb();
The programmer must police user-garbage personally by using a loop to the
effect of:

100
Chapter 14: Standard Output and Standard Input
while (inputisnonsense)
{
printf (“Get your act together out there!!\n”);
scanf (..)
skipgarb();
}
It is usually as well to use skipgarb() every time.
14.15 Examples
Here are some example programs with example runs to show how scanf
either works or fails.
/****************************************/
/* Example 1*/
/****************************************/
#include <stdio.h>
main ()
{ int i = 0;
char ch = ’*’;
float x = 0;
scanf (“%d %c %f”,&i,&ch,&x);
printf (“%d %c %f\n”,i,ch,x);
}
This program just waits for a line from the user and prints out what it
makes of that line. Things to notice about these examples are the way in

Examples
101
which scanf ‘misunderstands’ what the user has typed in and also the values
which the variables had before the scanf function.
Input : 1×2.3
Output: 1 x 2.300000

102
Chapter 14: Standard Output and Standard Input
The input gets broken up in the following way:
——————
| 1 |’x’| 2.3 |’\n’|
——————
In this example everything works properly. There are no spaces to confuse
matters. it is simple for scanf to see what the first number is because the
next character is x which is not a valid number.
Input : 1 x 2.3
Output: 10.000000
————
|1|’ ’| <break> |x 2.3|
————
In this example the integer is correctly matched as 1. The character is now
a space and the x is left in the stream. The x does not match the description
of a float value so scanf terminates, leaving x 2.3 still in the input stream.
Input : .
Output: 0 * 0.000000

|’.’| <break>

Examples
103

104
Chapter 14: Standard Output and Standard Input
A single full-stop (period). scanf quits straight away because it looks for
an integer. It leaves the whole input line (which is just the period ‘.’) in the
input stream.
/****************************************/
/* Example 2*/
/****************************************/
#include <stdio.h>
main ()
{ int i = 0;
char ch = ’*’,ch2,ch3;
float x = 0;
scanf (“%d %c %f”, &i,&ch,&x);
scanf (“%c %c”, &ch2,&ch3);
printf (“%d %c %f\n %c %c”);
}
The input for this program is:
6 x2.36
and the output is:
60.000000
x 2
———————-
| 6 | ’ ’ | <break> |’x’|’2’| .36 |
———————-
Here the integer is successfully matched with 6. The character is matched
with a space but the float character finds an x in the way, so the first scanf
aborts leaving the value of x unchanged and the rest of the characters still
in the file. The second scanf function then picks these up. It can be seen
that the first two characters are the x which caused the previous scanf to
fail and the first 2 of the intended floating point number.
/****************************************/
/* Example 3*/
/****************************************/
#include <stdio.h>
main()
{ char ch1,ch2,ch3;
scanf (“%c %c %c”,&ch1,&ch2,&ch3);

Matching without assigning
printf (“%c %c %c”,ch1,ch2,ch3);
}
105
Trials:
input : abc
output: a b c
input : a [return]
b [return]
c [return]
output: a b c
input : 2.3
output: 2 . 3
14.16 Matching without assigning
scanf allows input types to be matched but then discarded without being
assigned to any variable. It also allows whole sequences of characters to be
matched and skipped. For example:
scanf (“%*c”);
would skip a single character. The ‘*’ character means do not make an
assignment. Note carefully that the following is wrong:
scanf (“%*c”, &ch);
A pointer should not be given for a dummy conversion character. In this
simple case above it probably does not matter, but in a string with sev-
eral things to be matched, it would make the conversion characters out of
step with the variables, since scanf does not return a value from a dummy
conversion character. It might seem as though there would be no sense in
writing:
scanf (“%*s %f %c”,&x,&ch);
because the whole input file is one long string after all, but this is not true
because, as far as scanf is concerned a string is terminated by any white
space character, so the float type x and the character ch would receive values
provided there were a space or newline character after any string.
If any non-conversion characters are typed into the string scanf will match
and skip over them in the input. For example:
scanf (” Number = %d”,&i);

106
Chapter 14: Standard Output and Standard Input
If the input were: Number = 256, scanf would skip over the Number =
. As usual, if the string cannot be matched, scanf will abort, leaving the
remaining characters in the input stream.
/****************************************/
/* Example 4*/
/****************************************/
#include <stdio.h>
main()
{ float x = 0;
int i = 0;
char ch = ’*’;
scanf(“Skipthis! %*f %d %*c”,&i);
printf(“%f %d %c”,x,i,ch);
}
Input : Skipthis! 23
Output: 0.000000 23 *
Input : 26
Output: 0.000000 0 *
In this last case scanf aborted before matching anything.
14.17 Formal Definition of scanf
The general form of the scanf function is:
n = scanf (“string…”, pointers);
The value n returned is the number of items matched or the end of file
character EOF, or NULL if the first item did not match. This value is often
discarded. The control string contains a number of conversion specifiers with
the following general form:
%[*][n]X
[*]
[n]
the optional assignment suppression character.
this is a number giving the maximum field width to be accepted
by scanf for a particular item. That is, the maximum number of

Questions
107
characters which are to be thought of as being part of one the
current variable value.
X
is one of the characters listed above.
Any white space characters in the scanf string are ignored. Any other
characters are matched. The pointers must be pointers to variables of the
correct type and they must match the conversion specifiers in the order in
which they are written.
There are two variations on the conversion specifiers for strings, though
it is very likely that many compilers will not support this. Both of the
following imply strings:
%[set of characters ]
a string made up of the given characters only.
%[^set of characters ]
a string which is delimited by the set of characters given.
For example, to read the rest of a line of text, up to but not including the
end of line, into a string array one would write:
scanf(“%[^\n]”,stringarray);
14.18 Summary of points about scanf
• Scanf works across input lines as though it were dealing with a file.
Usually the user types in a line and hits return. The whole line is then
thought of as being part of the input file pointer stdin.
• If scanf finds the end of a line early it will try to read past it until all
its needs are satisfied.
• If scanf fails at any stage to match the correct type of string at the
correct time, it will quit leaving the remaining input still in the file.
• If an element is not matched, no value will be assigned to the corre-
sponding variable.
• White space characters are ignored for all conversion characters except
%c. Only a %c type can contain a white space character.
• White space characters in
14.19 Questions
1. What is a white space character?
2. Write a program which fetches two integers from the user and multiplies
them together. Print out the answer. Try to make the input as safe as
possible.
3. Write a program which just echoes all the input to the output.

108
Chapter 14: Standard Output and Standard Input
4. Write a program which strips spaces out of the input and replaces them
with a single newline character.
5. scanf always takes pointer arguments. True or false?
14.20 Low Level Input/Output
14.20.1 getchar and putchar
scanf() and printf() are relatively high level functions: this means that
they are versatile and do a lot of hidden work for the user. C also provides
some functions for dealing with input and output at a lower level: character
by character. These functions are called getchar() and putchar() but,
in fact, they might not be functions: they could be macros instead, See
Chapter 12 [Preprocessor], page 71.
high level:
printf()
/
low level: putchar()
|
|
|
|
|
|
|
scanf()
\
getchar()
getchar gets a single character from the input file stdin; putchar writes a
single character to the output file stdout. getchar returns a character type:
the next character on the input file. For example:
char ch;
ch = getchar();
This places the next character, what ever it might be, into the variable
ch. Notice that no conversion to different data types can be performed by
getchar() because it deals with single characters only. It is a low level func-
tion and does not ‘know’ anything about data types other than characters.
getchar was used in the function skipgarb() to tame the scanf() func-
tion. This function was written in a very compact way. Another way of
writing it would be as below:
skipgarb ()
{ char ch;
ch = getchar();
while (ch != ’\n’)
{
ch = getchar();
/* skip garbage corrupting scanf */

gets and puts
}
}
109
The ‘!=’ symbol means “is not equal to” and the while statement is a loop.
This function keeps on getchar-ing until it finds the newline character and
then it quits. This function has many uses. One of these is to copy immediate
keypress statements of languages like BASIC, where a program responds to
keys as they are pressed without having to wait for return to be pressed.
Without special library functions to give this kind of input (which are not
universal) it is only possible to do this with the return key itself. For example:
printf(“Press RETURN to continue\n”);
skipgarb();
skipgarb() does not receive any input until the user presses RETURN, and
then it simply skips over it in one go! The effect is that it waits for RETURN
to be pressed.
putchar() writes a character type and also returns a character type. For
example:
char ch = ’*’;
putchar (ch);
ch = putchar (ch);
These two alternatives have the same effect. The value returned by
putchar() is the character which was written to the output. In other words
it just hands the same value back again. This can simply be discarded, as
in the first line. putchar() is not much use without loops to repeat it over
and over again.
An important point to remember is that putchar() and getchar() could
well be implemented as macros, rather than functions. This means that it
might not be possible to use functions as parameters inside them:
putchar( function() );
This depends entirely upon the compiler, but it is something to watch out
for.
14.20.2 gets and puts
Two functions which are similar to putchar() and getchar() are puts()
and gets() which mean putstring and getstring respectively. Their purpose
is either to read a whole string from the input file stdin or write a whole
string to the output stdout. Strings are groups or arrays of characters. For
instance:
char *string[length];

110
Chapter 14: Standard Output and Standard Input
string = gets(string);
puts(string);
More information about these is given later, See undefined [Strings],
page undefined .
14.21 Questions
1. Is the following statement possible? (It could depend upon your com-
piler: try it!)
putchar(getchar());
What might this do? (Hint: re-read the chapter about the pre-
processor.)
2. Re write the statement in question 1, assuming that putchar() and
getchar() are macros.

Expressions and values
111
15 Assignments, Expressions and
Operators
Thinking in C. Working things out.
An operator is something which takes one or more values and does some-
thing useful with those values to produce a result. It operates on them. The
terminology of operators is the following:
operator
operand
operation
Something which operates on someting.
Each thing which is operated upon by an operator is called an
operand.
The action which was carried out upon the operands by the
operator!
There are lots of operators in C. Some of them may already be familiar:
+

*
/
=
& ==
Most operators can be thought of as belonging to one of three groups, divided
up arbitrarily according to what they do with their operands. These rough
groupings are thought of as follows:
• Operators which produce new values from old ones. They make a result
from their operands. e.g. +, the addition operator takes two numbers
or two variables or a number and a variable and adds them together to
give a new number.
• Operators which make comparisons. e.g. less than, equal to, greater
than…
• Operators which produce new variable types: like the cast operator.
The majority of operators fall into the first group. In fact the second
group is a subset of the first, in which the result of the operation is a boolean
value of either true of false.
C has no less than thirty nine different operators. This is more than, say,
Pascal and BASIC put together! The operators serve a variety of purposes
and they can be used very freely. The object of this chapter is to explain
the basics of operators in C. The more abstruse operators are looked at in
another chapter.
15.1 Expressions and values
The most common operators in any language are basic arithmetic operators.
In C these are the following:
+
plus (unary)

112

+

*
/
/
Chapter 15: Assignments, Expressions and Operators
minus (force value to be negative)
addition
subtraction
multiplication
floating point division
integer division “div”
%integer remainder “mod”
These operators would not be useful without a partner operator which could
attach the values which they produce to variables. Perhaps the most impor-
tant operator then is the assignment operator:
=
assignment operator
This has been used extensively up to now. For example:
double x,y;
x = 2.356;
y = x;
x = x + 2 + 3/5;
The assignment operator takes the value of whatever is on the right hand
side of the ‘=’ symbol and puts it into the variable on the left hand side. As
usual there is some standard jargon for this, which is useful to know because
compilers tend to use this when handing out error messages. The assignment
operator can be summarized in the following way:
lvalue = expression ;
This statement says no more than what has been said about assignments
already: namely that it takes something on the right hand side and attaches
it to whatever is on the left hand side of the ‘=’ symbol. An expression is
simply the name for any string of operators, variables and numbers. All of
the following could be called expressions:
1 + 2 + 3
a + somefunction()
32 * x/3
i % 4
x

Output
1
(22 + 4*(function() + 2))
function ()
/* provided it returns a sensible value */
113
Lvalues on the other hand are simply names for memory locations: in other
words variable names, or identifiers. The name comes from ‘left values’
meaning anything which can legally be written on the left hand side of an
assignment.
15.2 Example
/**************************************/
/**/
/* Operators Demo # 1*/
/**/
/**************************************/
#include <stdio.h>
/**************************************/
main ()
{ int i;
printf (“Arithmetic Operators\n\n”);
i = 6;
printf (“i = 6, -i is : %d\n”, -i);
printf (“int 1 + 2 = %d\n”, 1 + 2);
printf (“int 5 – 1 = %d\n”, 5 – 1);
printf (“int 5 * 2 = %d\n”, 5 * 2);
printf (“\n9 div 4 = 2 remainder 1:\n”);
printf (“int 9 / 4 = %d\n”, 9 / 4);
printf (“int 9 % 4 = %d\n”, 9 % 4);
printf (“double 9 / 4 = %f\n”, 9.0 / 4.0);
}
15.3 Output
Arithmetic Operators
i = 6, -i is : -6

114
int 1 + 2 = 3
int 5 – 1 = 4
int 5 * 2 = 10
Chapter 15: Assignments, Expressions and Operators
9 div 4 = 2 remainder 1:
int 9 / 4 = 2
int 9 4 = 1
double 9 / 4 = 2.250000
15.4 Parentheses and Priority
Parentheses are classed as operators by the compiler, although their position
is a bit unclear. They have a value in the sense that they assume the value
of whatever expression is inside them. Parentheses are used for forcing a
priority over operators. If an expression is written out in an ambiguous way,
such as:
a + b / 4 * 2
it is not clear what is meant by this. It could be interpreted in several ways:
((a + b) / 4) * 2
or
(a + b)/ (4 * 2)
or
a + (b/4) * 2
and so on. By using parentheses, any doubt about what the expression
means is removed. Parentheses are said to have a higher priority than + *
or / because they are evaluated as “sealed capsules” before other operators
can act on them. Putting parentheses in may remove the ambiguity of
expressions, but it does not alter than fact that
a + b / 4 * 2
is ambiguous. What will happen in this case? The answer is that the C
compiler has a convention about the way in which expressions are evaluated:
it is called operator precedence. The convention is that some operators are
stronger than others and that the stronger ones will always be evaluated first.
Otherwise, expressions like the one above are evaluated from left to right: so
an expression will be dealt with from left to right unless a strong operator
overrides this rule. Use parentheses to be sure. A table of all operators and
their priorities is given in the reference section.

Special Assignment Operators ++ and —
115
15.5 Unary Operator Precedence
Unary operators are operators which have only a single operand: that is,
they operate on only one object. For instance:
++

+

&
The precedence of unary operators is from right to left so an expression like:
*ptr++;
would do ++ before *.
15.6 Special Assignment Operators ++ and —
C has some special operators which cut down on the amount of typing in-
volved in a program. This is a subject in which it becomes important to
think in C and not in other languages. The simplest of these perhaps are
the increment and decrement operators:
++
increment: add one to
–decrement: subtract one from
These attach to any variable of integer or floating point type. (character
types too, with care.) They are used to simply add or subtract 1 from a
variable. Normally, in other languages, this is accomplished by writing:
variable = variable + 1;
In C this would also be quite valid, but there is a much better way of doing
this:
variable++; or
++variable;
would do the same thing more neatly. Similarly:
variable = variable – 1;
is equivalent to:
variable–;
or
–variable;
Notice particularly that these two operators can be placed in front or after
the name of the variable. In some cases the two are identical, but in the

116
Chapter 15: Assignments, Expressions and Operators
more advanced uses of C operators, which appear later in this book, there
is a subtle difference between the two.
15.7 More Special Assignments
Here are some of the nicest operators in C. Like ++ and — these are short
ways of writing longer expressions. Consider the statement:
variable = variable + 23;
In C this would be a long winded way of adding 23 to variable. It could
be done more simply using the general increment operator: +=
variable += 23;
This performs exactly the same operation. Similarly one could write:
variable1 = variable1 + variable2;
as
variable1 += variable2;
and so on. There is a handful of these
<operation>=
operators: one for each of the major operations which can be performed.
There is, naturally, one for subtraction too:
variable = variable – 42;
can be written:
variable -= 42;
More surprisingly, perhaps, the multiplicative assignment:
variable = variable * 2;
may be written:
variable *= 2;
and so on. The main arithmetic operators all follow this pattern:
+=
-=
add assign
subtract assign

Example Listing
*=
/=
%=
multiply assign
divide (double) and (int) types
117
remainder (int) type only.
and there are more exotic kinds, used for bit operations or machine level
operations, which will be ignored at this stage:
>>=
<<=
^=
|=
&=
15.8 Example Listing
/**************************************/
/**/
/* Operators Demo # 2*/
/**/
/**************************************/
#include <stdio.h>
/**************************************/
main ()
{ int i;
printf (“Assignment Operators\n\n”);
i = 10;
printf(“i = 10 : %d\n”,i);
i++;
printf (“i++ : %d\n”,i);
i += 5;
printf (“i += 5 : %d\n”,i);
i–;
printf (“i– : %d\n”,i);
i -= 2;
printf (“i -= 2 : %d\n”,i);
i *= 5;
printf (“i *= 5 :%d\n”,i);
/* Assignment */
/* i = i + 1 */
/* i = i + 5 */
/* i = i = 1 */
/* i = i – 2 */
/* i = i * 5 */

118
Chapter 15: Assignments, Expressions and Operators
i /= 2;
printf (“i /= 2 : %d\n”,i);
i %= 3;
printf (“i %%= 3 : %d\n”,i);
}
/* i = i / 2 */
/* i = i % 3 */
15.9 Output
Assignment Operators
i = 10 : 10
i++ : 11
i += 5 : 16
i– : 15
i -= 2 : 13
i *= 5 :65
i /= 2 : 32
i %= 3 : 2
15.10 The Cast Operator
The cast operator is an operator which forces a particular type mould or type
cast onto a value, hence the name. For instance a character type variable
could be forced to fit into an integer type box by the statement:
char ch;
int i;
i = (int) ch;
This operator was introduced earlier, See Chapter 9 [Variables], page 37.
It will always produce some value, whatever the conversion: however re-
motely improbable it might seem. For instance it is quite possible to convert
a character into a floating point number: the result will be a floating point
representation of its ASCII code!
15.11 Expressions and Types
There is a rule in C that all arithmetic and mathematical operations must
be carried out with long variables: that is, the types
double
long float
int
long int

Comparisons and Logic
119
If the programmer tries to use other types like short or float in a math-
ematical expression they will be cast into long types automatically by the
compiler. This can cause confusion because the compiler will spot an error
in the following statement:
short i, j = 2;
i = j * 2 + 1;
A compiler will claim that there is a type mismatch between i and the
expression on the right hand side of the assignment. The compiler is perfectly
correct of course, even though it appears to be wrong. The subtlety is that
arithmetic cannot be done in short type variables, so that the expression is
automatically converted into long type or int type. So the right hand side
is int type and the left hand side is short type: hence there is indeed a type
mismatch. The programmer can get around this by using the cast operator
to write:
short i, j = 2;
i = (short) j * 2 + 1;
A similar thing would happen with float:
float x, y = 2.3;
x = y * 2.5;
would also be incorrect for the same reasons as above.
Comparisons and Logic
15.12 Comparisons and Logic
Six operators in C are for making logical comparisons. The relevance of
these operators will quickly become clear in the next chapter, which is about
decisions and comparisons. The six operators which compare values are:
==
!=
>
<
>=
is equal to
is not equal to
is greater than
is less than
is greater than or equal to
<=is less than or equal to
These operators belong to the second group according to the scheme above
but they do actually result in values so that they could be thought of as being

120
Chapter 15: Assignments, Expressions and Operators
a part of the first group of operators too. The values which they produce are
called true and false. As words, “true” and “false” are not defined normally
in C, but it is easy to define them as macros and they may well be defined
in a library file:
#define TRUE 1
#define FALSE 0
Falsity is assumed to have the value zero in C and truth is represented by any
non-zero value. These comparison operators are used for making decisions,
but they are themselves operators and expressions can be built up with them.
1 == 1
has the value “true” (which could be anything except zero). The statement:
int i;
i = (1 == 2);
would be false, so i would be false. In other words, i would be zero.
Comparisons are often made in pairs or even in groups and linked together
with words like OR and AND. For instance, some test might want to find
out whether:
(A is greater than B) AND (A is greater than C)
C does not have words for these operations but gives symbols instead. The
logical operators, as they are called, are as follows:
&&
||
logical AND
logical OR inclusive
!logical NOT
The statement which was written in words above could be translated as:
(A > B) && (A > C)
The statement:
(A is greater than B) AND (A is not greater than C)
translates to:
(A > B) && !(A > C)
Shakespeare might have been disappointed to learn that, whatever the value
of a variable tobe the result of

Summary of Operators and Precedence
121
thequestion = tobe || !tobe
must always be true. The NOT operator always creates the logical opposite:
!true is false and !false is true. On or the other of these must be true.
thequestion is therefore always true. Fortunately this is not a matter of
life or death!
15.13 Summary of Operators and Precedence
The highest priority operators are listed first.
Operator
()
[]
++

(type)
*
&

~
!
*
/
%
+

>>
<<
>
>=
<=
<
==
!=
&
^
|
&&
||
=
Operation
parentheses
square brackets
increment
decrement
cast operator
the contents of
the address of
unary minus
one’s complement
logical NOT
multiply
divide
remainder (MOD)
add
subtract
shift right
shift left
is greater than
greater than or equal to
less than or equal to
less than
is equal to
is not equal to
bitwise
bitwise
bitwise
logical
logical
assign
AND
exclusive OR
inclusive OR
AND
OR
Evaluated.
left to right
left to right
right
right
right
right
right
right
right
right
to
to
to
to
to
to
to
to
left
left
left
left
left
left
left
left
left to right
left to right
left to right
left to right
left to right
left to right
left to right
left
left
left
left
to
to
to
to
right
right
right
right
left to right
left to right
left
left
left
left
left
to
to
to
to
to
right
right
right
right
right
right to left

122
+=
-=
*=
/=
%=
>>=
<<=
&=
^=
|=
Chapter 15: Assignments, Expressions and Operators
add assign
subtract assign
multiply assign
divide assign
remainder assign
right shift assign
left shift assign
AND assign
exclusive OR assign
inclusive OR assign
right
right
right
right
right
right
right
right
right
right
to
to
to
to
to
to
to
to
to
to
left
left
left
left
left
left
left
left
left
left
15.14 Questions
1. What is an operand?
2. Write a statement which prints out the remainder of 5 divided by 2.
3. Write a short statement which assigns the remainder of 5 divided by 2
to a variable called “rem”.
4. Write a statement which subtracts -5 from 10.
5. Write in C: if 1 is not equal to 23, print out “Thank goodness for
mathematics!”

Decisions
123
16 Decisions
Testing and Branching. Making conditions.
Suppose that a fictional traveller, some character in a book like this one,
came to the end of a straight, unfinished road and waited there for the author
to decide where the road would lead. The author might decide a number of
things about this road and its traveller:
• The road will carry on in a straight line. If the traveller is thirsty he
will stop for a drink before continuing.
• The road will fork and the traveller will have to decide whether to take
the left branch or the right branch.
• The road might have a crossroads or a meeting point where many roads
come together. Again the traveller has to decide which way to go.
We are often faced with this dilemma: a situation in which a decision has to
be made. Up to now the simple example programs in this book have not had
any choice about the way in which they progressed. They have all followed
narrow paths without any choice about which way they were going. This is
a very limited way of expressing ideas though: the ability to make decisions
and to choose different options is very useful in programming. For instance,
one might want to implement the following ideas in different programs:
• If the user hits the jackpot, write some message to say so. “You’ve won
the game!”
• If a bank balance is positive then print C for credit otherwise print D
for debit.
• If the user has typed in one of five things then do something special for
each special case, otherwise do something else.
These choices are actually just the same choices that the traveller had to
make on his undecided path, thinly disguised. In the first case there is a
simple choice: a do of don’t choice. The second case gives two choices: do
thing 1 or thing 2. The final choice has several possibilities.
C offers four ways of making decisions like the ones above. They are
listed here below. The method which is numbered 2b was encountered in
connection with the C preprocessor; its purpose is very similar to 2a.
1:
if (something_is_true)
{
/* do something */
}
if (something_is_true)
{
/* do one thing */
2a:

124
}
else
{
/* do something else */
}
2b:
? (something_is_true) :
/* do one thing */
:
/* do something else */
Chapter 16: Decisions
3:
switch (choice)
{
case first_possibility : /* do something */
case second_possibility : /* do something */
….
}
16.1 if
The first form of the if statement is an all or nothing choice. if some
condition is satisfied, do what is in the braces, otherwise just skip what is
in the braces. Formally, this is written:
if (condition) statement;
or
if (condition)
{
compound statement
}

if
125

126
Chapter 16: Decisions
Notice that, as well as a single statement, a whole block of statements can
be written under the if statement. In fact, there is an unwritten rule of thumb
in C that wherever a single statement will do, a compound statement will do
instead. A compound statement is a block of single statements enclosed by
curly braces.
A condition is usually some kind of comparison, like the ones discussed
in the previous chapter. It must have a value which is either true or false (1
or 0) and it must be enclosed by the parentheses ( and ). If the condition
has the value ‘true’ then the statement or compound statement following
the condition will be carried out, otherwise it will be ignored. Some of the
following examples help to show this:
int i;
printf (“Type in an integer”);
scanf (“%ld”,&i);
if (i == 0)
{
printf (“The number was zero”);
}
if (i > 0)
{
printf (“The number was positive”);
}
if (i < 0)
{
printf (“The number was negative”);
}
The same code could be written more briefly, but perhaps less consistently
in the following way:
int i;
printf (“Type in an integer”);
scanf (“%ld”,&i);
if (i == 0) printf (“The number was zero”);
if (i > 0) printf (“The number was positive”);
if (i < 0) printf (“The number was negative”);
The preference in this book is to include the block braces, even when they
are not strictly required. This does no harm. It is no more or less efficient,
but very often you will find that some extra statements have to go into those
braces, so it is as well to include them from the start. It also has the appeal

Example Listings
127
that it makes if statements look the same as all other block statements and
it makes them stand out clearly in the program text. This rule of thumb is
only dropped in very simple examples like:
if (i == 0) i++;
The if statement alone allows only a very limited kind of decision: it makes
do or don’t decisions; it could not decide for the traveller whether to take
the left fork or the right fork of his road, for instance, it could only tell him
whether to get up and go at all. To do much more for programs it needs
to be extended. This is the purpose of the else statement, described after
some example listings..
16.2 Example Listings
/*****************************************/
/**/
/* If… #1*/
/**/
/*****************************************/
#include <stdio.h>
#define TRUE
#define FALSE
1
0
/******************************************/
main ()
{ int i;
if (TRUE)
{
printf (“This is always printed”);
}
if (FALSE)
{
printf (“This is never printed”);
}
}
/*******************************************/
/**/
/* If demo #2*/
/**/
/*******************************************/

128
/* On board car computer. Works out the */
/* number of kilometers to the litre*/
/* that the car is doing at present*/
Chapter 16: Decisions
#include <stdio.h>
/*******************************************/
/* Level 0*/
/*******************************************/
main ()
{ double fuel,distance;
FindValues (&fuel,&distance);
Report (fuel,distance);
}
/********************************************/
/* Level 1*/
/********************************************/
FindValues (fuel,distance)
/* from car */
/* These values would be changing in */
/* a real car, independently of the */
/* program.*/
double *fuel,*distance;
{
/* how much fuel used since last check on values */
printf (“Enter fuel used”);
scanf (“%lf”,fuel);
/* distance travelled since last check on values */
printf (“Enter distance travelled”);
scanf (“%lf”,distance);
}
/**********************************************/
Report (fuel,distance)
double fuel,distance;
{ double kpl;
kpl = distance/fuel;
/* on dashboard */

if … else
129
printf (“fuel consumption: %2.1lf”,kpl);
printf (” kilometers per litre\n”);
if (kpl <= 1)
{
printf (“Predict fuel leak or car”);
printf (” needs a service\n”);
}
if (distance > 500)
{
printf (“Remember to check tyres\n”);
}
if (fuel > 30)/* Tank holds 40 l */
{
printf (“Fuel getting low: %s left\n”,40-fuel);
}
}
16.3 if … else
The ‘if .. else’ statement has the form:
if (condition ) statement1 ; else statement2 ;
This is most often written in the compound statement form:
if (condition )
{
statements
}
else
{
statements
}
The ‘if..else’ statement is a two way branch: it means do one thing or
the other. When it is executed, the condition is evaluated and if it has the
value ‘true’ (i.e. not zero) then statement1 is executed. If the condition is
‘false’ (or zero) then statement2 is executed. The ‘if..else’ construction
often saves an unnecessary test from having to be made. For instance:
int i;
scanf (“%ld”,i);
if (i > 0)
{

130
printf (“That number was positive!”);
}
else
{
printf (“That number was negative or zero!”);
}
Chapter 16: Decisions
It is not necessary to test whether i was negative in the second block be-
cause it was implied by the ‘if..else’ structure. That is, that block would
not have been executed unless i were NOT greater than zero. The weary
traveller above might make a decision such as:
if (rightleg > leftleg)
{
take_left_branch();
}
else
{
take_right_branch();
}
16.4 Nested ifs and logic
Consider the following statements which decide upon the value of some vari-
able i. Their purposes are exactly the same.
if ((i > 2) && (i < 4))
{
printf (“i is three”);
}
or:
if (i > 2)
{
if (i < 4)
{
printf (“i is three”);
}
}

Nested ifs and logic
131
Both of these test i for the same information, but they do it in different
ways. The first method might been born out of the following sequence of
thought:
If i is greater than 2 and i is less than four, both at the same time,
then i has to be 3.
The second method is more complicated. Think carefully. It says:

132
Chapter 16: Decisions
If i is greater than 2, do what is in the curly braces. Inside these curly
braces i is always greater than 2 because otherwise the program would
never have arrived inside them. Now, if i is also less than 4, then do
what is inside the new curly braces. Inside these curly braces i is always
less than 4. But wait! The whole of the second test is held inside the
“i is greater than 2” braces, which is a sealed capsule: nothing else
can get in, so, if the program gets into the “i is less than 4” braces as
well, then both facts must be true at the same time. There is only one
integer which is bigger than 2 and less than 4 at the same time: it is 3.
So i is 3.
The aim of this demonstration is to show that there are two ways of
making multiple decisions in C. Using the logical comparison operators &&,
|| (AND,OR) and so on.. several multiple tests can be made. In many
cases though it is too difficult to think in terms of these operators and the
sealed capsule idea begins to look attractive. This is another advantage of
using the curly braces: it helps the programmer to see that if statements
and ‘if..else’ statements are made up of sealed capsule parts. Once inside
a sealed capsule
if (i > 2)
{
/* i is greater than 2 in here! */
}
else
{
/* i is not greater than 2 here! */
}
the programmer can rest assured that nothing illegal can get in. The block
braces are like regions of grace: they cannot be penetrated by anything which
does not satisfy the right conditions. This is an enourmous weight off the
mind! The programmer can sit back and think: I have accepted that i is
greater than 2 inside these braces, so I can stop worrying about that now.
This is how programmers learn to think in a structured way. They learn
to be satisfied that certain things have already been proven and thus save
themselves from the onset of madness as the ideas become too complex to
think of all in one go.
16.5 Example Listing
/***********************************************/
/**/
/* If demo #3*/
/**/
/***********************************************/
#include <stdio.h>

Stringing together if..else
/***********************************************/
main ()
{ int persnum,usernum,balance;
persnum = 7462;
balance = -12;
printf (“The Plastic Bank Corporation\n”);
printf (“Please enter your personal number :”);
usernum = getnumber();
if (usernum == 7462)
{
printf (“\nThe current state of your account\n”);
printf (“is %d\n”,balance);
if (balance < 0)
{
printf (“The account is overdrawn!\n”);
}
}
else
{
printf (“This is not your account\n”);
}
printf (“Have a splendid day! Thank you.\n”);
}
/**************************************************/
getnumber ()
{ int num = 0;
scanf (“%d”,&num);
if ((num > 9999) || (num <= 0))
{
printf (“That is not a valid number\n”);
}
return (num);
}
/* get a number from the user */
133

134
Chapter 16: Decisions
16.6 Stringing together if..else
What is the difference between the following programs? They both interpret
some imaginary exam result in the same way. They both look identical when
compiled and run. Why then are they different?
/**************************************************/
/* Program 1*/
/**************************************************/
#include <stdio.h>
main ()
{ int result;
printf(“Type in exam result”);
scanf (“%d”,&result);
if (result < 10)
{
printf (“That is poor”);
}
if (result > 20)
{
printf (“You have passed.”);
}
if (result > 70)
{
printf (“You got an A!”);
}
}
/* end */
/**************************************************/
/* Program 2*/
/**************************************************/
#include <stdio.h>
main ()
{ int result;
printf(“Type in exam result”);
scanf (“%d”,&result);
if (result < 10)

switch: integers and characters
{
printf (“That is poor”);
}
else
{
if (result > 20)
{
printf (“You have passed.”);
}
else
{
if (result > 70)
{
printf (“You got an A!”);
}
}
}
}
135
The answer is that the second of these programs can be more efficient.
This because it uses the else form of the if statement which in turn means
that few things have to be calculated. Program one makes every single
test, because the program meets every if statement, one after the other.
The second program does not necessarily do this however. The nested if
statements make sure that the second two tests are only made if the first
one failed. Similarly the third test is only performed if the first two failed.
So the second program could end up doing a third of the work of the first
program, in the best possible case. Nesting decisions like this can be an
efficient way of controlling long lists of decisions like those above. Nested
loops make a program branch into lots of possible paths, but choosing one
path would preclude others.
16.7 switch: integers and characters
The switch construction is another way of making a program path branch
into lots of different limbs. It can be used as a different way of writing a
string of ‘if .. else’ statements, but it is more versatile than that and it
only works for integers and character type values. It works like a kind of
multi-way switch. (See the diagram.) The switch statement has the following
form:
switch (int or char expression )
{
case constant : statement ;
break;

/* optional */

136
}
Chapter 16: Decisions
It has an expression which is evaluated and a number of constant ‘cases’
which are to be chosen from, each of which is followed by a statement or
compound statement. An extra statement called break can also be incorpo-
rated into the block at any point. break is a reserved word.

Example Listing
The switch statement can be written more specifically for integers:
switch (integer value )
{
case 1:
137
statement1 ;
break;
statement2 ;
break;
/* optional line */
case 2:
/* optional line */
….
default: default statement
break;
}
/* optional line */
When a switch statement is encountered, the expression in the parentheses
is evaluated and the program checks to see whether the result of that expres-
sion matches any of the constants labelled with case. If a match is made
(for instance, if the expression is evaluated to 23 and there is a statement
beginning “case 23 : …”) execution will start just after that case statement
and will carry on until either the closing brace } is encountered or a break
statement is found. break is a handy way of jumping straight out of the
switch block. One of the cases is called default. Statements which follow
the default case are executed for all cases which are not specifically listed.
switch is a way of choosing some action from a number of known instances.
Look at the following example.
16.8 Example Listing
/************************************************/
/**/
/* switch .. case*/
/**/
/************************************************/
/* Morse code program. Enter a number and */
/* find out what it is in Morse code*/
#include <stdio.h>
#define CODE 0
/*************************************************/
main ()
{ short digit;

138
Chapter 16: Decisions
printf (“Enter any digit in the range 0..9”);
scanf (“%h”,&digit);
if ((digit < 0) || (digit > 9))
{
printf (“Number was not in range 0..9”);
return (CODE);
}
printf (“The Morse code of that digit is “);
Morse (digit);
}
/************************************************/
Morse (digit)
short digit;
{
switch (digit)
{
case 0 : printf
break;
case 1 : printf
break;
case 2 : printf
break;
case 3 : printf
break;
case 4 : printf
break;
case 5 : printf
break;
case 6 : printf
break;
case 7 : printf
break;
case 8 : printf
break;
case 9 : printf
}
}
/* print out Morse code */
(“—–“);
(“.—-“);
(“..—“);
(“…–“);
(“….-“);
(“…..”);
(“-….”);
(“–…”);
(“—..”);
(“—-.”);
The program selects one of the printf statements using a switch construction.
At every case in the switch, a break statement is used. This causes control
to jump straight out of the switch statement to its closing brace }. If break
were not included it would go right on executing the statements to the end,

Things to try
139
testing the cases in turn. break this gives a way of jumping out of a switch
quickly.
There might be cases where it is not necessary or not desirable to jump
out of the switch immediately. Think of a function yes() which gets a
character from the user and tests whether it was ’y’ or ’Y’.
yes ()
/* A sloppy but simple function */
{
switch (getchar())
{
case ’y’ :
case ’Y’ : return TRUE
default : return FALSE
}
}
If the character is either ’y’ or ’Y’ then the function meets the statement
return TRUE. If there had been a break statement after case ’y’ then control
would not have been able to reach case ’Y’ as well. The return statement
does more than break out of switch, it breaks out of the whole function, so in
this case break was not required. The default option ensures that whatever
else the character is, the function returns false.
16.9 Things to try
1. Write a program to get a lot of numbers from the user and print out
the maximum and minimum of those.
2. Try to make a counter which is reset to zero when it reaches 9999.
3. Try to write a program incorporating the statement if (yes()) {…}.

140
Chapter 16: Decisions

while
141
17 Loops
Controlling repetitive processes. Nesting loops
Decisions can also be used to make up loops. Loops free a program from
the straitjacket of doing things only once. They allow the programmer to
build a sequence of instructions which can be executed again and again, with
some condition deciding when they will stop. There are three kinds of loop
in C. They are called:
• while
• do … while
• for
These three loops offer a great amount of flexibility to programmers and can
be used in some surprising ways!
17.1 while
The simplest of the three loops is the while loop. In common language while
has a fairly obvious meaning: the while-loop has a condition:
while (condition )
{
statements ;
}
and the statements in the curly braces are executed while the condition has
the value “true” ( 1 ). There are dialects of English, however, in which

142
Chapter 17: Loops
“while” does not have its commonplace meaning, so it is worthwhile ex-
plaining the steps which take place in a while loop.
The first important thing about this loop is that has a conditional ex-
pression (something like (a > b) etc…) which is evaluated every time the
loop is executed by the computer. If the value of the expression is true, then
it will carry on with the instructions in the curly braces. If the expression
evaluates to false (or 0) then the instructions in the braces are ignored
and the entire while loop ends. The computer then moves onto the next
statement in the program.
The second thing to notice about this loop is that the conditional expres-
sion comes at the start of the loop: this means that the condition is tested
at the start of every ‘pass’, not at the end. The reason that this is important
is this: if the condition has the value false before the loop has been executed
even once, the statements inside the braces will not get executed at all – not
even once.

Example Listing
143
The best way to illustrate a loop is to give an example of its use. One
example was sneaked into an earlier chapter before its time, in order to write
the skipgarb() function which complemented scanf(). That was:
skipgarb ()
/* skip garbage corrupting scanf */
{
while (getchar() != ’\n’)
{
}
}
This is a slightly odd use of the while loop which is pure C, through and
through. It is one instance in which the programmer has to start thinking C
and not any other language. Something which is immediately obvious from
listing is that the while loop in skipgarb() is empty: it contains no state-
ments. This is quite valid: the loop will merely do nothing a certain number
of times… at least it would do nothing if it were not for the assignment in
the conditional expression! It could also be written:
skipgarb ()
/* skip garbage corrupting scanf */
{
while (getchar() != ’\n’)
{
}
}
The assignment inside the conditional expression makes this loop special.
What happens is the following. When the loop is encountered, the computer
attempts to evaluate the expression inside the parentheses. There, inside
the parentheses, it finds a function call to getchar(), so it calls getchar()
which fetches the next character from the input. getchar() then takes
on the value of the character which it fetched from the input file. Next the
computer finds the != “is not equal to” symbol and the newline character \n.
This means that there is a comparison to be made. The computer compares
the character fetched by getchar() with the newline character and if they
are ‘not equal’ the expression is true. If they are equal the expression is
false. Now, if the expression is true, the while statement will loop and start
again – and it will evaluate the expression on every pass of the loop to check
whether or not it is true. When the expression eventually becomes false the
loop will quit. The net result of this subtlety is that skipgarb() skips all
the input characters up to and including the next newline ‘\n’ character and
that usually means the rest of the input.
17.2 Example Listing
Another use of while is to write a better function called yes(). The idea
of this function was introduced in the previous section. It uses a while loop

144
Chapter 17: Loops
which is always true to repeat the process of getting a response from the user.
When the response is either yes or no it quits using the return function to
jump right out of the loop.
/***********************************************/
/**/
/* Give me your answer!*/
/**/
/***********************************************/
#include <stdio.h>
#define TRUE
#define FALSE
1
0
/*************************************************/
/* Level 0*/
/*************************************************/
main ()
{
printf (“Yes or no? (Y/N)\n”);
if (yes())
{
printf (“YES!”);
}
else
{
printf (“NO!”);
}
}
/*************************************************/
/* Level 1*/
/*************************************************/
yes ()
{ char getkey();
/* get response Y/N query */
while (true)
{
switch (getkey())
{
case ’y’ : case ’Y’ : return (TRUE);
case ’n’ : case ’N’ : return (FALSE);
}
}
}

Example Listing
145
/*************************************************/
/* Toolkit*/
/*************************************************/
char getkey ()
{ char ch;
ch = getchar();
skipgarb();
}
/**************************************************/
skipgarb ()
{
while (getchar() != ’\n’)
{
}
}
/* end */
/* get a character + RETURN */
17.3 Example Listing
This example listing prompts the user to type in a line of text and it counts
all the spaces in that line. It quits when there is no more input left and
printf out the number of spaces.
/***********************************************/
/**/
/* while loop*/
/**/
/***********************************************/
/* count all the spaces in an line of input */
#include <stdio.h>
main ()
{ char ch;
short count = 0;
printf (“Type in a line of text\n”);
while ((ch = getchar()) != ’\n’)

146
{
if (ch == ’ ’)
{
count++;
}
}
printf (“Number of space = %d\n”,count);
}
Chapter 17: Loops
17.4 do..while
The do..while loop resembles most closely the repeat..until loops of Pascal
and BASIC except that it is the ‘logical opposite’. The do loop has the form:
do
{
statements ;
}
while (condition )
Notice that the condition is at the end of this loop. This means that a
do..while loop will always be executed at least once, before the test is
made to determine whether it should continue. This is the only difference
between while and do..while.
A do..while loop is like the “repeat .. until” of other languages in the
following sense: if the condition is NOTed using the ‘!’ operator, then the
two are identical.
repeat
==
until(condition)
do
while (!condition)

Example Listing
147
This fact might be useful for programmers who have not yet learned to think
in C!
17.5 Example Listing
Here is an example of the use of a do..while loop. This program gets a
line of input from the user and checks whether it contains a string marked
out with “” quote marks. If a string is found, the program prints out the
contents of the string only. A typical input line might be:
Onceupon a time “Here we go round the…”what a terrible..
The output would then be:
Here we go round the…
If the string has only one quote mark then the error message ‘string was not
closed before end of line’ will be printed.
/**********************************************/
/**/

148
/* do .. while demo*/
/**/
/**********************************************/
/* print a string enclosed by quotes ” ” */
/* gets input from stdin i.e. keyboard*/
/* skips anything outside the quotes*/
Chapter 17: Loops
#include <stdio.h>
/*************************************************/
/* Level 0*/
/*************************************************/
main ()
{ char ch,skipstring();
do
{
if ((ch = getchar()) == ’”’)
{
printf (“The string was:\n”);
ch = skipstring();
}
}
while (ch != ’\n’)
{
}
}
/*************************************************/
/* Level 1*/
/*************************************************/
char skipstring () /* skip a string “…” */
{ char ch;
do
{
ch = getchar();
putchar(ch);
if (ch == ’\n’)
{
printf (“\nString was not closed “);
printf (“before end of line\n”);
break;
}

for
}
while (ch != ’”’)
{
}
return (ch);
}
149
17.6 for
The most interesting and also the most difficult of all the loops is the for
loop. The name for is a hangover from earlier days and other languages. It
is not altogether appropriate for C’s version of for. The name comes from
the typical description of a classic for loop:
For all values of variable from value1 to value2 in steps of value3,
repeat the following sequence of commands….
In BASIC this looks like:
FOR variable = value1 TO value2 STEP value3
NEXT variable
The C for loop is much more versatile than its BASIC counterpart; it is
actually based upon the while construction. A for loop normally has the
characteristic feature of controlling one particular variable, called the control
variable. That variable is somehow associated with the loop. For example
it might be a variable which is used to count “for values from 0 to 10” or
whatever. The form of the for loop is:
for (statement1 ; condition ; statement2 )
{
}
For normal usage, these expressions have the following significance.
statement1
This is some kind of expression which initializes the control vari-
able. This statement is only carried out once before the start of
the loop. e.g. i = 0;
condition
This is a condition which behaves like the while loop. The con-
dition is evaluated at the beginning of every loop and the loop
is only carried out while this expression is true. e.g. i < 20;
This is some kind of expression for altering the value of the con-
trol variable. In languages such as Pascal this always means
statement2

150
Chapter 17: Loops
adding or subtracting 1 from the variable. In C it can be abso-
lutely anything. e.g. i++ or i *= 20 or i /= 2.3 …
Compare a C for loop to the BASIC for loop. Here is an example in which
the loop counts from 0 to 10 in steps of 0.5:
FOR X = 0 TO 10 STEP 0.5
NEXT X
for (x = 0; x <= 10; x += 0.5)
{
}
The C translation looks peculiar in comparison because it works on a subtly
different principle. It does not contain information about when it will stop,
as the BASIC one does, instead it contains information about when it should
be looping. The result is that a C for loop often has the <= symbol in it.
The for loop has plenty of uses. It could be used to find the sum of the first
n natural numbers very simply:
sum = 0;
for (i = 0; i <= n; i++)
{
sum += i;
}
It generally finds itself useful in applications where a single variable has
to be controlled in a well determined way.
g4
17.7 Example Listing
This example program prints out all the primes numbers between 1 and the
macro value maxint. Prime numbers are numbers which cannot be divided
by any number except 1 without leaving a remainder.
/************************************************/
/**/
/* Prime Number Generator #1*/
/**/
/************************************************/
/*
/*
/*
/*
Check for prime number by raw number
crunching. Try dividing all numbers
up to half the size of a given i, if
remainder == 0 then not prime!
*/
*/
*/
*/

The flexible for loop
#include <stdio.h>
#define MAXINT
#define TRUE
#define FALSE
500
1
0
151
/*************************************************/
/* Level 0*/
/*************************************************/
main ()
{ int i;
for (i = 2; i <= MAXINT; i++)
{
if (prime(i))
{
printf (“%5d”,i);
}
}
}
/*************************************************/
/* Level 1*/
/*************************************************/
prime (i)
int i;
{ int j;
for (j = 2; j <= i/2; j++)
{
if (i % j == 0)
{
return FALSE;
}
}
return TRUE;
}
/* check for a prime number */
17.8 The flexible for loop
The word ‘statement’ was chosen carefully, above, to describe what goes into
a for loop. Look at the loop again:
for (statement1 ; condition ; statement2 )
{

152
}
Chapter 17: Loops
Statement really means what it says. C will accept any statement in the
place of those above, including the empty statement. The while loop could
be written as a for loop!
for (; condition; )
{
}
/* while ?? */
Here there are two empty statements, which are just wasted. This flexibility
can be put to better uses though. Consider the following loop:
for (x = 2; x <= 1000; x = x * x)
{
….
}
This loop begins from 2 and each time the statements in the braces are
executed x squares itself! Another odd looking loop is the following one:
for (ch = ’*’; ch != ’\n’; ch = getchar())
{
}
This could be used to make yet another different kind of skipgarb() func-
tion. The loop starts off by initializing ch with a star character. It checks
that ch != ’\n’ (which it isn’t, first time around) and proceeds with the
loop. On each new pass, ch is reassigned by calling the function getchar().
It is also possible to combine several incremental commands in a loop:
for (i = 0, j=10; i < j; i++, j–)
{
printf(“i = %d, j= %d\n”,i,j);
}
Statement2 can be any statement at all which the programmer would
like to be executed on every pass of the loop. Why not put that statement
in the curly braces? In most cases that would be the best thing to do, but
in special instances it might keep a program tidier or more readable to put
it in a for loop instead. There is no good rule for when to do this, except to
say: make you code as clear as possible.
It is not only the statements which are flexible. An unnerving feature
of the for construction (according to some programmers) is that even the
conditional expression in the for loop can be altered by the program from
within the loop itself if is written as a variable.
int i, number = 20;

Quitting Loops and Hurrying Them Up!
for (i = 0; i <= number; i++)
{
if (i == 9)
{
number = 30;
}
}
153
This is so nerve shattering that many languages forbid it outright. To be
sure, is not often a very good idea to use this facility, but in the right hands,
it is a powerful one to have around.
17.9 Quitting Loops and Hurrying Them Up!
C provides a simple way of jumping out of any of the three loops above at
any stage, whether it has finished or not. The statement which performs
this action is the same statement which was used to jump out of switch
statements in last section.
break;
If this statement is encountered a loop will quit where it stands. For instance,
an expensive way of assigning i to be 12 would be:
for (i = 1; i <= 20; i++)
{
if (i == 12)
{
break;
}
}
Still another way of making skipgarb() would be to perform the follow-
ing loop:
while (TRUE)
{
ch = getchar();
if (ch == ’\n’)
{
break;
}
}
Of course, another way to do this would be to use the return() statement,
which jumps right out of a whole function. break only jumps out of the
loop, so it is less drastic.
As well as wanting to quit a loop, a programmer might want to hurry
a loop on to the next pass: perhaps to avoid executing a lot of irrelevant
statements, for instance. C gives a statement for this too, called:

154
Chapter 17: Loops
continue;
When a continue statement is encountered, a loop will stop whatever it is
doing and will go straight to the start of the next loop pass. This might be
useful to avoid dividing by zero in a program:
for (i = -10; i <= 10; i++)
{
if (i == 0)
{
continue;
}
printf (“%d”, 20/i);
}
17.10 Nested Loops
Like decisions, loops will also nest: that is, loops can be placed inside other
loops. Although this feature will work with any loop at all, it is most com-
monly used with the for loop, because this is easiest to control. The idea of
nested loops is important for multi-dimensional arrays which are examined
in the next section. A for loop controls the number of times that a par-
ticular set of statements will be carried out. Another outer loop could be
used to control the number of times that a whole loop is carried out. To see
the benefit of nesting loops, the example below shows how a square could
be printed out using two printf statements and two loops.
/*****************************************/
/**/
/* A “Square”*/
/**/
/*****************************************/
#include <stdio.h>
#define SIZE
10
/*****************************************/
main ()
{ int i,j;
for (i = 1; i <= SIZE; i++)
{
for (j = 1; j <= SIZE; j++)
{
printf(“*”);

Questions
}
printf (“\n”);
}
}
155
The output of this program is a “kind of” square:
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
17.11 Questions
1.
2.
3.
4.
5.
How many kinds of loop does C offer, and what are they?
When is the condition tested in each of the loops?
Which of the loops is always executed once?
Write a program which copies all input to output line by line.
Write a program to get 10 numbers from the user and add them together.

156
Chapter 17: Loops

Why use arrays?
157
18 Arrays
Rows and tables of storage.
Arrays are a convenient way of grouping a lot of variables under a single
variable name. Arrays are like pigeon holes or chessboards, with each com-
partment or square acting as a storage place; they can be one dimensional,
two dimensional or more dimensional! An array is defined using square
brackets []. For example: an array of three integers called “triplet” would
be declared like this:
int triplet[3];
Notice that there is no space between the square bracket [ and the name of
the array. This statement would cause space for three integers type variables
to be created in memory next to each other as in the diagram below.
int triplet:
————————————
||||
————————————
The number in the square brackets of the declaration is referred to as the
‘index’ (plural: indicies) or ‘subscript’ of the array and it must be an integer
number between 0 and (in this case) 2. The three integers are called elements
of the array and they are referred to in a program by writing:
triplet[0]
triplet[1]
triplet[2]
Note that the indicies start at zero and run up to one less than the number
which is placed in the declaration (which is called the dimension of the array.)
The reason for this will become clear later. Also notice that every element in
an array is of the same type as every other. It is not (at this stage) possible
to have arrays which contain many different data types. When arrays are
declared inside a function, storage is allocated for them, but that storage
space is not initialized: that is, the memory space contains garbage (random
values). It is usually necessary, therefore, to initialize the array before the
program truly begins, to prepare it for use. This usually means that all the
elements in the array will be set to zero.
18.1 Why use arrays?
Arrays are most useful when they have a large number of elements: that is,
in cases where it would be completely impractical to have a different name

158
Chapter 18: Arrays
for every storage space in the memory. It is then highly beneficial to move
over to arrays for storing information for two reasons:
• The storage spaces in arrays have indicies. These numbers can often be
related to variables in a problem and so there is a logical connection to
be made between an array an a program.
• In C, arrays can be initialized very easily indeed. It is far easier to
initialize an array than it is to initialize twenty or so variables.
The first of these reasons is probably the most important one, as far as
C is concerned, since information can be stored in other ways with equally
simple initialization facilities in C. One example of the use of an array might
be in taking a census of the types of car passing on a road. By defining
macros for the names of the different cars, they could easily be linked to the
elements in an array.
Type
car
auto
bil
Array Element
0
1
2
The array could then be used to store the number of cars of a given type
which had driven past. e.g.
/***********************************************/
/**/
/* Census*/
/**/
/***********************************************/
#include <stdio.h>
#define
#define
#define
#define
NOTFINISHED
CAR
AUTO
BIL
1
0
1
2
/************************************************/
main ()
{ int type[3];
int index;
for (index = 0; index < 3; index++)
{
type[index] = 0;
}
while (NOTFINISHED)
{

Limits and The Dimension of an array
printf (“Enter type number 0,1, or 2”);
scanf (“%d”, &index);
skipgarb();
type[index] += 1;
}
}
/* See text below */
159
This program, first of all, initializes the elements of the array to be zero.
It then enters a loop which repeatedly fetches a number from the user and
increases the value stored in the array element, labelled by that number, by
1. The effect is to count the cars as they go past. This program is actually
not a very good program for two reasons in particular:
• Firstly, it does not check that the number which the user typed is ac-
tually one of the elements of the array. (See the section below about
this.)
• The loop goes on for ever and the program never gives up the informa-
tion which is stores. In short: it is not very useful.
Another example, which comes readily to mind, would be the use of a two
dimensional array for storing the positions of chess pieces in a chess game.
Two dimensional arrays have a chessboard-like structure already and they
require two numbers (two indicies) to pinpoint a particular storage cell. This
is just like the numbers on chess board, so there is an immediate and logical
connection between an array and the problem of keeping track of the pieces
on a chess board. Arrays play an important role in the handling of string
variables. Strings are important enough to have a section of their own, See
undefined [Strings], page undefined .
18.2 Limits and The Dimension of an array
C does not do much hand holding. It is invariably up to the programmer to
make sure that programs are free from errors. This is especially true with
arrays. C does not complain if you try to write to elements of an array which
do not exist! For example:
char array[5];
is an array with 5 elements. If you wrote:
array[7] = ’*’;
C would happily try to write the character ‘*’ at the location which
would have corresponded to the seventh element, had it been declared that
way. Unfortunately this would probably be memory taken up by some other
variable or perhaps even by the operating system. The result would be
either:

160
Chapter 18: Arrays
• The value in the incorrect memory location would be corrupted with
unpredictable consequences.
• The value would corrupt the memory and crash the program completely!
On Unix systems this leads to a memory segmentation fault.
The second of these tends to be the result on operating systems with proper
memory protection. Writing over the bounds of an array is a common source
of error. Remember that the array limits run from zero to the size of the
array minus one.
18.3 Arrays and for loops
Arrays have a natural partner in programs: the for loop. The for loop
provides a simple way of counting through the numbers of an index in a
controlled way. Consider a one dimensional array called array. A for loop
can be used to initialize the array, so that all its elements contain zero:
#define SIZE
main ()
{ int i, array[SIZE];
for (i = 0; i < SIZE; i++)
{
array[i] = 0;
}
}
10;
It could equally well be used to fill the array with different values. Consider:
#define SIZE
main ()
{ int i, array[size];
for (i = 0; i < size; i++)
{
array[i] = i;
}
}
10;
This fills each successive space with the number of its index:
index
0
1
2
3
4
5
6
7
8
9
element
contents
—————————————
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
—————————————

Example Listing
161
The for loop can be used to work on an array sequentially at any time during
a program, not only when it is being initialized. The example listing below
shows an example of how this might work for a one dimensional array, called
an Eratosthenes sieve. This sieve is an array which is used for weeding out
prime numbers, that is: numbers which cannot be divided by any number
except 1 without leaving a remainder or a fraction. It works by filling an
array with numbers from 0 to some maximum value in the same way that was
shown above and then by going through the numbers in turn and deleting
(setting equal to zero) every multiple of every number from the array. This
eliminates all the numbers which could be divided by something exactly and
leaves only the prime numbers at the end. Try to follow the listing below.
18.4 Example Listing
/******************************************************/
/**/
/* Prime Number Sieve*/
/**/
/******************************************************/
#include <stdio.h>
#define SIZE
#define DELETED
5000
0
/*******************************************************/
/* Level 0*/
/*******************************************************/
main ()
{ short sieve[SIZE];
printf (“Eratosthenes Sieve \n\n”);
FillSeive(sieve);
SortPrimes(sieve);
PrintPrimes(sieve);
}
/*********************************************************/
/* Level 1*/
/*********************************************************/
FillSeive (sieve)
short sieve[SIZE];
/* Fill with integers */

162
{ short i;
for (i = 2; i < SIZE; i++)
{
sieve[i] = i;
}
}
Chapter 18: Arrays
/**********************************************************/
SortPrimes (sieve)
short sieve[SIZE];
{ short i;
for (i = 2; i < SIZE; i++)
{
if (sieve[i] == DELETED)
{
continue;
}
DeleteMultiplesOf(i,sieve);
}
}
/***********************************************************/
PrintPrimes (sieve)
short sieve[SIZE];
{ short i;
for (i = 2; i < SIZE; i++)
{
if (sieve[i] == DELETED)
{
continue;
}
else
{
printf (“%5d”,sieve[i]);
}
}
}
/***********************************************************/
/* Level 2*/
/***********************************************************/
DeleteMultiplesOf (i,sieve)
/* Delete.. of an integer */
/* Print out array */
/* Delete non primes */

Arrays Of More Than One Dimension
163
short i,sieve[SIZE];
{ short j, mult = 2;
for (j = i*2; j < SIZE; j = i * (mult++))
{
sieve[j] = DELETED;
}
}
/* end */
18.5 Arrays Of More Than One Dimension
There is no limit, in principle, to the number of indicies which an array can
have. (Though there is a limit to the amount of memory available for their
storage.) An array of two dimensions could be declared as follows:
float numbers[SIZE][SIZE];
SIZE is some constant. (The sizes of the two dimensions do not have to be the
same.) This is called a two dimensional array because it has two indicies, or
two labels in square brackets. It has (SIZE * SIZE) or size-squared elements
in it, which form an imaginary grid, like a chess board, in which every square
is a variable or storage area.
————————————
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | … (up to SIZE)
————————————
| 1 |||||||||
————————————
| 2 |||||||||
————————————
| 3 |||||||||
————————————
| 4 |||||||||
————————————
| 5 |||||||||
————————————
| 6 |||||||||
————————————
| 7 |||||||||
————————————
.
.
(up to SIZE)

164
Chapter 18: Arrays
Every element in this grid needs two indicies to pin-point it. The elements
are accessed by giving the coordinates of the element in the grid. For instance
to set the element 2,3 to the value 12, one would write:
array[2][3] = 12;
The usual terminology for the two indicies is that the first gives the row
number in the grid and that the second gives the column number in the
grid. (Rows go along, columns hold up the ceiling.) An array cannot be
stored in the memory as a grid: computer memory is a one dimensional
thing. Arrays are therefore stored in rows. The following array:
————
| 1 | 2 | 3 |
————
| 4 | 5 | 6 |
————
| 7 | 8 | 9 |
————
would be stored:
————————————
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
————————————
* ROW # 1 * ROW # 2 * ROW #3*
Another way of saying that arrays are stored row-wise is to say that the sec-
ond index varies fastest, because a two-dimensional array is always thought
of as…
array[row][column]
so for every row stored, there will be lots of columns inside that row. That
means the column index goes from 0..SIZE inside every row, so it is changing
faster as the line of storage is followed.
A three dimensional array, like a cube or a cuboid, could also be defined
in the same kind of way:
double cube[SIZE][SIZE][SIZE];
or with different limits on each dimension:
short
notcubic[2][6][8];
Three dimensional arrays are stored according to the same pattern as two
dimensional arrays. They are kept in computer memory as a linear sequence
of variable stores and the last index is always the one which varies fastest.

Example Listing
165
18.6 Arrays and Nested Loops
Arrays of more than one dimension are usually handled by nested for loops.
A two dimensional array might be initialized in the following way:
main ()
{ int i,j;
float array[SIZE1][SIZE2];
for (i = 0; i < SIZE1; i++)
{
for (j = 0; j < SIZE2; j++)
{
array[i][j] = 0;
}
}
}
In three dimensions, three nested loops would be needed:
main ()
{ int i,j,k;
float array[SIZE1][SIZE2][SIZE3];
for (i = 0; i < SIZE1; i++)
{
for (j = 0; j < SIZE2; j++)
{
for (k = 0; k < SIZE3; k++)
{
array[i][j][k] = 0;
}
}
}
}
An example program helps to show how this happens in practice. The
example below demonstrates the so-called “Game of Life”. The aim is to
mimic something like cell reproduction by applying some rigid rules to a
pattern of dots ‘.’ and stars ‘*’. A dot is a place where there is no life
(as we know it!) and a star is a place in which there is a living thing. The
rules will be clear from the listing. Things to notice are the way the program
traverses the arrays and the way in which it checks that it is not overstepping
the boundaries of the arrays.
18.7 Example Listing
/*********************************************************/

166
Chapter 18: Arrays
/**/
/* Game of Life*/
/**/
/*********************************************************/
/*
/*
/*
/*
/*
/*
/*
Based upon an article from Scientific American
in 1970. Simulates the reproduction of cells
which depend on one another. The rules are
that cells will only survive if they have a
certain number of neighbours to support them
but not too many, or there won’t be enough
food!
*/
*/
*/
*/
*/
*/
*/
#include <stdio.h>
#define
#define
#define
#define
SIZE
MAXNUM
INBOUNDS
NORESPONSE
20
15
(a>=0)&&(a<SIZE)&&(b>=0)&&(b<SIZE)
1
/*********************************************************/
/* Level 0*/
/*********************************************************/
main ()
{ int count[SIZE][SIZE];
char array[SIZE][SIZE];
int generation = 0;
printf (“Game of Life\n\n\n”);
InitializeArray(array);
while (NORESPONSE)
{
CountNeighbours(array,count);
BuildNextGeneration(array,count);
UpdateDisplay(array,++generation);
printf (“\n\nQ for quit. RETURN to continue.\n”);
if(quit()) break;
}
}
/**********************************************************/
/* Level 1*/
/**********************************************************/
InitializeArray (array)
char array[SIZE][SIZE];
/* Get starting conditions */

Example Listing
{ int i,j;
char ch;
printf
printf
printf
printf
(“\nEnter starting setup. Type ’.’ for empty”);
(“\nand any other character for occupied.\n”);
(“RETURN after each line.\n\n”);
(“Array size guide:\n\n”);
167
for (i=0; i++ < SIZE; printf(“%c”,’^’));
printf (“\n\n”);
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
scanf (“%c”,&ch);
if (ch == ’.’)
{
array[i][j] = ’.’;
}
else
{
array[i][j] = ’*’;
}
}
skipgarb();
}
printf (“\n\nInput is complete. Press RETURN.”);
skipgarb();
}
/********************************************************/
CountNeighbours (array,count) /* count all neighbours */
char array[SIZE][SIZE];
int count[SIZE][SIZE];
{ int i,j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
count[i][j] = numalive(array,i,j);
}
}
}
/*******************************************************/

168
BuildNextGeneration (array,count)
/* A cell will survive if it has two or three */
/* neighbours. New life will be born to a dead */
/* cell if there are exactly three neighbours */
char array[SIZE][SIZE];
int count[SIZE][SIZE];
{ int i,j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
if (array[i][j] == ’*’)
{
switch (count[i][j])
{
case 2 :
case 3 : continue;
default: array[i][j] = ’.’;
break;
}
}
else
{
switch (count[i][j])
{
case 3 : array[i][j] = ’*’;
break;
default: continue;
}
}
}
}
}
Chapter 18: Arrays
/*******************************************************/
UpdateDisplay (array,g)
char array[SIZE][SIZE];
int g;
{ int i,j;
printf (“\n\nGeneration %d\n\n”,g);
for (i = 0; i < SIZE; i++)
{
/* print out life array */

Example Listing
for (j = 0; j < SIZE; j++)
{
printf(“%c”,array[i][j]);
}
printf(“\n”);
}
}
/*******************************************************/
/* Level 2*/
/*******************************************************/
numalive (array,i,j)
/* Don’t count array[i,j] : only its neighbours */
/* Also check that haven’t reached the boundary */
/* of the array*/
char array[SIZE][SIZE];
int i,j;
{ int a,b,census;
census = 0;
for (a = (i-1); (a <= (i+1)); a++)
{
for (b = (j-1); (b <= (j+1)); b++)
{
if (INBOUNDS && (array[a][b] == ’*’))
{
census++;
}
}
}
if (array[i][j] == ’*’) census–;
return (census);
}
/********************************************************/
/* Toolkit input*/
/********************************************************/
quit()
{ char ch;
while (NORESPONSE)
{
scanf (“%c”,&ch);
169

170
if (ch != ’\n’) skipgarb();
switch (ch)
{
case ’q’ : case ’Q’ : return (1);
default :return (0);
}
}
}
Chapter 18: Arrays
/********************************************************/
skipgarb ()
{
while (getchar() != ’\n’)
{
}
}
18.8 Output of Game of Life
Game of Life
Enter starting setup. Type ’.’ for empty
and any other character for occupied.
RETURN after each line.
Array SIZE guide:
^^^^^^^^^^^^^^^^^^^^
(user types in:
………………..
………………..
…………………
…………………
…………………
……….***……..
………..*………
………………….
…………………
…………………
…………………
*********************
…………………
………………….
………………..
…………………
………………….
………………….
(It doesn’t matter if the input
spills over the SIZE guide,
because “skipgarb()” discards it.)

Output of Game of Life
………………….
…………………. )
171
Input is complete. Press RETURN.
Generation 1
………………..
………………..
………………..
………………..
………..*……..
……….***…….
……….***…….
………………..
………………..
………………..
.******************.
.******************.
.******************.
………………..
………………..
………………..
………………..
………………..
………………..
………………..
Q for quit. RETURN to continue.
Generation 2
………………..
………………..
………………..
………………..
……….***…….
………………..
……….*.*…….
………..*……..
………………..
..****************..
.*…………….*.
*………………*
.*…………….*.
..****************..
………………..
………………..

172
………………..
………………..
………………..
………………..
Chapter 18: Arrays
Q for quit. RETURN to continue.
Generation 3
………………..
………………..
………………..
………..*……..
………..*……..
……….*.*…….
………..*……..
………..*……..
…*******…****…
..****************..
.******************.
**…………….**
.******************.
..****************..
…**************…
………………..
………………..
………………..
………………..
………………..
Q for quit. RETURN to continue.
Generation 4
………………..
………………..
………………..
………………..
……….***…….
……….*.*…….
……….***…….
….*****.*.*.**….
..*…………..*..
.*…………….*.
*………………*
*………………*
*………………*
.*…………….*.

Initializing Arrays
..*…………..*..
….************….
………………..
………………..
………………..
………………..
173
Q for quit. RETURN to continue.
etc… Try experimenting with different starting patterns.
18.9 Initializing Arrays
Arrays can be initialized in two ways. The first way is by assigning every
element to some value with a statement like:
array[2] = 42;
array[3] = 12;
or perhaps with the aid of one or more for loops. Because it is tedious, to
say the least, not to mention uneconomical, to initialize the values of each
element to as different value, C provides another method, which employs a
single assignment operator ‘=’ and curly braces { }. This method only works
for static variables and external variables.
Recall that arrays are stored row-wise or with the last index varying
fastest. A 3 by 3 array could be initialized in the following way:
static int array[3][3] =
{
{10,23,42},
{1,654,0},
{40652,22,0}
};
The internal braces are unnecessary, but help to distinguish the rows from
the columns. The same thing could be written:
int array[3][3] =
{
10,23,42,
1,654,0
40652,22,0
};
Take care to include the semicolon at the end of the curly brace which closes
the assignment.

174
Chapter 18: Arrays
Note that, if there are not enough elements in the curly braces to account
for every single element in an array, the remaining elements will be filled out
with zeros. Static variables are always guaranteed to be initialized to zero
anyway, whereas auto or local variables are guaranteed to be garbage: this is
because static storage is created by the compiler in the body of a program,
whereas auto or local storage is created at run time.
18.10 Arrays and Pointers
The information about how arrays are stored was not included just for in-
terest. There is another way of looking at arrays which follows the BCPL
idea of an array as simply a block of memory. An array can be accessed with
pointers as well as with [] square brackets.
The name of an array variable, standing alone, is actually a pointer to
the first element in the array.
For example: if an array is declared
float numbers[34];
then numbers is a pointer to the first floating point number in the array;
numbers is a pointer in its own right. (In this case it is type ‘pointer to
float’.) So the first element of the array could be accessed by writing:
numbers[0] = 22.3;
or by writing
*numbers = 22.3;
For character arrays, which are dealt with in some depth in chapter 20, this
gives an alternative way of getting at the elements in the array.
char arrayname[5];
char *ptr;
for (ptr = arrayname; ptr <= arrayname+4; ptr++)
{
*ptr = 0;
}
The code above sets the array arrayname to zero. This method of getting
at array data is not recommended by this author except in very simple
computer environments. If a program is running on a normal microcomputer,
then there should be few problems with this alternative method of handling
arrays. On the hand, if the microcomputer is multi-tasking, or the program
is running on a larger system which has a limited manager, then memory
ceases to be something which can be thought of as a sequence of boxes
standing next to one another. A multi-tasking system shares memory with

Questions
175
other programs and it takes what it can find, where it can find it. The upshot
of this is that it is not possible to guarantee that arrays will be stored in one
simple string of memory locations: it might be scattered around in different
places. So
ptr = arrayname + 5;
might not be a pointer to the fifth character in a character array. This could
be found instead using the ‘&’ operator. A pointer to the fifth element can
be reliably found with:
ptr = &(arrayname[5]);
Be warned!
18.11 Arrays as Parameters
What happens if we want to pass an array as a parameter? Does the program
copy the entire array into local storage? The answer is no because it would
be a waste of time and memory. Arrays can be passed as parameters, but
only as variable ones. This is a simple matter, because the name of the array
is a pointer to the array. The Game of Life program above does this. Notice
from that program how the declarations for the parameters are made.
main ()
{
char array[23];
function (array);
…..
}
function (arrayformal)
char arrayformal[23];
{
}
Any function which writes to the array, passed as a parameter, will affect
the original copy. Array parameters are always variable parameters
18.12 Questions
1. Given any array, how would you find a pointer to the start of it?
2. How do you pass an array as a parameter? When the parameter is
received by a function does C allocate space for a local variable and
copy the whole array to the new location?

176
Chapter 18: Arrays
3. Write a statement which declares an array of type double which mea-
sures 4 by 5. What numbers can be written in the indicies of the array?

Strings, Arrays and Pointers
177
19 Strings
Communication with arrays.
Strings are pieces of text which can be treated as values for variables. In
C a string is represented as some characters enclosed by double quotes.
“This is a string”
A string may contain any character, including special control characters,
such as ‘\n’, ‘\r’, ‘\7’ etc…
“Beep! \7 Newline \n…”
19.1 Conventions and Declarations
There is an important distinction between a string and a single character in
C. The convention is that single characters are enclosed by single quotes e.g.
‘*’ and have the type char. Strings, on the hand, are enclosed by double
quotes e.g. “string…” and have the type “pointer to char” ‘(char *)’ or
array of char. Here are some declarations for strings which are given without
immediate explanations.
/**********************************************************/
/**/
/* String Declaration*/
/**/
/**********************************************************/
#define SIZE
10
char *global_string1;
char global_string2[SIZE];
main ()
{ char *auto_string;
char arraystr[SIZE];
static char *stat_strng;
static char statarraystr[SIZE];
}

178
Chapter 19: Strings
19.2 Strings, Arrays and Pointers
A string is really an array of characters. It is stored at some place the
memory and is given an end marker which standard library functions can
recognize as being the end of the string. The end marker is called the zero
(or NULL) byte because it is just a byte which contains the value zero: ‘’.
Programs rarely gets to see this end marker as most functions which handle
strings use it or add it automatically.
Strings can be declared in two main ways; one of these is as an array of
characters, the other is as a pointer to some pre-assigned array. Perhaps the
simplest way of seeing how C stores arrays is to give an extreme example
which would probably never be used in practice. Think of how a string called
string might be used to to store the message “Tedious!”. The fact that a
string is an array of characters might lead you to write something like:
#define LENGTH 9;
main ()
{ char string[LENGTH];
string[0]
string[1]
string[2]
string[3]
string[4]
string[5]
string[6]
string[7]
string[8]
=
=
=
=
=
=
=
=
=
’T’;
’e’;
’d’;
’i’;
’o’;
’u’;
’s’;
’!’;
’’;
printf (“%s”, string);
}
This method of handling strings is perfectly acceptable, if there is time to
waste, but it is so laborious that C provides a special initialization service
for strings, which bypasses the need to assign every single character with a
new assignment!. There are six ways of assigning constant strings to arrays.
(A constant string is one which is actually typed into the program, not one
which in typed in by the user.) They are written into a short compilable
program below. The explanation follows.
/**********************************************************/
/**/
/* String Initialization*/
/**/
/**********************************************************/
char *global_string1 = “A string declared as a pointer”;

Strings, Arrays and Pointers
char
global_string2[] = “Declared as an array”;
179
main ()
{ char *auto_string = “initializer…”;
static char *stat_strng = “initializer…”;
static char statarraystr[] = “initializer….”;
/* char arraystr[] = “initializer….”; IS ILLEGAL! */
/* This is because the array is an “auto” type*/
/* which cannot be preinitialized, but…*/
char arraystr[20];
printf (“%s %s”, global_string1, global_string2);
printf (“%s %s %s”, auto_string, stat_strng, statarraystr);
}
/* end */
The details of what goes on with strings can be difficult to get to grips
with. It is a good idea to get revise pointers and arrays before reading
the explanations below. Notice the diagrams too: they are probably more
helpful than words.
The first of these assignments is a global, static variable. More correctly,
it is a pointer to a global, static array. Static variables are assigned storage
space in the body of a program when the compiler creates the executable
code. This means that they are saved on disk along with the program code, so
they can be initialized at compile time. That is the reason for the rule which
says that only static arrays can be initialized with a constant expression in
a declaration. The first statement allocates space for a pointer to an array.
Notice that, because the string which is to be assigned to it, is typed into
the program, the compiler can also allocate space for that in the executable
file too. In fact the compiler stores the string, adds a zero byte to the
end of it and assigns a pointer to its first character to the variable called
global_string1.
The second statement works almost identically, with the exception that,
this time the compiler sees the declaration of a static array, which is to be
initialized. Notice that there is no size declaration in the square brackets.
This is quite legal in fact: the compiler counts the number of characters in
the initialization string and allocates just the right amount of space, filling
the string into that space, along with its end marker as it goes. Remember
also that the name of the array is a pointer to the first character, so, in fact,
the two methods are identical.

180
Chapter 19: Strings
The third expression is the same kind of thing, only this time, the decla-
ration is inside the function main() so the type is not static but auto. The
difference between this and the other two declarations is that this pointer
variable is created every time the function main() is called. It is new each
time and the same thing holds for any other function which it might have
been defined in: when the function is called, the pointer is created and when
it ends, it is destroyed. The string which initializes it is stored in the exe-
cutable file of the program (because it is typed into the text). The compiler
returns a value which is a pointer to the string’s first character and uses that
as a value to initialize the pointer with. This is a slightly round about way
of defining the string constant. The normal thing to do would be to declare
the string pointer as being static, but this is just a matter of style. In fact
this is what is done in the fourth example.
The fifth example is again identical, in practice to other static types, but
is written as an ‘open’ array with an unspecified size.
The sixth example is forbidden! The reason for this might seem rather
trivial, but it is made in the interests of efficiency. The array declared is
of type auto: this means that the whole array is created when the function
is called and destroyed afterwards. auto-arrays cannot be initialized with a
string because they would have to be re-initialized every time the array were
created: that is, each time the function were called. The final example could
be used to overcome this, if the programmer were inclined to do so. Here an
auto array of characters is declared (with a size this time, because there is
nothing for the compiler to count the size of). There is no single assignment
which will fill this array with a string though: the programmer would have
to do it character by character so that the inefficiency is made as plain as
possible!
19.3 Arrays of Strings
In the previous chapter we progressed from one dimensional arrays to two
dimensional arrays, or arrays of arrays! The same thing works well for
strings which are declared static. Programs can take advantage of C’s easy
assignment facilities to let the compiler count the size of the string arrays
and define arrays of messages. For example here is a program which prints
out a menu for an application program:
/*********************************************************/
/**/
/* MENU : program which prints out a menu*/
/**/
/*********************************************************/
main ()
{ int str_number;

Example Listing
for (str_number = 0; str_number < 13; str_number++)
{
printf (“%s”,menutext(str_number));
}
}
/*********************************************************/
char *menutext(n)
int n;
{
static char *t[] =
{
” ————————————– \n”,
” |++ MENU ++|\n”,
” |~~~~~~~~~~~~|\n”,
” |(1) Edit Defaults|\n”,
” |(2) Print Charge Sheet|\n”,
” |(3) Print Log Sheet|\n”,
” |(4) Bill Calculator|\n”,
” |(q) Quit|\n”,
” ||\n”,
” ||\n”,
” |Please Enter Choice|\n”,
” ||\n”,
” ————————————– \n”
};
return (t[n]);
}
/* return n-th string ptr */
181
Notice the way in which the static declaration works. It is initialized once at
compile time, so there is effectively only one statement in this function and
that is the return statement. This function retains the pointer information
from call to call. The Morse coder program could be rewritten more econom-
ically using static strings, See undefined [Example 15], page undefined .
19.4 Example Listing
/************************************************/
/**/
/* static string array*/
/**/
/************************************************/
/* Morse code program. Enter a number and */
/* find out what it is in Morse code*/
#include <stdio.h>

182
Chapter 19: Strings
#define CODE 0
/*************************************************/
main ()
{ short digit;
printf (“Enter any digit in the range 0..9”);
scanf (“%h”,&digit);
if ((digit < 0) || (digit > 9))
{
printf (“Number was not in range 0..9”);
return (CODE);
}
printf (“The Morse code of that digit is “);
Morse (digit);
}
/************************************************/
Morse (digit)
short digit;
{
static char *code[] =
{
“dummy”,
“—–“,
“.—-“,
“..—“,
“…–“,
“….-“,
“…..”,
“-….”,
“–…”,
“—..”,
“—-.”,
};
/* print out Morse code */
/* index starts at 0 */
printf (“%s\n”,code[digit]);
}

Strings from the user
183
19.5 Strings from the user
All the strings mentioned so far have been typed into a program by the
programmer and stored in a program file, so it has not been necessary to
worry about where they were stored. Often though we would like to fetch
a string from the user and store it somewhere in the memory for later use.
It might even be necessary to get a whole bunch of strings and store them
all. But how will the program know in advance how much array space to
allocate to these strings? The answer is that it won’t, but that it doesn’t
matter at all!
One way of getting a simple, single string from the user is to define an
array and to read the characters one by one. An example of this was the
Game of Life program the the previous chapter:
• Define the array to be a certain size
• Check that the user does not type in too many characters.
• Use the string in that array.
Another way is to define a static string with an initializer as in the following
example. The function filename() asks the user to type in a filename, for
loading or saving by and return it to a calling function.
char *filename()
{ static char *filenm = “……………………”;
do
{
printf (“Enter filename :”);
scanf (“%24s”,filenm);
skipgarb();
}
while (strlen(filenm) == 0);
return (filenm);
}
The string is made static and given an initializing expression and this forces
the compiler to make some space for the string. It makes exactly 24 charac-
ters plus a zero byte in the program file, which can be used by an applica-
tion. Notice that the conversion string in scanf prevents the characters from
spilling over the bounds of the string. The function strlen() is a standard
library function which is described below; it returns the length of a string.
skipgarb() is the function which was introduced in chapter 15.
Neither of the methods above is any good if a program is going to be
fetching a lot of strings from a user. It just isn’t practical to define lots of
static strings and expect the user to type into the right size boxes! The next
step in string handling is therefore to allocate memory for strings personally:
in other words to be able to say how much storage is needed for a string while
a program is running. C has special memory allocation functions which can

184
Chapter 19: Strings
do this, not only for strings but for any kind of object. Suppose then that a
program is going to get ten strings from the user. Here is one way in which
it could be done:
1. Define one large, static string (or array) for getting one string at a time.
Call this a string buffer, or waiting place.
2. Define an array of ten pointers to characters, so that the strings can be
recalled easily.
3. Find out how long the string in the string buffer is.
4. Allocate memory for the string.
5. Copy the string from the buffer to the new storage and place a pointer
to it in the array of pointers for reference.
6. Release the memory when it is finished with.
The function which allocates memory in C is called malloc() and it
works like this:
• malloc() should be declared as returning the type pointer to character,
with the statement:
char *malloc();
• malloc() takes one argument which should be an unsigned integer value
telling the function how many bytes of storage to allocate. It returns a
pointer to the first memory location in that storage:
char *ptr;
unsigned int size;
ptr = malloc(size);
• The pointer returned has the value NULL if there was no memory left to
allocate. This should always be checked.
The fact that malloc() always returns a pointer to a character does not
stop it from being used for other types of data too. The cast operator can
force malloc() to give a pointer to any data type. This method is used for
building data structures in C with “struct” types.
malloc() has a complementary function which does precisely the oppo-
site: de-allocates memory. This function is called free(). free() returns
an integer code, so it does not have to be declared as being any special type.
• free() takes one argument: a pointer to a block of memory which has
previously been allocated by malloc().
int returncode;
returncode = free (ptr);

Handling strings
• The pointer should be declared:
char *ptr;
185
• The return code is zero if the release was successful.
An example of how strings can be created using malloc() and free() is
given below. First of all, some explanation of Standard Library Functions is
useful to simplify the program.
19.6 Handling strings
The C Standard Library commonly provides a number of very useful func-
tions which handle strings. Here is a short list of some common ones which
are immediately relevant (more are listed in the following chapter). Chances
are, a good compiler will support a lot more than those listed below, but,
again, it really depends upon the compiler.
strlen()
This function returns a type int value, which gives the length
or number of characters in a string, not including the NULL byte
end marker. An example is:
int len;
char *string;
len = strlen (string);
strcpy()
This function copies a string from one place to another. Use this
function in preference to custom routines: it is set up to handle
any peculiarities in the way data are stored. An example is
char *to,*from;
to = strcpy (to,from);
Where to is a pointer to the place to which the string is to be
copied and from is the place where the string is to be copied
from.
strcmp()
This function compares two strings and returns a value which
indicates how they compared. An example:
int value;
char *s1,*s2;
value = strcmp(s1,s2);
The value returned is 0 if the two strings were identical. If the
strings were not the same, this function indicates the (ASCII)
alphabetical order of the two. s1 > s2, alphabetically, then the

186
Chapter 19: Strings
value is > 0. If s1 < s2 then the value is < 0. Note that numbers
come before letters in the ASCII code sequence and also that
upper case comes before lower case.
Tests whether a substring is present in a larger string
int n;
char *s1,*s2;
if (n = strstr(s1,s2))
{
printf(“s2 is a substring of s1, starting at %d”,n);
}
strstr()
strncpy()
This function is like strcpy, but limits the copy to no more than
n characters.
strncmp()
This function is like strcmp, but limits the comparison to no
more than n characters.
More string functions are described in the next section along with a host of
Standard Library Functions.
19.7 Example Listing
This program aims to get ten strings from the user. The strings may not
contain any spaces or white space characters. It works as follows:
The user is prompted for a string which he/she types into a buffer. The
length of the string is tested with strlen() and a block of memory is al-
located for it using malloc(). (Notice that this block of memory is one
byte longer than the value returned by strlen(), because strlen() does
not count the end of string marker ‘’.) malloc() returns a pointer to the
space allocated, which is then stored in the array called array. Finally the
strings is copied from the buffer to the new storage with the library func-
tion strcpy(). This process is repeated for each of the 10 strings. Notice
that the program exits through a low level function called QuitSafely().
The reason for doing this is to exit from the program neatly, while at the
same time remembering to perform all a programmer’s duties, such as de-
allocating the memory which is no longer needed. QuitSafely() uses the
function exit() which should be provided as a standard library function.
exit() allows a program to end at any point.
/******************************************************/
/**/
/* String storage allocation*/
/**/
/******************************************************/

Example Listing
187
#include <stdio.h>
/* #include another file for malloc() and*/
/* strlen() ???. Check the compiler manual! */
#define NOOFSTR
#define BUFSIZE
#define CODE
10
255
0
/******************************************************/
/* Level 0*/
/******************************************************/
main ()
{ char *array[NOOFSTR], *malloc();
char buffer[BUFSIZE];
int i;
for (i = 0; i < NOOFSTR; i++)
{
printf (“Enter string %d :”,i);
scanf (“%255s”,buffer);
array[i] = malloc(strlen(buffer)+1);
if (array[i] == NULL)
{
printf (“Can’t allocate memory\n”);
QuitSafely (array);
}
strcpy (array[i],buffer);
}
for (i = 0; i < NOOFSTR; i++)
{
printf (“%s\n”,array[i]);
}
QuitSafely(array);
}
/******************************************************/
/* Snakes & Ladders!*/
/******************************************************/
QuitSafely (array)
char *array[NOOFSTR];
/* Quit & de-alloc memory */

188
Chapter 19: Strings
{ int i, len;
for (i = 0; i < NOOFSTR; i++)
{
len = strlen(array[i]) + 1;
if (free (array[i]) != 0)
{
printf (“Debug: free failed\n”);
}
}
exit (CODE);
}
/* end */
19.8 String Input/Output
Because strings are recognized to be special objects in C, some special library
functions for reading and writing are provided for them. These make it easier
to deal with strings, without the need for special user-routines. There are
four of these functions:
gets()
puts()
sprintf()
sscanf()
19.8.1 gets()
This function fetches a string from the standard input file stdin and places
it into some buffer which the programmer must provide.
#define SIZE
255
char *sptr, buffer[SIZE];
strptr = gets(buffer);
If the routine is successful in getting a string, it returns the value buffer
to the string pointer strptr. Otherwise it returns NULL (==0). The ad-
vantage of gets() over scanf(“%s”..) is that it will read spaces in strings,
whereas scanf() usually will not. gets() quits reading when it finds a
newline character: that is, when the user presses RETURN.
NOTE: there are valid concerns about using this function. Often it is
implemented as a macro with poor bounds checking and can be exploited

sscanf()
189
to produce memory corruption by system attackers. In order to write more
secure code, use fgets() instead.
19.8.2 puts()
puts() sends a string to the output file stdout, until it finds a NULL end of
string marker. The NULL byte is not written to stdout, instead a newline
character is written.
char *string;
int returncode;
returncode = puts(string);
puts() returns an integer value, whose value is only guaranteed if there is
an error. returncode == EOF if an end of file was encountered or there was
an error.
19.8.3 sprintf()
This is an interesting function which works in almost the same way as
printf(), the exception being that it prints to a string! In other words
it treats a string as though it were an output file. This is useful for creating
formatted strings in the memory. On most systems it works in the following
way:
int n;
char *sp;
n = sprintf (sp, “control string”, parameters, values);
n is an integer which is the number of characters printed. sp is a pointer
to the destination string or the string which is to be written to. Note care-
fully that this function does not perform any check on the output string to
make sure that it is long enough to contain the formatted output. If the
string is not large enough, then a crash could be in store! This can also be
considered a potential security problem, since buffer overflows can be used
to capture control of important programs. Note that on system V Unix
systems the sprintf functionr returns a pointer to the start of the printed
string, breaking the pattern of the other printf functions. To make such an
implementation compatible with the usual form you would have to write:
n = strlen(sprintf(parameters…… ));
19.8.4 sscanf()
This function is the complement of sprintf(). It reads its input from a
string, as though it were an input file.
int n;

190
char *sp;
n = sscanf (sp,”control string”, pointers…);
Chapter 19: Strings
sp is a pointer to the string which is to be read from. The string must be NULL
terminated (it must have a zero-byte end marker ’’). sscanf() returns
an integer value which holds the number of items successfully matched or
EOF if an end of file marker was read or an error occurred. The conversion
specifiers are identical to those for scanf().
19.9 Example Listing
/************************************************/
/**/
/* Formatted strings*/
/**/
/************************************************/
/* program rewrites s1 in reverse into s2 */
#include <stdio.h>
#define SIZE
#define CODE
20
0
/************************************************/
main ()
{ static char *s1 = “string 2.3 55x”;
static char *s2 = “………………..”;
char ch, *string[SIZE];
int i,n;
float x;
sscanf (s1,”%s %f %d %c”, string, &x, &i, &ch);
n = sprintf (s2,”%c %d %f %s”, ch, i, x, string);
if (n > SIZE)
{
printf (“Error: string overflowed!\n”);
exit (CODE);
}
puts (s2);
}

Questions
191
19.10 Questions
1. What are the two main ways of declaring strings in a program?
2. How would you declare a static array of strings?
3. Write a program which gets a number between 0 and 9 and prints out a
different message for each number. Use a pre-initialized array to store
the strings.

192
Chapter 19: Strings

The argument vector
193
20 Putting together a program
Putting it all together.
20.1 The argument vector
C was written in order to implement Unix in a portable form. Unix was
designed with a command language which was built up of independent pro-
grams. These could be passed arguments on the command line. For instance:
ls -l /etc
cc -o program prog.c
In these examples, the first word is the command itself, while the subsequent
words are options and arguments to the command. We need some way
getting this information into a C program. Unix solved this problem by
passing C programs an array of these arguments together with their number
as parameters to the function main(). Since then most other operating
systems have adopted the same model, since it has become a part of the C
language.
main (argc,argv)
int argc;
char *argv[];
{
}
The traditional names for the parameters are the argument count argc and
the argument vector (array) argv. The operating system call which starts
the C program breaks up the command line into an array, where the first
element argv[0] is the name of the command itself and the last argument
argv[argc-1] is the last argument. For example, in the case of
cc -o program prog.c
would result in the values
argv[0]
argv[1]
argv[2]
argv[3]
cc
-o
program
prog.c
The following program prints out the command line arguments:

194
Chapter 20: Putting together a program
main (argc,argv)
int argc;
char *argv[];
{ int i;
printf (“This program is called %s\n”,argv[0]);
if (argc > 1)
{
for (i = 1; i < argc; i++)
{
printf(“argv[%d] = %s\n”,i,argv[i]);
}
}
else
{
printf(“Command has no arguments\n”);
}
}
20.2 Processing options
getopt
20.3 Environment variables
When we write a C program which reads command line arguments, they are
fed to us by the argument vector. Unix processes also a set of text variable
associations called environment variables. Each child process inherits the
environment of its parent. The static environment variables are stored in a
special array which is also passed to main() and can be read if desired.
main (argc,argv,envp)
int argc;
char *argv[], *envp[];
{
}
The array of strings ‘envp[]’ is a list of values of the environment variables
of the system, formatted by
NAME=value
This gives C programmers access to the shell’s global environment.

Environment variables
195
In addition to the ‘envp’ vector, it is possible to access the environment
variables through the call ‘getenv()’. This is used as follows; suppose we
want to access the shell environment variable ‘$HOME’.
char *string;
string = getenv(“HOME”);
‘string’ is now a pointer to static but public data. You should not use
‘string’ as if it were you’re own property because it will be used again by
the system. Copy it’s contents to another string before using the data.
char buffer[500];
strcpy (buffer,string);

196
Chapter 20: Putting together a program

Character Identification
197
21 Special Library Functions and Macros
Checking character types. Handling strings. Doing maths.
C provides a repertoire of standard library functions and macros for spe-
cialized purposes (and for the advanced user). These may be divided into
various categories. For instance
• Character identification (‘ctype.h’)
• String manipulation (‘string.h’)
• Mathematical functions (‘math.h’)
A program generally has to #include special header files in order to use
special functions in libraries. The names of the appropriate files can be
found in particular compiler manuals. In the examples above the names of
the header files are given in parentheses.
21.1 Character Identification
Some or all of the following functions/macros will be available for identifying
and classifying single characters. The programmer ought to beware that
it would be natural for many of these facilities to exist as macros rather
than functions, so the usual remarks about macro parameters apply, See
Chapter 12 [Preprocessor], page 71. An example of their use is given above.
Assume that ‘true’ has any non-zero, integer value and that ‘false’ has the
integer value zero. ch stands for some character, or char type variable.
isalpha(ch)
This returns true if ch is alphabetic and false otherwise. Alpha-
betic means a..z or A..Z.
isupper(ch)
Returns true if the character was upper case. If ch was not an
alphabetic character, this returns false.
islower(ch)
Returns true if the character was lower case. If ch was not an
alphabetic character, this returns false.
isdigit(ch)
Returns true if the character was a digit in the range 0..9.
isxdigit(ch)
Returns true if the character was a valid hexadecimal digit: that
is, a number from 0..9 or a letter a..f or A..F.
isspace(ch)
Returns true if the character was a white space character, that
is: a space, a TAB character or a newline.

198
Chapter 21: Special Library Functions and Macros
ispunct(ch)
Returns true if ch is a punctuation character.
isalnum(ch)
Returns true if a character is alphanumeric: that is, alphabetic
or digit.
isprint(ch)
Returns true if the character is printable: that is, the character
is not a control character.
isgraph(ch)
Returns true if the character is graphic. i.e. if the character is
printable (excluding the space)
iscntrl(ch)
Returns true if the character is a control character. i.e. ASCII
values 0 to 31 and 127.
isascii(ch)
Returns true if the character is a valid ASCII character: that is,
it has a code in the range 0..127.
iscsym(ch)
Returns true if the character was a character which could be
used in a C identifier.
toupper(ch)
This converts the character ch into its upper case counterpart.
This does not affect characters which are already upper case, or
characters which do not have a particular case, such as digits.
tolower(ch)
This converts a character into its lower case counterpart. It does
not affect characters which are already lower case.
toascii(ch)
This strips off bit 7 of a character so that it is in the range 0..127:
that is, a valid ASCII character.
21.2 Examples
/********************************************************/
/**/
/* Demonstration of character utility functions*/
/**/
/********************************************************/
/* prints out all the ASCII characters which give */
/* the value “true” for the listed character fns */

Examples
#include <stdio.h>
#include <ctype.h>
#define
ALLCHARS
199
/* contains character utilities */
ch = 0; isascii(ch); ch++
/********************************************************/
main ()
{ char ch;
printf (“VALID CHARACTERS FROM isalpha()\n\n”);
for (ALLCHARS)
{
if (isalpha(ch))
{
printf (“%c “,ch);
}
}
printf (“\n\nVALID CHARACTERS FROM isupper()\n\n”);
for (ALLCHARS)
{
if (isupper(ch))
{
printf (“%c “,ch);
}
}
printf (“\n\nVALID CHARACTERS FROM islower()\n\n”);
for (ALLCHARS)
{
if (islower(ch))
{
printf (“%c “,ch);
}
}
printf (“\n\nVALID CHARACTERS FROM isdigit()\n\n”);
for (ALLCHARS)
{
if (isdigit(ch))
{
printf (“%c “,ch);
}
}
printf (“\n\nVALID CHARACTERS FROM isxdigit()\n\n”);
/* A criminally long main program! */

200
Chapter 21: Special Library Functions and Macros
for (ALLCHARS)
{
if (isxdigit(ch))
{
printf (“%c “,ch);
}
}
printf (“\n\nVALID CHARACTERS FROM ispunct()\n\n”);
for (ALLCHARS)
{
if (ispunct(ch))
{
printf (“%c “,ch);
}
}
printf (“\n\nVALID CHARACTERS FROM isalnum()\n\n”);
for (ALLCHARS)
{
if (isalnum(ch))
{
printf (“%c “,ch);
}
}
printf (“\n\nVALID CHARACTERS FROM iscsym()\n\n”);
for (ALLCHARS)
{
if (iscsym(ch))
{
printf (“%c “,ch);
}
}
}
21.3 Program Output
VALID CHARACTERS FROM isalpha()
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j
k l m n o p q r s t u v w x y z
VALID CHARACTERS FROM isupper()
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

String Manipulation
201
VALID CHARACTERS FROM islower()
a b c d e f g h i j k l m n o p q r s t u v w x y z
VALID CHARACTERS FROM isdigit()
0 1 2 3 4 5 6 7 8 9
VALID CHARACTERS FROM isxdigit()
0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f
VALID CHARACTERS FROM ispunct()
! ” # $ % & ’ ( ) * + , – . / : ; < = > ? @ [ \ ] ^ _ ‘ { | } ~
VALID CHARACTERS FROM isalnum()
0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W
X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z
VALID CHARACTERS FROM iscsym()
0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W
X Y Z _ a b c d e f g h i j k l m n o p q r s t u v w x y z
21.4 String Manipulation
The following functions perform useful functions for string handling, See
undefined [Strings], page undefined .
strcat() This function “concatenates” two strings: that is, it joins them
together into one string. The effect of:
char *new,*this, onto[255];
new = strcat(onto,this);
is to join the string this onto the string onto. new is a pointer to
the complete string; it is identical to onto. Memory is assumed
to have been allocated for the starting strings. The string which
is to be copied to must be large enough to accept the new string,
tagged onto the end. If it is not then unpredictable effects will
result. (In some programs the user might get away without
declaring enough space for the “onto” string, but in general the
results will be garbage, or even a crashed machine.) To join two
static strings together, the following code is required:
char *s1 = “string one”;
char *s2 = “string two”;

202
Chapter 21: Special Library Functions and Macros
main ()
{ char buffer[255];
strcat(buffer,s1);
strcat(buffer,s2);
}
buffer would then contain “string onestring two”.
strlen()
This function returns a type int value, which gives the length
or number of characters in a string, not including the NULL byte
end marker. An example is:
int len;
char *string;
len = strlen (string);
strcpy()
This function copies a string from one place to another. Use this
function in preference to custom routines: it is set up to handle
any peculiarities in the way data are stored. An example is
char *to,*from;
to = strcpy (to,from);
Where to is a pointer to the place to which the string is to be
copied and from is the place where the string is to be copied
from.
strcmp()
This function compares two strings and returns a value which
indicates how they compared. An example:
int value;
char *s1,*s2;
value = strcmp(s1,s2);
The value returned is 0 if the two strings were identical. If the
strings were not the same, this function indicates the (ASCII)
alphabetical order of the two. s1 > s2, alphabetically, then the
value is ‘> 0’. If s1 < s2 then the value is < 0. Note that numbers
come before letters in the ASCII code sequence and also that
upper case comes before lower case.
There are also variations on the theme of the functions above which begin
with ‘strn’ instead of ‘str’. These enable the programmer to perform the
same actions with the first n characters of a string:

Examples
strncat()
203
This function concatenates two strings by copying the first n
characters of this to the end of the onto string.
char *onto,*new,*this;
new = strncat(onto,this,n);
strncpy()
This function copies the first n characters of a string from one
place to another
char *to,*from;
int n;
to = strncpy (to,from,n);
strncmp()
This function compares the first n characters of two strings
int value;
char *s1,*s2;
value = strcmp(s1,s2,n);
The following functions perform conversions between strings and floating
point/integer types, without needing to use sscanf(). They take a pre-
initialized string and work out the value represented by that string.
atof()ASCII to floating point conversion.
double x;
char *stringptr;
x = atof(stringptr);
atoi()
ASCII to integer conversion.
int i;
char *stringptr;
i = atoi(stringptr);
atol()
ASCII to long integer conversion.
long i;
char *stringptr;
i = atol(stringptr);

204
Chapter 21: Special Library Functions and Macros
21.5 Examples
/********************************************************/
/**/
/* String comparison*/
/**/
/********************************************************/
#include <stdio.h>
#define TRUE1
#define MAXLEN 30
/********************************************************/
main ()
{ char string1[MAXLEN],string2[MAXLEN];
int result;
while (TRUE)
{
printf (“Type in string 1:\n\n”);
scanf (“%30s”,string1);
printf (“Type in string 2:\n\n”);
scanf (“%30s”,string2);
result = strcmp (string1,string2);
if (result == 0)
{
printf (“Those strings were the same!\n”);
}
if (result > 0)
{
printf (“string1 > string2\n”);
}
if (result < 0)
{
printf (“string1 < string 2\n”);
}
}
}
21.6 Mathematical Functions
C has a library of standard mathematical functions which can be accessed by
#including the appropriate header files (‘math.h’ etc.). It should be noted

Mathematical Functions
205
that all of these functions work with double or long float type variables.
All of C’s mathematical capabilities are written for long variable types. Here
is a list of the functions which can be expected in the standard library file.
The variables used are all to be declared long
int i;
double x,y,result;
/* long int */
/* long float */
The functions themselves must be declared long float or double (which might
be done automatically in the mathematics library file, or in a separate file)
and any constants must be written in floating point form: for instance, write
‘7.0’ instead of just ‘7’.
ABS()
fabs()
MACRO. Returns the unsigned value of the value in parentheses.
See fabs() for a function version.
Find the absolute or unsigned value of the value in parentheses:
result = fabs(x);
ceil()
Find out what the ceiling integer is: that is, the integer which
is just above the value in parentheses. This is like rounding up.
i = ceil(x);
/* ceil (2.2) is 3 */
floor()
Find out what the floor integer is: that is, the integer which is
just below the floating point value in parentheses
i = floor(x);
/* floor(2.2) is 2 */
exp()
Find the exponential value.
result = exp(x);
result = exp(2.7);
log()
Find the natural (Naperian) logarithm. The value used in the
parentheses must be unsigned: that is, it must be greater than
zero. It does not have to be declared specifically as unsigned.
e.g.
result = log(x);
result = log(2.71828);

206
log10()
Chapter 21: Special Library Functions and Macros
Find the base 10 logarithm. The value used in the parentheses
must be unsigned: that is, it must be greater than zero. It does
not have to be declared specifically as unsigned.
result = log10(x);
result = log10(10000);
pow()
Raise a number to the power.
result = pow(x,y); /*raise x to the power y */
result = pow(x,2); /*find x-squared */
result = pow(2.0,3.2); /* find 2 to the power 3.2 …*/
sqrt()
Find the square root of a number.
result = sqrt(x);
result = sqrt(2.0);
sin()
Find the sine of the angle in radians.
result = sin(x);
result = sin(3.14);
cos()
Find the cosine of the angle in radians.
result = cos(x);
result = cos(3.14);
tan()
Find the tangent of the angle in radians.
result = tan(x);
result = tan(3.14);
asin()
Find the arcsine or inverse sine of the value which must lie be-
tween +1.0 and -1.0.
result = asin(x);
result = asin(1.0);
acos()
Find the arccosine or inverse cosine of the value which must lie
between +1.0 and -1.0.
result = acos(x);
result = acos(1.0);
atan()
Find the arctangent or inverse tangent of the value.

Examples
207
result = atan(x);
result = atan(200.0);
atan2()
This is a special inverse tangent function for calculating the
inverse tangent of x divided by y. This function is set up to find
this result more accurately than atan().
result = atan2(x,y);
result = atan2(x/3.14);
sinh()
Find the hyperbolic sine of the value. (Pronounced “shine” or
“sinch”)
result = sinh(x);
result = sinh(5.0);
cosh()
Find the hyperbolic cosine of the value.
result = cosh(x);
result = cosh(5.0);
tanh()
Find the hyperbolic tangent of the value.
result = tanh(x);
result = tanh(5.0);
21.7 Examples
/******************************************************/
/**/
/* Maths functions demo #1*/
/**/
/******************************************************/
/* use sin(x) to work out an animated model */
#include <stdio.h>
#include <math.h>
#include <limits.h>
#define TRUE
#define AMPLITUDE
#define INC
double pi;
1
30
0.02
/* this may already be defined */
/* in the math file */

208
Chapter 21: Special Library Functions and Macros
/******************************************************/
/* Level 0*/
/******************************************************/
main ()
{ pi = asin(1.0)*2;
/* The simple pendulum program */
/* if PI is not defined */
printf (“\nTHE SIMPLE PENDULUM:\n\n\n”);
Pendulum();
}
/*****************************************************/
/* Level 1*/
/*****************************************************/
Pendulum ()
{ double x, twopi = pi * 2;
int i,position;
while (true)
{
for (x = 0; x < twopi; x += INC)
{
position = (int)(AMPLITUDE * sin(x));
for (i = -AMPLITUDE; i <= AMPLITUDE; i++)
{
if (i == position)
{
putchar(’*’);
}
else
{
putchar(’ ’);
}
}
startofline();
}
}
}
/*****************************************************/
/* Toolkit*/
/*****************************************************/
startofline()
{

Maths Errors
putchar(’\r’);
}
209
21.8 Maths Errors
Mathematical functions can be delicate animals. There exist mathemati-
cal functions which simply cannot produce sensible answers in all possible
cases. Mathematical functions are not “user friendly”! One example of an
unfriendly function is the inverse sine function asin(x) which only works for
values of x in the range +1.0 to -1.0. The reason for this is a mathematical
one: namely that the sine function (of which asin() is the opposite) only
has values in this range. The statement
y = asin (25.3);
is nonsense and it cannot possibly produce a value for y, because none exists.
Similarly, there is no simple number which is the square root of a negative
value, so an expression such as:
x = sqrt(-2.0);
would also be nonsense. This doesn’t stop the programmer from writing
these statements though and it doesn’t stop a faulty program from stray-
ing out of bounds. What happens then when an erroneous statement is
executed? Some sort of error condition would certainly have to result.
In many languages, errors, like the ones above, are terminal: they cause
a program to stop without any option to recover the damage. In C, as the
reader might have come to expect, this is not the case. It is possible (in
principle) to recover from any error, whilst still maintaining firm control of
a program.
Errors like the ones above are called domain errors (the set of values
which a function can accept is called the domain of the function). There are
other errors which can occur too. For example, division by zero is illegal,
because dividing by zero is “mathematical nonsense” – it can be done, but
the answer can be all the numbers which exist at the same time! Obviously
a program cannot work with any idea as vague as this. Finally, in addition
to these “pathological” cases, mathematical operations can fail just because
the numbers they deal with get too large for the computer to handle, or too
small, as the case may be.
Domain error
Illegal value put into function
Division by zero
Dividing by zero is nonsense.
Overflow
Number became too large
Underflow Number became too small.

210
Chapter 21: Special Library Functions and Macros
Loss of accuracy
No meaningful answer could be calculated
Errors are investigated by calling a function called matherr(). The math-
ematical functions, listed above, call this function automatically when an
error is detected. The function responds by returning a value which gives
information about the error. The exact details will depend upon a given
compiler. For instance a hypothetical example: if the error could be recov-
ered from, matherr() returns 0, otherwise it returns -1. matherr() uses
a “struct” type variable called an “exception” to diagnose faults in math-
ematical functions, See undefined [Structures and Unions], page unde-
fined . This can be examined by programs which trap their errors dutifully.
Information about this structure must be found in a given compiler manual.
Although it is not possible to generalize, the following remarks about the
behaviour of mathematical functions may help to avoid any surprises about
their behaviour in error conditions.
• A function which fails to produce a sensible answer, for any of the
reasons above, might simply return zero or it might return the maximum
value of the computer. Be careful to check this. (Division by zero and
underflow probably return zero, whereas overflow returns the maximum
value which the computer can handle.)
• Some functions return the value ‘NaN’. Not a form of Indian unleavened
bread, this stands for ‘Not a Number’, i.e. no sensible result could be
calculated.
• Some method of signalling errors must clearly be used. This is the ex-
ception structure (a special kind of C variable) which gives information
about the last error which occurred. Find out what it is and trap errors!
• Obviously, wherever possible, the programmer should try to stop errors
from occurring in the first place.
21.9 Example
Here is an example for the mathematically minded. The program below
performs numerical integration by the simplest possible method of adding
up the area under small strips of a graph of the function f(y) = 2*y. The
integral is found between the limits 0 and 5 and the exact answer is 25. (See
diagram.) The particular compiler used for this program returns the largest
number which can be represented by the computer when numbers overflow,
although, in this simple case, it is impossible for the numbers to overflow.
/**********************************************************/
/**/
/* Numerical Estimation of Integral*/
/**/
/**********************************************************/

Questions
#include <stdio.h>
#include <math.h>
#include <limits.h>
#define LIMIT 5
double inc = 0.001;
double twopi;
/* Increment width – arbitrary */
211
/***********************************************************/
/** LEVEL 0*/
/***********************************************************/
main ()
{ double y,integrand();
double integral = 0;
twopi = 4 * asin(1.0);
for ( y = inc/2; y < LIMIT; y += inc )
{
integral += integrand (y) * inc;
}
printf (“Integral value = %.10f \n”,integral);
}
/***************************************************************/
/** LEVEL 1**/
/***************************************************************/
double integrand (y)
double y;
{ double value;
value = 2*y;
if (value > 1e308)
{
printf (“Overflow error\n”);
exit (0);
}
return (value);
}
21.10 Questions
1. What type of data is returned from mathematical functions?

212
2.
3.
4.
5.
Chapter 21: Special Library Functions and Macros
All calculations are performed using long variables. True or false?
What information is returned by strlen()?
What action is performed by strcat()?
Name five kinds of error which can occur in a mathematical function.

Hidden operators and values
213
22 Hidden operators and values
Concise expressions
Many operators in C are more versatile than they appear to be, at first
glance. Take, for example, the following operators
=
++

+=
-=
etc…
the assignment, increment and decrement operators… These innocent look-
ing operators can be used in some surprising ways which make C source code
very neat and compact.
The first thing to notice is that ++ and — are unary operators: that
is, they are applied to a single variable and they affect that variable alone.
They therefore produce one unique value each time they are used. The
assignment operator, on the other hand, has the unusual position of being
both unary, in the sense that it works out only one expression, and also
binary or dyadic because it sits between two separate objects: an “lvalue”
on the left hand side and an expression on the right hand side. Both kinds
of operator have one thing in common however: both form statements which
have values in their own right. What does this mean? It means that certain
kinds of statement, in C, do not have to be thought of as being complete and
sealed off from the rest of a program. To paraphrase a famous author: “In
C, no statement is an island”. A statement can be taken as a whole (as a
“black box”) and can be treated as a single value, which can be assigned and
compared to things! The value of a statement is the result of the operation
which was carried out in the statement.
Increment/decrement operator statements, taken as a whole, have a value
which is one greater / or one less than the value of the variable which they
act upon. So:
c = 5;
c++;
The second of these statement ‘c++;’ has the value 6, and similarly:
c = 5;
c–;
The second of these statements ‘c–;’ has the value 4. Entire assignment
statements have values too. A statement such as:
c = 5;

214
Chapter 22: Hidden operators and values
has the value which is the value of the assignment. So the example above
has the value 5. This has some important implications.
22.1 Extended and Hidden =
The idea that assignment statement has a value, can be used to make C
programs neat and tidy for one simple reason: it means that a whole assign-
ment statement can be used in place of a value. For instance, the value ‘c =
0;’ could be assigned to a variable b:
b = (c = 0);
or simply:
b = c = 0;
These equivalent statements set b and c to the value zero, provided b and c
are of the same type! It is equivalent to the more usual:
b = 0;
c = 0;
Indeed, any number of these assignments can be strung together:
a = (b = (c = (d = (e = 5))))
or simply:
a = b = c = d = e = 5;
This very neat syntax compresses five lines of code into one single line! There
are other uses for the valued assignment statement, of course: it can be used
anywhere where a value can be used. For instance:
• In other assignments (as above)
• As a parameter for functions
• Inside a comparison (== > < etc..)
• As an index for arrays….
The uses are manifold. Consider how an assignment statement might be used
as a parameter to a function. The function below gets a character from the
input stream stdin and passes it to a function called ProcessCharacter():
ProcessCharacter (ch = getchar());
This is a perfectly valid statement in C, because the hidden assignment
statement passes on the value which it assigns. The actual order of events
is that the assignment is carried out first and then the function is called. It
would not make sense the other way around, because, then there would be

Example
215
no value to pass on as a parameter. So, in fact, this is a more compact way
of writing:
ch = getchar();
ProcessCharacter (ch);
The two methods are entirely equivalent. If there is any doubt, examine a
little more of this imaginary character processing program:
ProcessCharacter(ch = getchar());
if (ch == ’*’)
{
printf (“Starry, Starry Night…”);
}
The purpose in adding the second statement is to impress the fact that ch has
been assigned quite legitimately and it is still defined in the next statement
and the one after…until it is re-assigned by a new assignment statement.
The fact that the assignment was hidden inside another statement does not
make it any less valid. All the same remarks apply about the specialized
assignment operators +=, *=, /= etc..
22.2 Example
/************************************************/
/**/
/* Hidden Assignment #1*/
/**/
/************************************************/
main ()
{
do
{
switch (ch = getchar())
{
default : putchar(ch);
break;
case ’Q’ : /* Quit */
}
}
while (ch != ’Q’);
}
/* end */

216
Chapter 22: Hidden operators and values
/************************************************/
/**/
/* Hidden Assignment #2*/
/**/
/************************************************/
main ()
{ double x = 0;
while ((x += 0.2) < 20.0)
{
printf (“%lf”,x);
}
}
/* end */
22.3 Hidden ++ and —
The increment and decrement operators also form statements which have
intrinsic values and, like assignment expressions, they can be hidden away
in inconspicuous places. These two operators are slightly more complicated
than assignments because they exist in two forms: as a postfix and as a
prefix:
Postfix
var++
var–
Prefix
++var
–var
and these two forms have subtly different meanings. Look at the following
example:
int i = 3;
PrintNumber (i++);
The increment operator is hidden in the parameter list of the function
PrintNumber(). This example is not as clear cut as the assignment state-
ment examples however, because the variable i has, both a value before the
++ operator acts upon it, and a different value afterwards. The question is
then: which value is passed to the function? Is i incremented before or after
the function is called? The answer is that this is where the two forms of the
operator come into play.

Arrays, Strings and Hidden Operators
217
If the operator is used as a prefix, the operation is performed before the
function call. If the operator is used as a postfix, the operation is performed
after the function call.
In the example above, then, the value 3 is passed to the function and when
the function returns, the value of i is incremented to 4. The alternative is
to write:
int i = 3;
PrintNumber (++i);
in which case the value 4 is passed to the function PrintNumber(). The
same remarks apply to the decrement operator.
22.4 Arrays, Strings and Hidden Operators
Arrays and strings are one area of programming in which the increment and
decrement operators are used a lot. Hiding operators inside array subscripts
or hiding assignments inside loops can often make light work of tasks such as
initialization of arrays. Consider the following example of a one dimensional
array of integers.
#define SIZE
20
int i, array[SIZE];
for (i = 0; i < SIZE; array[i++] = 0)
{
}
This is a neat way of initializing an array to zero. Notice that the postfixed
form of the increment operator is used. This prevents the element array[0]
from assigning zero to memory which is out of the bounds of the array.
Strings too can benefit from hidden operators. If the standard library
function strlen() (which finds the length of a string) were not available,
then it would be a simple matter to write the function
strlen (string)
char *string;
{ char *ptr;
int count = 0;
for (ptr = string; *(ptr++) != ’’; count++)
{
}
return (count);
}
/* count the characters in a string */

218
Chapter 22: Hidden operators and values
This function increments count while the end of string marker ‘’ is not
found.
22.5 Example
/*********************************************************/
/**/
/* Hidden Operator Demo*/
/**/
/*********************************************************/
/* Any assignment or increment operator has a value */
/* which can be handed straight to printf() …*/
/* Also compare the prefix / postfix forms of ++/– */
#include <stdio.h>
/*********************************************************/
main ()
{ int a,b,c,d,e;
a = (b = (c = (d = (e = 0))));
printf (“%d %d %d %d %d\n”, a, b++, c–, d = 10, e += 3);
a = b = c = d = e = 0;
printf (“%d %d %d %d %d\n”, a, ++b, –c, d = 10, e += 3);
}
/* end */
/*******************************************************/
/**/
/* Hidden Operator demo #2*/
/**/
/*******************************************************/
#include <stdio.h>
/*******************************************************/
main ()
{
/* prints out zero! */

Cautions about Style
printf (“%d”,Value());
}
/*******************************************************/
Value()
{ int value;
if ((value = GetValue()) == 0)
{
printf (“Value was zero\n”);
}
return (value);
}
/********************************************************/
GetValue()
{
return (0);
}
/* end */
/* Some function to get a value */
/* Check for zero …. */
219
22.6 Cautions about Style
Hiding operators away inside other statements can certainly make programs
look very elegant and compact, but, as with all neat tricks, it can make
programs harder to understand. Never forget that programming is com-
munication to other programmers and be kind to the potential reader of a
program. (It could be you in years or months to come!) Statements such as:
if ((i = (int)ch++) <= –comparison)
{
}
are not recommendable programming style and they are no more efficient
than the more longwinded:
ch++;
i = (int)ch;
if (i <= comparison)
{
}
comparison–;

220
Chapter 22: Hidden operators and values
There is always a happy medium in which to settle on a readable version of
the code. The statement above might perhaps be written as:
i = (int) ch++;
if (i <= –comparison)
{
}
22.7 Example
/******************************************************/
/**/
/* Arrays and Hidden Operators*/
/**/
/******************************************************/
#include <stdio.h>
#define SIZE 10
/******************************************************/
/* Level 0*/
/******************************************************/
main ()
/* Demo prefix and postfix ++ in arrays */
{ int i, array[SIZE];
Initialize(array);
i = 4;
array[i++] = 8;
Print (array);
Initialize(array);
i = 4;
array[++i] = 8;
Print(array);
}
/*******************************************************/
/* Level 1*/
/*******************************************************/
Initialize (array)
int array[SIZE];
{ int i;
/* set to zero */

Questions
for (i = 0; i < SIZE; array[i++] = 0)
{
}
}
/******************************************************/
Print (array)
int array[SIZE];
{ int i = 0;
while (i < SIZE)
{
printf (“%2d”,array[i++]);
}
putchar (’\n’);
}
/* end */
/* to stdout */
221
/****************************************************/
/**/
/* Hidden Operator*/
/**/
/****************************************************/
#include <stdio.h>
#define MAXNO
20
/*****************************************************/
main ()
{ int i, ctr = 0;
for (i = 1; ++ctr <= MAXNO; i = ctr*5)
{
printf (“%3d”,i);
}
}
/* Print out 5 x table */
22.8 Questions
1. Which operators can be hidden inside other statements?
2. Give a reason why you would not want to do this in every possible case.

222
Chapter 22: Hidden operators and values
3. Hidden operators can be used in return statements .e.g
return (++x);
Would there be any point in writing:
return (x++);

Special Constant Expressions
223
23 More on data types
This section is about the remaining data types which C has to offer pro-
grammers. Since C allows you to define new data types we shall not be able
to cover all of the possiblities, only the most important examples. The most
important of these are
FILE
enum
void
volatile
const
struct
union
The type which files are classified under
Enumerated type for abstract data
The “empty” type
New ANSI standard type for memory mapped I/O
New ANSI standard type for fixed data
Groups of variables under a single name
Multi-purpose storage areas for dynamical memory allocation
23.1 Special Constant Expressions
Constant expressions are often used without any thought, until a program-
mer needs to know how to do something special with them. It is worth
making a brief remark about some special ways of writing integer constants,
for the latter half of this book.
Up to now the distinction between long and short integer types has largely
been ignored. Constant values can be declared explicitly as long values, in
fact, by placing the letter L after the constant.
long int variable = 23L;
variable = 236526598L;
Advanced programmers, writing systems software, often find it convenient
to work with hexadecimal or octal numbers since these number bases have
a special relationship to binary. A constant in one of these types is declared
by placing either ‘0’ (zero) or ‘0x’ in front of the appropriate value. If ddd
is a value, then:
Octal number
Hexadecimal number
0ddd
0xddd
For example:
oct_value = 077;
hex_value = 0xFFEF;
/* 77 octal */
/* FFEF hex */
This kind of notation has already been applied to strings and single character
constants with the backslash notation, instead of the leading zero character:

224
Chapter 23: More on data types
ch = ’\ddd ’;
ch = ’\xdd ’;
The values of character constants, like these, cannot be any greater than
255.
23.2 FILE
In all previous sections, the files stdin, stdout and stderr alone have
been used in programs. These special files are always handled implicitly by
functions like printf() and scanf(): the programmer never gets to know
that they are, in fact, files. Programs do not have to use these functions
however: standard input/output files can be treated explicitly by general
file handling functions just as well. Files are distinguished by filenames and
by file pointers. File pointers are variables which pass the location of files
to file handling functions; being variables, they have to be declared as being
some data type. That type is called FILE and file pointers have to be declared
“pointer to FILE”. For example:
FILE *fp;
FILE *fp = stdin;
FILE *fopen();
File handling functions which return file pointers must also be declared as
pointers to files. Notice that, in contrast to all the other reserved words FILE
is written in upper case: the reason for this is that FILE is not a simple data
type such as char or int, but a structure which is only defined by the header
file ‘stdio.h’ and so, strictly speaking, it is not a reserved word itself. We
shall return to look more closely at files soon.
23.3 enum
Abstract data are usually the realm of exclusively high level languages such
as Pascal. enum is a way of incorporating limited “high level” data facilities
into C.
enum is short for enumerated data. The user defines a type of data which
is made up of a fixed set of words, instead of numbers or characters. These
words are given substitute integer numbers by the compiler which are used
to identify and compare enum type data. For example:
enum countries
{
England,
Scotland,

Example
Wales,
Eire,
Norge,
Sverige,
Danmark,
Deutschland
};
main ()
{ enum countries variable;
variable = England;
}
225
Why go to all this trouble? The point about enumerated data is that they
allow the programmer to forget about any numbers which the computer
might need in order to deal with a list of words, like the ones above, and
simply concentrate on the logic of using them. Enumerated data are called
abstract because the low level number form of the words is removed from the
users attention. In fact, enumerated data are made up of integer constants,
which the compiler generates itself. For this reason, they have a natural
partner in programs: the switch statement. Here is an example, which uses
the countries above to make a kind of airport “help computer” in age of
electronic passports!
23.4 Example
/**********************************************************/
/**/
/* Enumerated Data*/
/**/
/**********************************************************/
#include <stdio.h>
enum countries
{
England,
Ireland,
Scotland,
Wales,
Danmark,
Island,
Norge,
Sverige
};

226
Chapter 23: More on data types
/**********************************************************/
main ()
/* Electronic Passport Program */
{ enum countries birthplace, getinfo();
printf (“Insert electronic passport\n”);
birthplace = getinfo();
switch (birthplace)
{
case England : printf (“Welcome home!\n”);
break;
case Danmark :
case Norge: printf (“Velkommen til England\n”);
break;
}
}
/************************************************************/
enum countries getinfo()
{
return (England);
}
/* end */
/* interrogate passport */
enum makes words into constant integer values for a programmer. Data
which are declared enum are not the kind of data which it makes sense to
do arithmetic with (even integer arithmetic), so in most cases it should not
be necessary to know or even care about what numbers the compiler gives
to the words in the list. However, some compilers allow the programmer
to force particular values on words. The compiler then tries to give the
values successive integer numbers unless the programmer states otherwise.
For instance:
enum planets
{
Mercury,
Venus,
Earth = 12,
Mars,
Jupiter,
Saturn,
Uranus,
Neptune,
Pluto
};

Example
227
This would probably yield values Mercury = 0, Venus = 1, Earth = 12,
Mars = 13, Jupiter = 14 … etc. If the user tries to force a value which the
compiler has already used then the compiler will complain.
The following example program listing shows two points:
• enum types can be local or global.
• The labels can be forced to have certain values
23.5 Example
/**********************************************************/
/**/
/* Enumerated Data*/
/**/
/**********************************************************/
/* The smallest adventure game in the world */
#include <stdio.h>
#define TRUE 1
#define FALSE 0
enum treasures
{
rubies,
sapphires,
gold,
silver,
mask,
scroll,
lamp
};
/* Adventure Treasures */
/***********************************************************/
/* Level 0*/
/***********************************************************/
main ()
{ enum treasures object = gold;
if (getobject(object))
{
printf (“Congratulations you’ve found the gold!\n”);
}
else
{
printf (“Too bad — you just missed your big chance”);
}
}
/* Tiny Adventure! */

228
Chapter 23: More on data types
/***********************************************************/
/* Level 1*/
/***********************************************************/
getobject (ob)
enum treasures ob;
{ enum answer
{
no = false,
yes = true
};
if (ob == gold)
{
printf (“Pick up object? Y/N\n”);
switch (getchar())
{
case ’y’ :
case ’Y’ : return ((int) yes);/* true and false */
default : return ((int) no);/* are integers*/
}
}
else
{
printf (“You grapple with the dirt\n”);
return (false);
}
}
/* end */
/* yes or no ? */
23.6 Suggested uses for enum
Here are some suggested uses for enum.
enum numbers
{
zero,
one,
two,
three
};
enum animals
{
cat,
dog,

void
cow,
sheep,
};
enum plants
{
grass,
roses,
cabbages,
oaktree
};
enum diseases
{
heart,
skin,
malnutrition,
circulatory
};
enum quarks
{
up,
down,
charmed,
strange,
top,
bottom,
truth,
beauty
};
229
Other suggestions: colours, names of roads or types of train.
23.7 void
void is a peculiar data type which has some debatable uses. The void
datatypes was introduced in order to make C syntactically consistent. The
main idea of void is to be able to declare functions which have no return
value. The word ‘void’ is intended in the meaning ‘empty’ rather than ‘in-
valid’. If you recall, the default is for C functions to return a value of type
int. The value returned by a function did not have to be specified could
always be discarded, so this was not a problem in practice. It did make
compiler checks more difficult however: how do you warn someone about
inconsistent return values if it is legal to ignore return values?
The ANSI solution was to introduce a new data type which was called
void for functions with no value. The word void is perhaps an unfortunate
choice, since it has several implicit meanings none of which really express
what is intended. The words ‘novalue’ or ‘notype’ would have been better
choices. A variable or function can be declared void in the following ways.

230
Chapter 23: More on data types
void function();
void variable;
void *ptr;
(void) returnvalue();
The following are true of void:
• A variable which is declared void is useless: it cannot be used in an
expression and it cannot be assigned to a value. The data type was
introduced with functions in mind but the grammar of C allows us to
define variables of this type also, even though there is no point.
• A function which is declared void has no return value and returns simply
with:
return;
• A function call can be cast (void) in order to explicitly discard a re-
turn value (though this is done by the compiler anyway). For instance,
scanf() returns the number of items it matches in the control string,
but this is usually discarded.
scanf (“%c”,&ch);
or
(void) scanf(“%c”,&ch);
Few programmers would do this since it merely clutters up programs
with irrelevant verbiage.
• A void pointer can point to to any kind of object. This means that
any pointer can be assigned to a void pointer, regardless of its type.
This is also a highly questionable feature of the ANSI draft. It replaces
the meaning of void from ‘no type or value’ to ‘no particular type’. It
allows assignments between incompatible pointer types without a cast
operator. This is also rather dubious.
23.8 volatile
volatile is a type which has been proposed in the ANSI standard. The
idea behind this type is to allow memory mapped input/output to be held
in C variables. Variables which are declared volatile will be able to have
their values altered in ways which a program does not explicitly define: that
is, by external influences such as clocks, external ports, hardware, interrupts
etc…
The volatile datatype has found another use since the arrival of mul-
tiprocessor, multithreaded operating systems. Independent processes which

const
231
share common memory could each change a variable independently. In other
words, in a multithreaded environment the value of a variable set by one
process in shared memory might be altered by another process without its
knowledge. The keyword volatile servers as a warning to the compiler that
any optimizing code it produces should not rely on caching the value of the
variable, it should always reread its value.
23.9 const
The reserved word const is used to declare data which can only be assigned
once, either because they are in ROM (for example) or because they are data
whose values must not be corrupted. Types declared const must be assigned
when they are first initialized and they exist as stored values only at compile
time:
const double pi = 3.14;
const int one = 1;
Since a constant array only exists at compile time, it can be initialized by
the compiler.
const int array[] =
{
1,
2,
3,
4
};
array[0] then has the value 1, array[1] has the value 2 … and so on. Any
attempt to assign values to const types will result in compilation errors.
It is worth comparing the const declaration to enumerated data, since
they are connected in a very simple way. The following two sets of of state-
ments are the same:
enum numbers
{
zero,
one,
two,
three,
four
};
and
const
const
const
const
zero = 0;
one = 1;
two = 2;
three = 3;

232
const four = 4;
Chapter 23: More on data types
Constant types and enumerated data are therefore just different aspects of
the same thing. Enumerated data provide a convenient way of classifying
constants, however, while the compiler keeps track of the values and types.
With const you have to keep track of constant values personally.
23.10 struct
Structures are called records in Pascal and many other languages. They
are packages of variables which are all wrapped up under a single name.
Structures are described in detail in chapter 25.
23.11 union
Unions are often grouped together with structures, but they are quite unlike
them in almost all respects. They are like general purpose storage containers,
which can hold a variety of different variable types, at different times. The
compiler makes a container which is large enough to take any of these, See
undefined [Structures and Unions], page undefined .
23.12 typedef
C allows us to define our own data types or to rename existing ones by using
a compiler directive called typedef. This statement is used as follows:
typedef type newtypename;
So, for example, we could define a type called byte, which was exactly one
byte in size by redefining the word char:
typedef unsigned char byte;
The compiler type checking facilities then treat byte as a new type which
can be used to declare variables:
byte variable, function();
The typedef statement may be written inside functions or in the global white
space of a program.
/**************************************************/
/* Program*/
/**************************************************/
typedef int newname1;
main ()

Questions
233
{
typedef char newname2;
}
This program will compile and run (though it will not do very much).
It is not very often that you want to rename existing types in the way
shown above. The most important use for typedef is in conjunction with
structures and unions. Structures and unions can, by their very definition,
be all kinds of shape and size and their names can become long and tedious
to declare. typedef makes dealing with these simple because it means that
the user can define a structure or union with a simple typename.
23.13 Questions
1. Is FILE a reserved word? If so why is it in upper case?
2. Write a statement which declares a file pointer called fp.
3. Enumerated data are given values by the compiler so that it can do
arithmetic with them. True or false?
4. Does void do anything which C cannot already do without this type?
5. What type might a timer device be declared if it were to be called by a
variable name?
6. Write a statement which declares a new type “real” to be like the usual
type “double”.
7. Variables declared const can be of any type. True or false?

234
Chapter 23: More on data types

Bit Patterns
235
24 Machine Level Operations
Bits and Bytes. Flags/messages. Shifting.
Down in the depths of your computer, below even the operating system
are bits of memory. These days we are used to working at such a high
level that it is easy to forget them. Bits (or binary digits) are the lowest
level software objects in a computer: there is nothing more primitive. For
precisely this reason, it is rare for high level languages to even acknowledge
the existence of bits, let alone manipulate them. Manipulating bit patterns is
usually the preserve of assembly language programmers. C, however, is quite
different from most other high level languages in that it allows a programmer
full access to bits and even provides high level operators for manipulating
them.
Since this book is an introductory text, we shall treat bit operations only
superficially. Many of the facilities which are available for bit operations
need not concern the majority of programs at all. This section concerns the
main uses of bit operations for high level programs and it assumes a certain
amount of knowledge about programming at the low level. You may wish to
consult a book on assembly language programming to learn about low level
memory operations, in more detail.
24.1 Bit Patterns
All computer data, of any type, are bit patterns. The only difference between
a string and a floating point variable is the way in which we choose to
interpret the patterns of bits in a computer’s memory. For the most part,
it is quite unnecessary to think of computer data as bit patterns; systems
programmers, on the other hand, frequently find that they need to handle
bits directly in order to make efficient use of memory when using flags. A
flag is a message which is either one thing or the other: in system terms, the
flag is said to be ‘on’ or ‘off’ or alternatively set or cleared. The usual place
to find flags is in a status register of a CPU (central processor unit) or in a
pseudo-register (this is a status register for an imaginary processor, which
is held in memory). A status register is a group of bits (a byte perhaps) in
which each bit signifies something special. In an ordinary byte of data, bits
are grouped together and are interpreted to have a collective meaning; in a
status register they are thought of as being independent. Programmers are
interested to know about the contents of bits in these registers, perhaps to
find out what happened in a program after some special operation is carried
out. Other uses for bit patterns are listed below here:
• Messages sent between devices in a complex operating environment use
bits for efficiency.
• Serially transmitted data.

 

 

 

 

Will get back to you soon. :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s