C was not written as a teaching aid. This new edition is written in Texinfo. The book was originally published by Dabs Press. but with enough of the details so as not be outgrown as the years go by. Sceptics have said that it is a language in which everything which can go wrong does go wrong. then you will not be disappointed. It presumes that you have some previous aquaintance with programming -.but you do not need much experience. using either the Emacs Info reader. think about what you already know about programming and what you expect to look for in C.Preface
Every program is limited by the language which is used to write it. If you want to plan your assault on C. C is a programmer's language. You will most likely find all of those things and more. or a World Wide Web browser. Up:Top
Preface
3
. It is not essential to follow the order of the chapters rigorously. most languages have basically the same kinds of features: variables. ways of making loops. True. printed book. The examples programs range from quick one-function programs. ways of accessing files etc. this new edition was updated and rewritten in 1999. This book is a tutorial. Next:Reserved words & example. Its aim is to teach C to a beginner. Unlike BASIC or Pascal. which do no more than illustrate the sole use of one simple feature. the standalone Info reader. When it comes down to it. Node:Introduction. Since the book has gone out of print. ways of making decisions. You can read this tutorial online. 1999
This book was first written in 1987. as per the original contract. as you work though the chapters. C is ideally suited to modern computers and modern programming. to complete application examples occupying several pages. David Atherton of Dabs and I agreed to release the manuscript. In places these examples make use of features before they have properly been explained. Previous:Preface. but if you are a beginner to C it is recommended. or you can read this same text as a typeset.
Mark Burgess. but also it does not hold anything back. which is a documentation system that uses a single source file to produce both on-line information and printed output. C is a computer language and a programming tool which has grown popular because programmers like it! It is a tricky language but a masterful one.you need to know what a variable is and what a function is -. but as an implementation language. These programs serve as a taster of what is to come. If you have come to C in the hope of finding a powerful language for writing everyday computer programs. it does not do much hand holding. 1987.

which can be given to the computer without us needing to see what happens inside them . programming books often ignore an important role of high level languages: high level programs are not only a way to express instructions to the computer.) At a high level a computer becomes a group of black boxes which can then be thought of as the basic components of the computer. A higher level describes the same object. Before C. Imagine stepping back from the complexity of the machine level pieces and grouping together parts which work together. bolts. they are also a means of communication among Introduction 4
. C. pistons can be grouped together to make up a new basic object: an engine. Next:Basic ideas. C is one of a large number of high level languages which can be used for general purpose programming. (For instance. A computer falls definitely into the category of complex objects and it can be thought of as working at many different levels. Up:Introduction
High Levels and Low Levels
Any kind of object that is sufficiently complicated can be thought of as having levels of detail. on the other hand. As you might expect. The terms low level and high level are often used to describe these onion-layers of complexity in computers. the amount of detail we see depends upon how closely we scrutinize it. It is therefore the job of a high level language to provide a new set of black box instructions.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. in a car. The aim of any high level computer language is to provide an easy and natural way of giving a programme of instructions to a computer (a computer program). C is called a high level. Low level is perhaps the easiest to understand: it describes a level of detail which is buried down amongst the working parts of the machine: the low level is the level at which the computer seems most primitive and machine-like. a group of nuts. they did not not allow programmers to use all the facilities which the machine has to offer. to such an extent that the languages become inflexible: in other words. but with the detail left out. It is unusual in several ways. high level languages were criticized by machine code programmers because they shielded the user from the working details of the computer. The language of the raw computer is a stream of numbers called machine code. with their black box approach. 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. the action which results from a single machine code instruction is very primitive and many thousands of them are required to make a program which does anything substantial. compiler language. Previous:Introduction.Introduction
What is C? What is it for? Why is it special? • Levels: • Basic ideas: • The compiler: • Errors: • Use of Upper and Lower Case: • Questions 1: Node:Levels. that is. then covering up all the details. anything from writing small programs for personal amusement to writing complex applications.

personally. but a very powerful freedom which. it has a set of very flexible loop constructions (for. 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 differs in a number of ways.C Programming Tutorial human beings.
High Levels and Low Levels
5
. C tries to make the best of a computer by linking as closely as possible to the local environment. What can go wrong. we shall try to develop a style which finds the right balance. first of all. It is hoped that this will give a flavour of the kind of programming which C encourages. It does mean however that there are aspects of C which can run away with themselves unless some care is taken. The C language has been equipped with features that allow programs to be organized in an easy and logical way. Another unusual feature of C is the way it can express ideas concisely. 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 computer has to offer. These provide an excellent basis for controlling the flow of programs. Clearly no language can guarantee intrinsically good programs: there is always a responsibility on the programmer. Because this side of programming is so often presumed to be understood. while. Using a compiler language is not the same as using an interpreted language like BASIC or a GNU shell. when used with caution. opens up possibilities enormously. 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. To begin with. C allows things which are disallowed in other languages: this is no defect. This sounds. do) and neat ways of making decisions. This file is called an object file or executable file. The programmer carries an extra responsibility to write a careful and thoughtful program. Above all it is flexible. The aim of this book is to convey some of the C philosophy in a practical 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. They are not merely monologues to the machine. It is not possible to execute this file directly. the program is written in the form of a number of text files using a screen editor. Up:Introduction
Basic ideas about C
What to do with a compiler. The richness of a language shapes what it can talk about. to ensure that a program is neat. This is vitally important for writing lengthy programs because complex problems are only manageable with a clear organization and program structure. either like a great bonus or something a bit suspect. The executable file is said to have been compiled from the source text. efficient programs can be produced. The reward for this care is that fast. a C program has to be created in two stages: • Firstly. logical and well organized. Previous:Levels. This form of the program is called the source program. 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. C gives us the apparatus to build neat and compact programs. Next:The compiler. but it can give a framework in which it is easy to do so. Node:Basic ideas. • Secondly. they are a way to express ideas and a way to solve problems.

Up:Introduction
The Compiler
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. this approach provides a flexible way of compiling programs which are split into many files.c. write
gcc -o myprog myprog. Traditionally this is an executable program called cc for C Compiler:
cc filename gcc filename
On GNU systems. To avoid the irritation of typing two or three separate commands (which are often cumbersome) you will normally find a simple interface for executing compiler. A separate object file is built for each separate source file. these two stages are run with the command gcc -c. to create a program called myprog from a file called myprog. To tell the compiler what you would like the executable program to be called.o files. the compiler can fail. A two-phase compiler works in the following way: • Phase 1 scans a source program. In the GNU C compiler. • The compiler: • Errors: Node:The compiler. Even at this stage. 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. This program appends standard library code to the object file so that the code is complete and can "stand alone". As we shall see later. perhaps generating an intermediate code (quadruples or pcode) which helps to simplify the grammar of the language for subsequent processing.C Programming Tutorial Compiler languages do not usually contain their own editor. Up:Introduction
Basic ideas about C
6
. this results in the creation of an executable program with the default name a. Next:Errors. A compiler usually operates in two or more phases (and each phase may have stages within it). if it finds that it has a reference to a function which does not exist.c
Node:Errors. A C compiler linker suffers the slightly arduous task of linking together all the functions in the C program. Previous:Basic ideas.out. the output is one or more . Previous:The compiler. • Phase 2 is a Linker. It then converts the intermediate code into a file of object code (though this is usually not executable yet). use the -o option for setting the name of the object code:
gcc -o program-name filname
For example. With the GNU C compiler this stage is activated by the command gcc -o or ld. nor do they have words like RUN with which to execute a finished program. These phases must be executed one after the other. Next:Use of Upper and Lower Case.

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. in a program instead of y = sin (x). even through the careless use of upper and lower case characters. Up:Introduction
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. Upon compilation. look for the first error. If the compilation of a program is successful. the program. just look for and fix the first error. Node:Use of Upper and Lower Case. As a rule. The situation thus looks much worse than it really is. following a single true culprit. which assigns the value of the sin of x to y. Previous:Errors. There are different kinds of error: Syntax Errors in the syntax. you can jump directly to the error. This file will contain machine code which can be executed according to the rules of the computer's local operating system. Unlike languages such as Pascal and some versions of BASIC. suppose you write sin (x) y = . must then be recompiled.) A program with syntax errors will cause a compiler program to stop trying to generate machine code and will not create an executable. Of course. They are listed all in one go.C Programming Tutorial
Errors
Errors are mistakes which we the programmers make. after you have become experienced. but it can be very disheartening. the C compiler distinguishes between small letters and capital letters. you would see this error message:
eg. with the line number. then a new file is created. or the salient parts. you send a letter to all drivers whose licenses will expire sometime. in the text file.c: In function `main': eg. instead. You intend to send a letter to all drivers whose licenses will expire soon. You'll get used to this with experience. This is a potential source of quite trivial errors which can be difficult Errors 7
. and it is common to submit a program for compilation only to receive a long and ungratifying list of errors from the compiler. or word structure of a program are caught before you run it. at compilation time by the compiler program. Next:Questions 1. and then recompile.c:12: parse error before `y'
(If you compile the program in Emacs. at which the error occurred and a message to say what was wrong. but does not do what you intend it to do. When a programmer wants to make alterations and corrections to a C program. For example. However. these have to be made in the source text file itself using an editor. fix that. But at the beginning. 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. The C language is case dependent. you will recognize when subsequent error messages are due to independent problems and when they are due to a cascade. Intention Errors in goal or purpose (logical errors) occur when you write a program that works..

and secondly.C Programming Tutorial to spot. Node:Questions 1. This is called declaring variables. It serves two purposes: firstly. Up:Top
Use of Upper and Lower Case
8
. Are upper and lower case equivalent in C? 5. If a letter is typed in the wrong case. How is a C program compiled usually? 4. Previous:Use of Upper and Lower Case.
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. it provides the compiler with a definitive list of the variables. Previous:Introduction. enabling it to cross check for errors. What the two different kinds of error which can be in a program? Node:Reserved words & example. the compiler will complain and it will not produce an executable program. Next:Operating systems. What is a compiler? 2. you will see a compilation error. If you fail to declare a variable. or declare it to be the wrong type. Consequently. How is a C program run? 3. the type of a variable is of great importance to the compiler. C supports a variety of variable types (variables which hold different kinds of data) and allows one type to be converted into another. Up:Introduction
Questions
1. it informs the compiler how much space must be reserved for each variable when the program is run.

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.. Libraries provide frequently used functionality and.someinteger). in practice. effectively.
Text is easy. To print out an integer. Previous:printf. provides standard facilities for input to and output from a program. but we also want to be able to print out the contents of variables. Words used in included libaries are also. but we'll need it in many places before that. such as main. at least one library must be included in every program: the so-called C library. These words may not be used in just any old way: C demands that they are used only for giving commands or making statements. The basic instructions are built up using a reserved set of words. Next:Output 1. there will be a conflict between your choice and the library. While the features provided by libraries are not strictly a part of the C language itself. the control sequence %d is used:
printf ("Integer = %d". if. compile and execute it. and int. An attempt to do so will result in a compilation error. See All the Reserved Words. For example. double. they are essential and you will never find a version of C without them. for example.
The variable someinteger is printed instead of %d.. which is part of the C library. It provides an superbly versatile way of printing text. the stdio library."). • printf: • Example 1: • Output 1: • Questions 2: Node:printf. default. Up:Reserved words & example
The printf() function
One invaluable function provided by the standard input/output library is called printf or `print-formatted'. If you use a word which has already been adopted in a library.. extern. Next:Example 1. you can copy this to a file. to name just a few. its functions are defined and you cannot use their names. The printf function is described in full detail in the relevant chapter. After a library has been included in a program. Node:Example 1. as the name of a variable. for. In fact. 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. most of the facilities which C offers are provided as libraries that are included in programs as plug-in expansion units. for.while. Previous:Reserved words & example. You cannot use default.some string. reserved. Up:Reserved words & example Reserved words and an example 9
. If you are reading this in Info. for a complete list of the reserverd words. The simplest way to use it is to print out a literal string:
printf (". The example program below is a complete program. of standard functions.

(This is a slightly superficial view). This is a layer of software which drives the hardware and provides users with a comfortable environment in which to work. Previous:Operating systems.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. they are `devices'. though more primitive. The corresponding route for output is called "stdout" or standard output file and is usually a monitor screen. but C is an exception. Operating systems vary widely but most have a command language or shell which can be used to type in commands. but devices are represented by files with Operating systems and environments 11
. (it is not possible to re-read what has been sent to the monitor". Up:Operating systems
Files and Devices
The filing system is also a part of input/output.). The keyboard and the monitor screen are not really files. 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!). unchanging part of the C language. or write to the keyboard. In C the link between these two is very efficient. C does this implicitly (it comes from Unix). Input and output are not actually defined as a fixed. Next:Filenames. but which are specially adapted to your system. time-sharing principle and were incapable of interactive communication with the user. An operating system has two main components which are of interest to users: a user interface (often a command language) and a filing system. GUIs are good for carrying out simple procedures like editing.) The library is standard in the sense that C has developed a set of functions which all computers and operating systems must implement. because of its unique design. 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. whether it be to a screen or to files on a disk. of course. Both of these are parts of stdio or standard input output. In the network version of this book we shall concentrate on Unix shell commands since they are the most important to programmers. Recently the tendency has been to try to eliminate typing completely by providing graphical user interfaces (GUIs) for every purpose. Many compiler languages still have this inadequacy when carried over to modern computers. This file is called a standard C library. The operating system is the route to all input and output. they were intended to be run on large mainframe computers which operated on a multi-user. (See the next chapter for more information. On microcomputers command languages are usually very similar in concept. but they are not well suited to giving complicated instructions to a computer. The file from which C normally gets its input from is called stdin or standard input file and it is usually the keyboard. • Files devices: • Filenames: • Command languages: • Questions 3: Node:Files devices. 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. with only slightly different words for essentially the same commands. When most compiler languages were developed. For that one needs a command language.

Next:Command languages. it is exactly the same to read or write from a device as it is to read or write from a file. the printer might be called "PRN" or "PRT". when linked to libraries is called filename on Unix-like operating systems. To execute the compiler you type.. For instance. so that the keyboard is treated as a read-only file. Up:Operating systems
Filenames
The compiler uses a special convention for the file names. so that we do not confuse their contents. The dotted endings mean that the compiler can generate an executable file with the same name as the original source just a different ending.g. and can be written to or read from. You might have to open it explicitly as 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. Header files are always called libname. Next:Questions 3. The compiler generates a file of object code from this called filename. e.h. the monitor as a write only file.
cc filename
For example. as yet unlinked. The advantage of treating devices like this is that it is not necessary to know how a particular device works.EXE on Windows derived systems.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. This means the instructions which can be given to the system itself rather than the words which make up a C program. The libraries themselves are also files of object code.c.so. connected to the computer. When input is taken solely from the keyboard and output is always to the screen then these details can just be forgotten. and filename. The quad file and the object file are only working files and should be deleted by the compiler at the end of compilation.a or liblibraryname. only that it exists somewhere.
ls -l less filename emacs filename
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. The .. The endings `dot something' (called file extensions) identify the contents of files for the compiler. The final program. The name of a source program (the code which you write) is filename. typically called liblibraryname. Previous:Filenames.
cc foo. Previous:Files devices. Fortunately it is possible to get by with knowing just handful of the most common ones and Files and Devices 12
. Node:Filenames.o.C Programming Tutorial special names. In other words. Up:Operating systems
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.c
Node:Command languages.

Up:Operating systems
Questions
1.C Programming Tutorial having the system manual around to leaf through when necessary. 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. Previous:Operating systems. Next:Programming style. Previous:Command languages. Commonly this will involve two simultaneous key presses. Node:Questions 3. CTRL Z or CTRL-D etc. such as CTRL C. What would be the name of the file produced by the compiler of the program in 3? 5. In GNU/Linux. Another important object is the `panic button' or program interruption key. How would this program be run? Node:Libraries. Up:Top
Command Languages and Consoles
13
. CTRL-C is used. What is an operating system for? 2. Every system will have its own way of halting or terminating the operation of a program or the execution of a command.

h. The most commonly used header file is the standard input/output library which is called stdio.c -lm -lsocket
and so on. For example. Header files for libraries are included by adding to the source code:
#include header. if they weren't needed. Each library comes with a number of associated header files which make the functions easier to use. Or Libraries 14
. there are libraries of mathematical functions. string handling functions and input/output functions and graphics libraries. The core of the C language is small and simple.a. 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.h header file belongs to the mathematics library libm.c -lm
when you compile the program. We shall return to this later. say. If we wanted to add in the socket library libsocket. data types and external data to be used in conjunction with the libraries.h
at the top of a program file. making the resulting object code often much longer. we would type
cc -o program_name prog. but to do so you need to know how your operating system builds libraries. 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. This is what makes C so portable.a you would type
cc -o program_name prog. For instance:
#include "myheader. You can also make your own. For example. Some libraries are provided for you. to merge with the math library libm. Special functionality is provided in the form of libraries of ready-made functions. giving you access to many special abilities without needing to reinvent the wheel. the appropriate library code is included by the compiler. It is up to every programmer to make sure that libraries are added at compilation time by typing an optional string to the compiler. When library functions are used in programs. Once a header file has been included.a to do some network programming as well. The -lm means: add in libm. Libraries are supplemented by header files which define macros.Libraries
Plug-in C expansions. Header files. Libraries are files of ready-compiled code which we can merge with a C program at compilation time.h"
includes a personal header file which is in the current directory. The math. it has effectively added to the list of reserved words and commands in the language. This belongs to a subset of the standard C library which deals with file handling.

since every library adds to the size of both source and object code. so many libraries would slow them down. which is called libc.h>
includes a file which lies in a standard directory like /usr/include. printf ("Maths library ready").C Programming Tutorial
#include <stdio. Some operating systems are smarter than others when running programs and can load in only what they need of the large libraries. Up:Libraries
Libraries
15
. }
A program wishing to use a mathematical function such as cos would need to include a mathematics library header file. there may be a practical limit: namely memory. in principle.
#include <stdio. }
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. • Questions 4: Node:Questions 4. Libraries also add to the time it takes to compile a program. Others have to load in everything before they can run a program at all. The #include directive is actually a command to the C preprocessor.y.h> main () { printf ("C standard I/O file is included\n"). printf ("Hello world!"). See Preprocessor.h> main () { double x. These details will be found in the local manual for a particular C compiler or operating system. To know what names libraries have in a particular operating system you have to search through its documentation. Previous:Libraries. Although there is no limit. for example. y = sin (x). which is dealt with more fully later. 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.
#include <stdio. to the number of libraries which can be included in a program. Unix users are lucky in having an online manual which is better than most written ones.h> #include <math.

Previous:Libraries. How is a library file incorporated into a C program? 2. What is another name for a library file? Node:Programming style.C Programming Tutorial
Questions
1. 3. Next:Form of a C program. Up:Top
Questions
16
. Name the most common library file in C. Is it possible to define new functions with the same names as standard library functions? 4.

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

A program is made up of functions. In some languages. The form is the same everywhere in a program. No such thing is The form of a C program 18
. This function is always the starting point of a C program. Only the operating system can call the function main(): this is how a C program is started. words like CALL or PROC are used. One and only one of these functions in the program must have the name main(). Also. 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(). functions are made up of statements and declarations surrounded by curly braces { }. or even a symbol like &. This is all that is required to transfer control to the new function. the function main() cannot be called from any other function in the 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. written in some arbitrary order.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. Every C program is a collection of one or more functions. Firstly the function do_nothing() is called by typing its name followed by the characteristic () brackets and a semi-colon. This is how C distinguishes functions from ordinary variables. } /******************************************************/ do_nothing() { } /* Function called */ /* Main program */
The program now consists of two functions. The basic building block in a C program is the function. one of which is called by the other. whether it is the form of the main program or of a subroutine. It always starts where main() is. 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. There are several new things to notice about this program.

Here are some more basic elements which we shall cover. 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. When a program is compiled. All instructions in C must end with a semi-colon. helps to show how the elements of a C program relate.b). a=random(). When the program meets the closing brace } it then transfers back to main() where it meets another } brace and the program ends..C Programming Tutorial needed in C.h> #include <myfile. 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.. b=function1(). The following chapters will then expand upon this as a kind of basic plan. The semi-colon is vital however. shown below.
/****************************************************/ /* */ /* Skeleton program plan */ /* */ /****************************************************/ #include <stdio. } /****************************************************/ function1 () { . The function main() is treated just as any other function. } /****************************************************/ /* Purpose */ /* Main program & start */ /* declaration */
The form of a C program
19
. The `brace' characters { and } mark out a block into which instructions are written. function2(a. The examples above are obviously very simple but they illustrate how control flows in a C program. All functions have the same status as far as a program is concerned. This helps the compiler diagnose errors. This is the simplest way in which control flows between functions in C.. • comments • preprocessor commands • functions • declarations • variables • statements The skeleton plan of a program.b.c> #define SCREAM #define NUMBER_OF_BONES /* Preprocessor defns */
"arghhhhh" 123
/****************************************************/ main () { int a.

b) int a. Node:Question 5. Next:Functions. What is a block? 2.. Up:Form of a C program
Questions
1.. Previous:Form of a C program. Up:Top
Questions
20
.. What vital piece of punctuation goes at the end of every simple C statement? Node:Comments.b. Previous:Form of a C program. } /* Purpose */
• Question 5: 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. What happens when a program comes to a } character? What does this character signify? 5. Does a C program start at the beginning? (Where is the beginning?) 4.C Programming Tutorial
function2 (a. { . Name the six basic things which make up a C program. 3.

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. Previous:Comments. Up:Comments
Example 2
#include <stdio... comment ..h> #define NOTFINISHED /* header file */ 0
/**********************************************/
Comments
21
.. Comments are marked out or delimited by the following pairs of characters:
/* . This is because comments are stripped out of a source program by the compiler when it converts the source program into machine code. even in the middle of a statement.... Previous:Example comment. You should try to minimize the use of comments in a program while trying to maximize the readability of the program.. Programs can contain any number of comments without losing speed. Next:Question 7.. Next:Example comment 2.Comments
Annotating programs. */ } has no effect */ has none */ went all the way down */ /* The almost trivial program */
Node:Example comment 2. If there are too many comments you obscure your code and it is the code which is the main message in a program. • Example comment: • Example comment 2: • Question 7: Node:Example comment. Up:Comments
Example 1
main () { /* This little line /* This little line /* This little line to the next line /* And so on ..*/
Because a comment is skipped over as though it were a single space.. it can be placed anywhere where spaces are valid characters.. though this is not to be encouraged.

Next:Variables. Previous:Example comment 2.. } /* declarations */
Node:Question 7. to start but forgets the . do { /* Nothing !!! */ } while (NOTFINISHED).*/ to close? Node:Functions. What happens if a comment is not ended? That is if the programmer types /* . Up:Top
Example 2
22
. Up:Comments
Question
1..C Programming Tutorial
/* A bar like the one above can be used to */ /* separate functions visibly in a program */ main () { int i. Previous:Comments.

.y. . }
You will probably see both styles in C programs.. d = sqrt(x*x+y*y+z*z). double z) { double d. 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'. the same function is written slightly differently:
Pythagoras(double x.z) double x. Getting results. in Kernighan & Ritchie C they are always written in the following form:
identifier (parameter1. }
In the newer ANSI standard... { double d. printf("The distance to your point was %f\n"... Making functions is a way of isolating one block of code from other independent blocks of code... 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
Making black boxes.
Functions
23
. d = sqrt(x*x+y*y+z*z).. Solving problems.d). A function is a module or block of program code which deals with a particular task.. Functions help us to organize a program in a simple way.d). . printf("The distance to your point was %f\n". Functions serve two purposes..) types of parameters { variable declarations statements.parameter2.z.y. double y. }
For example
Pythagoras(x.

2). with a "%d" to indicate that it is printing a integer. Add_Two_Numbers (c. }
Functions
24
. c = a + b.b. } /* Add a and b */
Notice the position of the function name and where braces and semi-colons are placed: they are crucial.. from the the standard library.c). and consists of a number of statements and declarations. Here is a real example function which adds together two integer numbers a and b and prints the result c.. c = 1. A function can accept a number of parameters or values which pass information from outside. 9 (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.C Programming Tutorial Each function has a name or identifier by which is used to refer to it in a program. This function is not much use standing alone. The name of a function in C can be anything from a single letter to a long word. Add_Two_Numbers (1. enclosed by curly braces { }.d. The details are quickly learned with practice and experience. which make up the doing part of the object.. All the variables are chosen to be integers to keep things simple and the result is printed out using the print-formatted function printf. z (any letter from a to z) A .b) int a. d = 53. 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 .e.d). control is passed to the function) by using its name with the usual brackets () to follow it. It has to be called from somewhere. A function is called (i. along with the values which are to be passed to the function:
main () { int c.
Add_Two_Numbers (a. Z (any letter from A to Z) 0 . { int c. The declarations and `type of parameter' statements are formalities which will be described in good time. printf ("%d".

Where would you place the printf function in this hierarchy? Node:Program listing. Next:Functions with values. The idea is to illustrate the way in which the functions connect together: • Structure diagram: • Program listing: • Functions with values: • Breaking out early: • The exit function: • Functions and types: • Questions 6: Node:Structure diagram. Some functions call themselves. DownOne (). Previous:Structure diagram. Up:Functions
Program Listing
/***********************************************/ /* */ /* Function Snakes & Ladders */ /* */ /***********************************************/ #include <stdio. while others can be called from anywhere in a program.C Programming Tutorial The result of this program would be to print out the number 54 and then the number 3 and then stop. Next:Program listing. Here is a simple program which makes use of some functions in a playful way. Previous:Functions.h> /***********************************************/ /* Level 0 */ /***********************************************/ main () { printf ("This is level 0: the main program\n"). Up:Functions
Structure diagram
Level 0: main () | Level 1: DownOne () / / Level 2: DownLeft() \ \ DownRight()
Note: not all functions fit into a tidy hierarchy like these. The structure diagram shows how this can be visualized and the significance of the program `levels'. printf ("About to go down a level \n").
Structure diagram
25
.

printf ("On the left branch of the picture\n").C Programming Tutorial
printf ("Back at the end of the start!!\n"). Previous:Program listing. DownLeft (2). } /************************************************/ /* Level 2 */ /************************************************/ DownLeft (a) int a. Take the following example:
bill = CalculateBill(data. } /* Right branch */ /* Left branch */
Node:Functions with values. all is well\n").. It is possible to make a function hand back a value to the place at which it was called.. } /************************************************/ DownRight (a) int a.\n"). printf ("Through level 1. printf ("Going up!!"). printf ("Going back up a level!\n").a)... Up:Functions
Functions with values
In other languages and in mathematics a function is understood to be something which produces a value or a number. That is.). { printf ("This is deepest level %d\n". In C it is possible to choose whether or not a function will have a value. DownRight (2). { printf ("And level %d again!\n"..a). } /************************************************/ /* Level 1 */ /************************************************/ DownOne () /* Branch out! */
{ printf ("Down here at level 1. the whole function is thought of as having a value. Next:Breaking out early.
The variable bill is assigned to a function CalculateBill() and data are some data which are passed Program Listing 26
.

dessert) int starter. Forgetting a return statement can ruin a program. when it is done. Functions which work in this way are said to return a value. The value of the function is assigned to "bill" and the program continues. Up:Functions
Functions with values
27
. Next:The exit function. } /* Adds up values */
As soon as the return statement is met CalculateBill() stops executing and assigns the value total to the function. Previous:Functions with values. If there were no return statement 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.dessert.3).dessert) int starter.main. 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 () { CalculateBill (1.C Programming Tutorial to the function. When this statement is executed in a program. This statement makes it look as though CalculateBill() is a number. total = starter + main + dessert. a value which has not been returned cannot be used safely. or even no value at all. } /* WRONG! */
then the value bill would just be garbage (no predictable value). So a function in C can return a value but it does not have to be used. presuming that the compiler allowed this to be written at all. { int total. This is usually what is done with the input output functions printf() and scanf() which actually return values. quite legitimately. (See next chapter) Node:Breaking out early. { int total. NOTE : Functions do not have to return integers: you can decide whether they should return a different data type. this function will then hand control back. control will be passed to the function CalculateBill() and. }
then the value of the function would just be discarded. return (total).2.main. total = starter + main + dessert. Consider the function CalculateBill() from the statement above:
CalculateBill(starter. returning a value is a simple matter.main. In C.dessert. For instance if CalculateBill had just been:
CalculateBill (starter.main. on the other hand.

c).b) int a.C Programming Tutorial
Breaking out early
Suppose that a program is in the middle of some awkward process in a function which is not main(). The function is entered with some values for a and b and. This is where the beauty of the return statement becomes clear.
myfunction (a. Next:Functions and types. In that loop. Up:Functions
Functions and Types
All the variables and values used up to now have been integers. is a single if statement and a statement which increases a by one on each loop. 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 }. 0
This function also calls a number of other functions which perform tidy-up duties such as closing open files etc. This is called with a return code. like this:
#define CODE exit (CODE). If a becomes bigger than b at any point the return(b) statement gets executed and the function myfunction quits. 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. Next:Questions 6. and suddenly the function finds its answer. } a = a + 1.b. perhaps two or three loops working together. Node:The exit function. Up:Functions
The exit() function
The function called exit() can be used to terminate a program at any point. { while (a < b) { if (a > b) { return (b). for example. without having to arrive at the end brace }. it starts to execute one of C's loops called while. } } /* breaking out of functions early */
The example shows this. Node:Functions and types. Previous:Breaking out early. and passes the value of b back to the place it was called. assuming that a is less than b. Previous:The exit function.
Breaking out early
28
.b. no matter how many levels of function calls have been made.

C Programming Tutorial can only make sense if the variable bill and the value of the function CalculateBill() are the same kind of object: in other words if CalculatBill() returns a floating point number. What happens if a function returns a value but it is not assigned to anything? 4. Next:Parameters. So far no declarations have been needed because C assumes that all values are integers unless you specifically choose something different. Previous:Functions and types. Up:Functions
Questions
1. How can a function be made to quit early? Node:Variables. In fact this is done by declaring functions to return a particular type of data. 2. Declarations are covered in the next section. Previous:Functions. What happens if a function is assigned to an object but that function returns no value? 5. Write a function which takes two values a and b and returns the value of (a*b). Up:Top
Functions and Types
29
. then bill cannot be a character! Both sides of an assignment must match. Node:Questions 6. Is there anything wrong with a function which returns no value? 3.

.g. A variable is a seqeuence of program code with a name (also called its identifier). Z (any letter from A to Z) 0 . Types and Declarations 30
.Variables. No such convention exists in C.
In C variables do not only have names: they also have types. like PL/1. 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 .. Declaring data. 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. A name or identifier in C can be anything from a single letter to a word.. The names of these types are all reserved words in C and they are summarized as follows: char A single ASCII character short A short integer (usually 16-bits) short int A short integer int A standard integer (usually 32-bits) long Variables. • 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. integer only or floating point or none). z (any letter from a to z) A . There is a lot of different possible types in C. 9 (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 BASIC and in some older. Descriminating types.g. largely obsolete languages. but there is no need to do this right away: there are some basic types which are provided by C ready for use. e. Types and Declarations
Storing data. 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. The type of a variable conveys to the the compiler what sort of data will be stored in it. In fact it is possible for us to define our own.. Instead we specify the types of variables in their declarations. a special naming convention is used to determine the sort of data which can be held in particular variables..

whereas on others they are not. enum Discussed in a later chapter.fred.. • Declarations: • Where to declare things: • Declarations and Initialization: • Types: • Choosing Variables: • Assigning variables to one another: • Types and The Cast Operator: • Storage class register static and extern: • Functions types: • Questionsdeclare: Node:Declarations. On some systems variables are signed by default.. so that if no minus sign is present..
Declarations
31
. volatile Discussed in a later chapter.. the word unsigned can also be placed in front of any of these types.
For example:
int i. Previous:Variables. unsigned long int Name_of_Variable. Up:Variables
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.j. Unsigned means that only positive or zero values can be used. In addition to the above. Next:Where to declare things.. The ANSI standard also allows the word signed to be placed in front of any of these types.e. there is no minus sign). (i.variablenameN. double x. larger numbers can be stored in the same kind of variable.z. The advantage of using this kind of variable is that storing a minus sign takes up some memory. but increasingly 64 bits) float A floating point or real number (short) long float a long floating point number double A long floating point number void Discussed in a later chapter. so indicate the opposite of unsigned.y.C Programming Tutorial A long integer long int A long integer (usually 32-bits. char ch. There is some repetition in these words..

float global_floating_point. for example. /* .h> int globalinteger..)
#include <stdio. /* . */ }
Where to declare things
32
. (After the #include lines. */ while (i < 10) { char ch.. 1.) Variables declared here are called global variables.
main () { int a. The other place where declarations can be made is following the opening brace. See Scope. Any block will do. float x. Next:Declarations and Initialization.z. Variables of this kind only work inside their braces {} and are often called local variables. Node:Where to declare things. main () { } /* Here! outside {} */
2. /* statements */ }
or
function () { int i..y. There are also called static and external variables in special cases. one for each use of the undeclared variable. in the space between function definitions. Another name for them is automatic variables. Previous:Declarations. For now it will do to simply state what these places are. int g.. of a block. Up:Variables
Where to declare things
There are two kinds of place in which declarations can be made. A compiler is markedly more efficient than a customs officer: it will catch a missing declaration every time and will terminate a compiling session whilst complaining bitterly.C Programming Tutorial Failing to declare a variable is more risky than passing through customs and failing to declare your six tonnes of Swiss chocolate.. One place is outside all of the functions. as long as the declaration follows immediately after the opening brace. often with a host of messages. {}. That is.

Up:Types
char
A character type is a variable which can store a single ASCII character. Next:Example special chars. if ch is the name of a character:
char ch. Groups of char form strings. "string") For instance. Previous:Types. then it is better to initialize separately. nor (ideally) to the final code whether the first or second method is used. i = 0. ch = 'a'.g. In C single characters are written enclosed by single quotes. 'c'! (This is in contrast to strings of many characters which use double quotes.
This is called initialization of the variables. but it is not always desirable to do so.g.C Programming Tutorial
}
Node:Declarations and Initialization. If there are just one or two declarations then this initialization method can make a program neat and tidy.
are equivalent to the more longwinded
int i. Up:Variables
Declarations and Initialization
When a variable is declared in C. Up:Variables
Individual Types
• char: • Example special chars: • integers: • Float: Node:char. It makes no odds to the compiler. This is no more efficient than doing it in two stages. e. Next:Types. If there are many. as in the second case. Previous:Where to declare things. Previous:Declarations and Initialization. e. the language allows a neat piece of syntax which means that variables can be declared and assigned a value in one go. A lot means when it starts to look as though there are too many. char ch = 'a'. ch = 'a'. The following:
int i = 0. Next:Choosing Variables. C always allows the programmer to write declarations/initializers in this way. Node:Types.
Declarations and Initialization
33
. It is only for tidiness that this is allowed. but it is sometimes tidier. char ch.

) Control characters i. Previous:char. See Character Conversion Table. printf (" <.Start of this line!! \r"). printable or not printable from values -128 to 127. \xddd character ddd where ddd is an ASCII code given in hexadecimal or base 16. printf ("ch = \'a\' \n").h> main () { printf ("Beep! \7 \n"). Node:Example special chars. Next:integers. Up:Types
Listing
/***************************************************/ /* */ /* Special Characters */ /* */ /***************************************************/ #include <stdio.C Programming Tutorial would give ch the value of the character a.e. }
char
34
. The characters and their meanings are: \b backspace BS \f form feed FF (also clear screen) \n new line NL (like pressing return) \r carriage return CR (cursor to start of line) \t horizontal tab HT \v vertical tab (not all versions) \" double quotes (not all versions) \' single quote character ' \\ backslash character \ \ddd character ddd where ddd is an ASCII code given in octal or base 8.
A character can be any ASCII character. (But only 0 to 127 are used. See Character Conversion Table. The same effect can also be achieved by writing:
char ch = 'a'. non printable characters are put into programs by using a backslash \ and a special character or number.

j. You should always check these values. long long and short. Previous:Example special chars. i = j = 0.g. Even different flavours of Unix can have varying sizes for these objects.C Programming Tutorial The output of this program is:
Beep! (and the BELL sound ) ch = 'a' <. Up:Types
Integers
Whole numbers
There are five integer types in C and they are called char. Previous:integers. int. int means a `normal' integer and short means a `short' one.
Node:Float. Node:integers. 64 bit operating systems are appearing and long integers are 64 bits long. long. 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
short 16 unsigned short 16 int long unsigned int long long 32 32 32 64
Increasingly though. Up:Types
Listing
35
.
or
short i=0.Start of this line!!
and the text cursor is left where the arrow points. Some mainframe operating systems are completely 64 bit. e. Variables are declared in the usual way:
int i. It is also possible to have the type:
unsigned char
This admits ASCII values from 0 to 255. Usually. the two to remember are int and short. Unicos has no 32 bit values. Next:Float. not that that tells us much. The difference between these is the size of the integer which either can hold and the amount of storage required for them. rather than -128 to 127. The sizes of these objects depend on the operating system of the computer.j=0.

Similarly names like:
double x. All the mathematical functions which C can use require double or long float arguments so it is common to use the type float for storage only of small floating point numbers and to use double elsewhere. • Choose a sensible name for the variable.) 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 +/. Up:Variables
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. Previous:Types. bignum = 2. • Decide where the variable is allowed to exist.1.C Programming Tutorial
Floating Point
There are also long and short floating point numbers in C. Next:Assigning variables to one another. A common one. smallnum = 3.10E-37 to +/. y = 2.y. • Declare that name to be a variable of the chosen type. for controlling loops for instance.
tend to make one think of floating point numbers. x = 0. (There is not particular reason why this should be.10E-307 to +/.) Other integer values should have more meaningful names.y.10E308 (ditto)
Typical declarations:
float x. It is common to give these short names (single characters). it is just common practice.j.
to be integer type variables used for counting. for instance is to use the letters:
int i.36E208.smallnum. (This not always true since the C `cast' operator allows temporary conversions to be made. Some local variables are only used temporarily. A good habit to adopt is to keep to a consistent practice when using these variables. double bignum.
Node:Choosing Variables.10E38 +/.2E-300.k.z. Floating Point 36
.z.456E5 z = 0.

b = 1. Next:Types and The Cast Operator. a = b. since the truncated value 1 can be assigned to ch.C Programming Tutorial Node:Assigning variables to one another.
is a valid statement. and:
float x = 1.
In either case the objects on either side of the = symbol must be of the same type. Previous:Choosing Variables. This is a questionable practice though. Up:Variables
Assigning variables to one another
Variables can be assigned to numbers:
var = 10.
is a valid statement. char ch.
The result of this would be:
The ASCII code of A is 65
Node:Types and The Cast Operator.i). Numerical values and characters will interconvert because characters are stored by their ASCII codes (which are integers!) Thus the following will work:
int i. Next:Storage class register static and extern. It is possible (though not usually sensible) to assign a floating point number to a character for instance. ch = x. Up:Variables
Choosing Variables
37
. Previous:Assigning variables to one another. printf ("The ASCII code of %c is %d". It is unclear why anyone would choose to do this. char ch = 'A'. i = ch.4. So
int a.
and assigned to each other:
var1 = var2.ch.

i = (int) x. This will be seen particularly with regard to structures and unions.3. int i = 12.C Programming Tutorial
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:
int i.ch). int i.i.
float x. x = 2. ch = (char) x. i = (int) x. Similarly floating point and integer types can be interconverted:
float x = 3.
The value of i would be 3 because an integer cannot represent decimal points. This is the only integer which it would make any sense to talk about in connection with the character.x. char ch. int i.
The general form of the cast operator is therefore:
(type) variable
It does not always make sense to convert types. so the cast operator rounds the number. /* Use int float and char */
Types and The Cast Operator
38
. char ch = '\n'. x = (float) i. i = (int) ch. Cast operators crop up in many areas of C. This is not the last time they will have to be explained.h> main () { float x.
/***************************************************/ /* */ /* Demo of Cast operator */ /* */ /***************************************************/ #include <stdio.
The value of the integer would be the ASCII code of the character. There is no such problem the other way around.345. printf ("From float x =%f i =%d ch =%c\n".

x = (float) i. i = (int) ch.x). ch = '*'. the function main() in file 1 can use the variable i from the function main in file 2. x = (float) ch. 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. The opposite of static is auto. 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. NOTE: Every global variable.C Programming Tutorial
i = 45. on occasion. Up:Variables
Storage class static and extern
Sometimes C programs are written in more than one text file. Another class is called static. Node:Functions types. Previous:Types and The Cast Operator. Space is allocated for static variables in the program code itself and it is never disposed of unless the whole program is.i. Previous:Storage class register static and extern. printf ("From char ch=%c i=%d x=%f\n". There are two places where this must be done:
Storage class static and extern
39
. main () { extern int i. printf ("From int i=%d x=%f ch=%c\n". } { } function ()
In this example. If the word extern is placed in front of a variable then it can be referenced across files:
File 1 File 2 int i. Next:Questionsdeclare. Next:Functions types. If you want this to be different. ch = (char) i. just as variables have to be. 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.ch). it will be necessary to get at variables which were defined in another file. defined outside functions has the type static automatically.i. }
Node:Storage class register static and extern. If this is the case then.ch. Up:Variables
Functions. then a function has to be declared to be a certain type.x.

80shillings 3. What is the difference between the types floa and double. x = function1 (). Where are these? Functions. 7.229). What is an identifier? 2. not just main(). Types and Declarations 40
.C Programming Tutorial • The name of the function must be declared a certain type where the function is declared. A$ 6. ch = function2 (). _off 3. function1 (). if the two functions above were called from main(). A% 5. What is the difference between the types int and unsigned int? 6. Ralph23 2. float x. If we want to declare a function to return long float. it must be done in. then compilation errors will result! Notice also that the function must be declared inside every function which calls it. }
• As well as declaring a function's identifier to be a certain type in the function definition. What type does a C function return by default? 8. at least. }
If a function whose type is not integer is not declared like this. 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. Say which of the following are valid C identifiers: 1. Up:Variables
Questions
1. So. 4. Previous:Functions types. e. a function which returns a float value must be declared as:
float function1 () { return (1.g. function2 (). mission_control 4. Write a statement to declare two integers called i and j. two places. }
A function which returns a character:
char function2 () { return ('*'). 5. Node:Questionsdeclare. Write a statement which assigns the value 67 to the integer variable "I". they would have to declared in the variables section as:
main () { char ch.

Next:Scope. Is it possible to have an automatic global variable? Node:Parameters.C Programming Tutorial 9. Previous:Variables. Write a statement. to print out the integer part of the number 23. using the cast operator.1256. 10. Up:Top
Questions
41
.

Next:Value parameters.Parameters and Functions
Ways in and out of functions. • Declaring parameters: • Value parameters: • Functions as actual parameters: • Example 2: • Example 3: • Variable parameters: • Example 4: • Qulakfj: Node:Declaring parameters.) types of parameters
Parameters and Functions
42
. Parameters are about communication between different functions in a program.c) int a. Variable parameters are two-way. Previous:Parameters. { int total. the value of total depends upon the starting values of a.b. the function is said to accept parameters.. by writing them inside a functions brackets like this. return total.b and c. total = a + b + c. 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..b. Up:Parameters
Declaring Parameters
A function was defined by code which looks like this:
identifier (parameters.c. }
When variable values are handed to a function. For instance the function CalculateBill accepted three values a. but they can also be used to hand information back.
CalculateBill (a. Value parameters are one-way communication carrying information into a function from somewhere outside. They provide a way of getting information into a function.b and c. In CalculateBill the "behaviour" is the addition process. In other words. Examples of this have already been seen in a limited way. They are like messengers which pass information to and from different places. Parameters are usually split into two categories: value parameters and variable parameters. Not all functions will be as simple as the ones which have been given so far. In mathematics a parameter is a variable which controls the behaviour of something. In C it is a variable which carries some special information.

Up:Parameters
Value Parameters
A value parameter is the most common kind of parameter. also have types which must be declared.x. Node:Value parameters. { }
or
char function2 (x.y.4). Previous:Declaring parameters.
#include <stdio. All of the examples up to know have been examples of value parameters. Consider a function which is called from main() whose purpose is to add together two numbers and to print out the result. return (ch2). {
Declaring Parameters
43
. float x. { char ch2 = '*'.j. 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. } /*******************************************/ add (a.b. char ch.h> main () { add (1.j.b) int a. }
Notice that they are declared outside the block braces. like variables and functions. Next:Functions as actual parameters.C Programming Tutorial
{ }
Parameters.y) int i. For instance:
function1 (i. An example helps to show this.ch) double x.

• The value of a in main() is copied into the value of a in add(). The value 1 is copied into a and the value 4 is copied into b. b = 4. 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 function. }
then it is less clear what will happen. }
When this program is run. called a and 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(). Now.b).
Value Parameters
44
. So in the example above it would be equally valid to write:
#include <stdio. This means that if a and b are altered in add() they will not affect a and b in main(). two new variables are automatically created by the language. { printf ("%d". 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). They do not have to be the same as the actual parameters. } /**************************************/
add (a. • The value of b in main() is copied into the value of b in add().h> main () { int a = 1. b = 4. a+b). However if instead the program had been:
main () { int a = 1.b.C Programming Tutorial
printf ("%d". a+b). Another name for this is an argument. Here are some points about value parameters. They are constants. add (a. • The names of formal parameters can be anything at all. 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(). Formal Parameters These are the copies which work inside the function which was called. because 1 is always 1 and 4 is always 4.b) int a.

or expression with an appropriate type to a function. not an integer. we can send any literal value. This will probably be garbage.b).C Programming Tutorial
add (a. cos(a+b*2. }
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().41415).
Node:Functions as actual parameters.
main () { function ('*'. Next:Example 2. • The parameters ought to match by datatype when taken in an ordered sequence.
sin(3. ANSI C has a way of checking this by function `prototyping'. i+j). Previous:Value parameters. Up:Parameters
Value Parameters
45
. • Our use of variables as parameters should not leave you with the impression that we can only use variables as parameters. • The parameters ought to. but need not match in number! This surprising fact is important because programs can go wrong if a formal parameter was missed out.j) int i.g. causing yourself problems which are hard to diagnose. If the number of actual parameters is less than the number of formal parameters.i) char ch. { printf ("%d". 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.0). Some compilers will spot this if it is done accidentally and will flag it as an error. } /********************************/ function (ch. For example. int i. then the compiler will assign some unknown value to the formal parameters. but in Kernighan & Ritchie C there is no way to check this. It is possible to copy a floating point number into a character formal parameter. } /*******************************************/ add (i. strlen("The length of this string").0 is a floating point value.1.0). e. In fact. { }
is probably wrong because 1.j.

C Programming Tutorial One way to hand information back is to use the return statement. There are two new things to notice about this program: the symbols & and *. There is another way of handing back values which is less restrictive. Other operations 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. It is most easily explained with the aid of an example:
#include <stdio. In other words. GetValues (&i. This is easily confused with the multiplication symbol (which is identical). This function is slightly limited however in that it can only hand the value of one variable back at a time.". *q = 20. but more awkward than this. Similarly:
*q = 20.". { *p = 10. a: Variable Parameters 49
. it is not the variables themselves which are being passed to the procedure but the addresses of the the variables..h> main () { int i. with variable parameters. whereas the "contents of a pointer" applies only to a single variable and the star precedes the variable name.&j).
means make the contents of the address held in q equal to 20. The difference is only in the context in which the symbol is used. which are covered in later sections. so that the method can be used.*q.
means: Make the contents of the address held in p equal to 10. So. The ampersand & symbol should be read as "the address of.i. but information about how to get at the original variable which was passed. which are said to be pointers to i and j. often called a variable parameter.
*p = 10.j.. }
To understand fully what is going on in this program requires a knowledge of pointers and operators. This information can be used to alter the "actual parameters" directly and this is done with the * operator. 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. so this actually reads: make i equal to 10. Recall that the address held in p is the address of the variable i. So. but a brief explanation can be given here.q) int *p.. Fortunately this is not ambiguous since multiplication always takes place between two numbers or variables. in the program above. The star * symbol should be read as "the contents of the address. printf ("i = %d and j = %d". say.j) } /************************************/ GetValues (p. the function does not receive a copy of the variables themselves. This is by using a special kind of parameter.

Up:Top
Questions
51
. What do the symbols * and & mean. Next:Preprocessor. Does it mean anything to use a function directly as a variable parameter? 5.C Programming Tutorial 4. Previous:Parameters. Do actual and formal parameters need to have the same names? Node:Scope. when they are placed in front of an identifier? 6.

If every function is a ship floating in this sea of white space. Up:Scope
Global Variables
Global variables are declared in the white space between functions.Scope : Local And Global
Where a program's fingers can't reach. but can not get out of the large ship into the wide beyond. not the gaps inside functions. 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. From the computer's point of view. a C program is nothing more than a collection of functions and declarations. They can not enter just any region of the program because they are trapped inside blocks. and are connected together by means of function calls. in principle. 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). Every function has its own swimming pool! The idea can be taken further too.g. The swimming pool is just like a smaller sea. They can be used anywhere in a program: there is no restriction about where they can be used. A function called by a function. Up:Scope
Local Variables
Local variables are more interesting. • Global variables: • Local variables: • Parameters again: • Example 5: • Style note: • Scope and style: • Questions 11: Node:Global variables. switch channels) to make some choices. but one which is restricted to being inside a particular function. in other words the spaces and new line characters which are invisible to the eye. White space is the name given to the white of an imaginary piece of paper upon which a program is written. 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. Next:Parameters again.. You cannot go in and change the TV reality. but the television show draws its information from the world around it. Global variables are available everywhere. in a television show. What about swimming pools onboard the toy ships? (Meaning functions or blocks inside the functions! Scope : Local And Global 52
. The global white space is only the gaps between functions. Previous:Scope. Functions can be thought of as sealed capsules of program code which float on a background of white space. Node:Local variables. then local variables will work anywhere in the swimming pool (inside any of the toys ships. only observe the output. Next:Local variables. they are created when a program is started and are not destroyed until a program is stopped. Previous:Global variables. Another analogy is to think of what goes on in a function as being like watching a reality on television. is like seeing someone watching a televsion. You can send a parameter (e.

Whenever a pair of block braces is written into a program it is possible to make variable declarations inside the opening brace. Parameters are a way of handing local variables to other functions without letting them out! Value parameters (see last section) make copies of local variables without actually using them. or when control jumps out of the block. Next:Example 5. Where a variable is and is not defined is called the scope of that variable. In other words. it can't get out of the function to which is it passed . 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. Previous:Parameters again. which are both allowed to be in the same place (c in the example below) then the more local one wins. Because they only work in this local area of a program. They are only created when the opening brace is encountered and they are destroyed when the closing brace is executed. They cannot escape their block braces {}. The programmer does not have to think about this. Next:Style note. The copied parameter is then a local variable in another function.. Up:Scope
Example Listing
Notice about the example that if there are two variables of the same name. Up:Scope
Communication : parameters
If functions were sealed capsules and no local variables could ever communicate with other parts of the program. because the system automatically allocates and disposes of them. Variables can reach anywhere inside them but they cannot get out. Previous:Local variables. unless it is passed on as another parameter. This is why parameters are allowed. they are called local variables. /* statements */ }
These variables do not exist outside the braces. then functions would not be very useful. Node:Example 5. the last variable to be defined Local Variables 53
. That is. It tells a programmer what a variables horizons are! Node:Parameters again. Like this:
{ int locali.C Programming Tutorial
/* Global white space "sea" */ function () { /* On board ship */ { /* On board a toy ship */ } }
The same rules apply for the toy ships. char localch..

Up:Scope
Style Note
Some programmers complain about the use of global variables in a program. and this problem need not be crippling. c = a + b. Next:Scope and style. . printf ("%d". { .)
/***************************************************************/ /* */ /* SCOPE : THE CLLLED CAPSULES */ /* */ /***************************************************************/ #include <stdio. b = 2. if (a == 1) { int c.. } /**************************************************************/ handdown (a. 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. } /* Some function */
Node:Style note.b).c)..b) int a. Sometimes global variables are very useful however.h> /***************************************************************/ main () { int a = 1. while writing the rest of the variables in mainly small letters.. { int local integer...c). Previous:Example 5.
int GLOBALINTEGER. A way to make this clear is to write global variables in capital letters only.b.C Programming Tutorial takes priority. (Technically adept readers will realize that this is because it was the last one onto the variable stack. } handdown (a. c = 3. }
Example Listing
54
. printf ("%d"..

except where a program centres around a single data structure.
/**************************************/
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. Global variables will sometimes tidy up a program. 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. If a data structure is the main reason for a program's existence. which are longer than a couple of lines. Then add another function called another() and pass x_coord. How many different storage spaces are used when this program runs? (Hint: are x_coord. An alert and careful programmer can usually control this without difficulty. are written in an unusual way: with a levelled structure There are several good reasons for this. or reproducing a lot of variables. The diagram shows how the splitting of levels implies something about the scope of variables and the handing of parameters. Previous:Style note.y_coord which are LOCAL inside the function main(). The following guidelines may help the reader to decide whether to use local or global data: • Always think of using a local variable first. Another reason for restricting 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. Two integer variables called number_of_hats. Do parameters make functions leaky? i. Up:Scope
Scope and Style
All the programs in this book. • The preference in this book is to use local variables for all work. Do they spoil them by letting the variables leak out into other functions? 5. • 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. Is it impractical? Yes.counter which are GLOBAL and two float variables called x_coord. One is that the sealed capsules are shown to be sealed. What is meant by calling a block (enclosed by braces {} ) a "sealed capsule"? 4.y_coord to this function. if it means passing dozens of parameters to functions. Global variables punch holes in the sealed function capsules because they allow bugs from other functions to creep into tried and tested ones. Node:Questions 11. Next:Questions 11.C Programming Tutorial This allows global variables to be spotted easily. it is nearly always defined globally. Node:Scope and style.e. What is a global variable? 2. Up:Scope
Questions
1. provided it is not affected by any other functions from outside. Previous:Scope and style.y_coord and their copies the same?) Style Note 55
. by using a comment bar between each function. What is a local variable? 3. Write a program which declares 4 variables.

such as header files. The important feature of macros is that they are not merely numerical constants which are referenced at compile time. For instance.
#define TELEPHNUM 720663
This allows us to use the word TELEPHNUM in the program to mean the number 720663.14). in other words to include it as part of the program to be compiled.h. which had no constants in the language.
Using the macros instead makes the actions much clearer and allows the programmer to forget about what the numbers actually are. the simplest use of macros is to give constant values meaningful names: e. In this particular case. or whatever. One example of this has already been encountered for the standard header file stdio. The preprocessor has two main uses: it allows external files.postcode.h>
is a command which tells the preprocessor to treat the file stdio.TELEPHNUM. not to retype the number in every single instance. but it is more meaningful and would make a program read more naturally than if the raw number were used. a program which deals with several different fixed numbers like a telephone number. 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
Preprocessor Commands
57
.streetnum). Pre-processor commands are distinguished by the hash (number) symbol #. For example. the word is clearly not any shorter than the number it will replace.720663.
#include <stdio. This comes from its Unix origins.g. As its name might suggest.Preprocessor Commands
Making programming versatile. 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. the preprocessor is a phase which occurs prior to compilation of a program. a postcode and a street number could write:
printf("%d %d %d". C is unusual in that it has a pre-processor. it is only necessary to change the definition. It also means that a program is easy to alter because to change a telephone number.
instead of
printf("%d %d %d".345.h as if it were the actually part of the program text. 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.

The programmer just includes two dummy parameters with different names.) Notice that this definition uses a curious operator which belongs to C:
<test> ? <true result> : <false result>
Macro Functions
58
. (There is no reason why macros can't take more than one parameter too. no problem for macros.C Programming Tutorial would allow SUM to be used instead of 1+2+3+4. If a program read ABS(i) then the preprocessor would substitute i for x and so on."
would allow a commonly used string to be called by the identifier "string" instead of typing it out afresh each time. (Anything enclosed in string quotes is assumed to be complete and untouchable by the compiler. Consider the ABS() example. Next:Macros with parameters. This involves macros which accept parameters and hand back values. and it is. 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. say x..) Some macros are defined already in the file stdio. except inside strings. This would be no problem for a function which could accept parameters. See the example listing below. Or
#define STRING "Mary had a little lamb. Macros can also be made to take parameters. Up:Preprocessor
Macro Functions
A more advanced use of macros is also permitted by the preprocessor. 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. in fact.. This works by defining a macro with some dummy parameter. Previous:Preprocessor.h such as: EOF The end of file character (= -1 for instance) NULL The null character (zero) = 0 • Macro functions: • Macros with parameters: • Example 6: • Note about include: • Other Preprocessor commands: • Example 7: • Questions 12: Node:Macro functions. 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. If a programmer were to write ABS(4) then the preprocessor would substitute 4 for x.

Firstly the test is made. Errors in macros are very hard to find. Macros are also severely restricted in complexity by the limitations of the preprocessor. however. If the test is true then the first statement is carried out. Node:Macros with parameters.else is a statement with no value. As a memory aid. Function calls cannot be used as their parameters. But it is also slightly different: it is an expression which returns a value. but it is not a good idea to hold on to. Previous:Macro functions. since they allow us to single step through the code. No time is taken up in passing control over to a new function.else statement. Functions are easier to debug than macros. such as:
ABS(function())
has no meaning.) C can usually produce much more efficient code for this construction than for a corresponding if-else statement. 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. In the end (as with all programming choices) it is experience which counts towards the final ends.then. The advantage of a macro. Previous:Macros with parameters. Node:Example 6. Up:Preprocessor
When and when not to use macros with parameters
It is tempting to forget about the distinction between macros and functions.. otherwise the second is carried out.. Next:Example 6. is speed. It is simply not viable to copy complicated sequences of code all over programs. 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. Only variables or number constants will be substituted. and can be very confusing. where as an if.. There is a limitation with macros though.. ideal for macros. It is not a valid C statement.h> #define #define #define #define #define STRING1 STRING2 EXPRESSION EXPR2 ABS(x) "A macro definition\n" "must be all on one line!!\n" 1 + 2 + 3 + 4 EXPRESSION + 10 ((x) < 0) ? -(x) : (x)
When and when not to use macros with parameters
59
. thinking that it can be ignored. because control never leaves the home function when a macro is used: it just makes the function a bit longer.C Programming Tutorial This is like a compact way of writing an if. Next:Note about include. To some extent this is true for absolute beginners. Choosing between functions and macros is a matter of personal judgement. No simple rules can be given. Up:Preprocessor
Example Listing
/************************************************************/ /* */ /* MACRO DEMONSTRATION */ /* */ /************************************************************/ #include <stdio.then.

else leave out that code altogether. #ifdef This is followed by a macro name.b)) /************************************************************/ main () { printf printf printf printf printf printf } /* No #definitions inside functions! */
(STRING1).b) (a < b) ? (b) : (a) #define BIGGEST(a. Previous:Example 6. ("%d\n". #ifndef This is followed by a macro name.C Programming Tutorial
#define MAX(a.3)). #if This is followed by some expression on the same line.b) < c) ? (c) : (MAX(a. Up:Preprocessor
Note about #include
When an include statement is written into a program.
#undef This undefines a macro.EXPR2). leaving the name free. the #include statement is itself valid C. Previous:Note about include. (STRING2). ("Biggest of 1 2 and 3 is %d". Up:Preprocessor
Other Preprocessor commands
This section lies somewhat outside the main development of the book.b. There are a handful more preprocessor commands which can largely be ignored by the beginner. It is an advanced feature which can be used to say: only compile the code between #if and #endif if the value following #if is true.
Node:Note about include. Example Listing 60
.c) (MAX(a. It allows conditional compilation. This is different from not executing code--the code will not even be compiled. Next:Other Preprocessor commands. Next:Example 7.EXPRESSION). ("%d\n". so this means that a file which is included may contain #includes itself. ("%d\n". The includes are then said to be "nested". If that name is not defined then this is true.ABS(-5)). However.BIGGEST(1. If that macro is defined then this is true. NOTE : true has any non zero value in C. it is a sign that a compiler should merge another file of C programming with the current one. false is zero. This often makes includes simpler. Node:Other Preprocessor commands.2. They are commonly used in "include" files to make sure that things are not defined twice. You might wish to omit it on a first reading.

#line Has the form: #line constant filename This is for debugging mainly. Previous:Other Preprocessor commands. #error This is a part of the proposed ANSI standard. This statement causes the compiler to believe that the next line is line number (constant) and is part of the file (filename).e. #ifdef. printf (DITTO).. Next:Questions 12. Node:Example 7.
Other Preprocessor commands
61
.C Programming Tutorial #else This is part of an #if.. It forces the compiler to abort compilation. #ifndef preprocessor statement. " #else #define OPTIONSTRING "The alternative" #define DITTO "i. It is intended for debugging.. #endif This marks the end of a preprocessor statement. Up:Preprocessor
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 . This! "
#endif /***********************************************************/ #ifdef SOMEDEFINITION #define WHATEVER "Something was defined!" #else #define WHATEVER "Nothing was defined" #endif /************************************************************/ main () { printf (OPTIONSTRING).

True or false? 4. True or false? Node:Pointers. Define a macro called "birthday" which describes the day of the month upon which your birthday falls. Up:Preprocessor
Questions
1. 3. Next:Standard Output and Standard Input.h. Write an instruction to the preprocessor to include to maths library math. Previous:Example 7. Up:Top
Example
62
. A macro is always a number.C Programming Tutorial
}
Node:Questions 12. Previous:Preprocessor. 2. A macro is always a constant.

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.
The declarations of pointers look a little strange at first. although the address is the address of the beginning of the function executable. like some modern day programmers. If. it would be easy to find a pointer to it called location_ptr. location_ptr = &(location). after all. The four lines above make two identical pointers to a floating point variable called location. Pointers to variables are found by recording the address at which a variable is stored. the segment of memory returned will be four bytes long (32 bits) and be interpreted as an integer. in which variables can be given elegant and meaningful names: it seems like a step in the backward direction to want to be able to find out the exact number of the memory location at which it is stored! The whole point of variables. The star * symbol which stands in front of the variable name is C's way of declaring that variable to be a pointer. A pointer is a bundle of information that has two parts. The other part is the type of value that the pointer points to the beginning of. For instance: if location were a float type variable. it is probably a source of wonder why anyone Would ever want to know the address of these variables.
or
address = &(location). you believe in sanctity of high level languages. 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. You need to find that essential piece of information which is stored at some unknown location. You have a map (a plan) of the computer's memory. the type is the type of value that the function will return. This is not quite fair though. It is always possible to find the address of a piece of storage in C using the special & operator. Having gone to the trouble to design a high level language. if the pointer is of a type int.
float location. Thus. The point is that a pointer is just a place to keep a record of the address of a variable. Pointers point to these locations by keeping a record of the spot at which they were stored. like C. Remember: A pointer is a variable which holds the address of the storage location for another given variable.
Pointers
63
. It is certainly rare indeed when we should want to know the actual number of the memory location at which something is stored. float *location_ptr. one of them is called location_ptr and the other is called address. One part is the address of the beginning of the segment of memory that holds whatever is pointed to. This tells the computer how much of the memory after the beginning to read and how to interpret it.*address. so they are really the same thing. In the case of a function. is that it is not necessary to know exactly where information is really stored.Pointers
Making maps of data. That would really make the idea of a high level language a bit pointless.

without doubt.. The following example might help to clarify the way in which they are used:
int somevar. clinging on... The two operators * and & are always written in front of a variable. Another way of saying the second of these is: * The contents of the location pointed to by. somevar = 42. printf ("%d". For instance: &x The address at which the variable x is stored. *ptr_to_somevar = 56. They can be read in a program to have the following meanings: & The address of. Next:Uses for pointers.C Programming Tutorial C provides two operators & and * which allow pointers to be used in many versatile ways... • Pointer operators: • Uses for pointers: • Pointers and Initialization: • Example 8: • Types Casts and Pointers: • Function pointers: • Calling functions by pointer: • Questions 13: Node:Pointer operators. See Value parameters. Up:Pointers
& and *
The & and * operators have already been used once to hand back values to variable parameters. *ptr The contents of the variable which is pointed to by ptr. * The contents of the address held in. to that one variable. 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. ptr_to_somevar = &(somevar). /* 1 */ /* 2 */ /* 3 */ /* 4 */ /* 5 */ /* 6 */
The key to these statements is as follows: & and * 64
. Previous:Pointers. int *ptr_to_somevar. so that they refer..*ptr_to_somevar).

which is called scanf(). In particular they are vital when using data structures like strings or arrays or linked lists. For example: to read an integer:
int i. double y. except that it uses pointers to variables.
or
int *i. char ch.
Node:Uses for pointers.*xp. 3. Previous:Pointer operators. not variables themselves. One example of the use of pointers is the C input function.
int i. Let the contents of the location pointed to by ptr_to_somevar be 56. They are always declared with the * symbol. scanf ("%d". rather than an integer. The value is the address of the variable somevar.&i). Before this. We shall meet these objects in later chapters.
Uses for Pointers
65
.*yp.POKE are like pointers). Let somevar take the value 42. 6. The declaration (2) merely makes it a pointer which can point to any integer variable which is around.i). It is looked at in detail in the next section. short s. 4. Declare a pointer to an int type called ptr_to_somevar. its fate is quite open. Next:Pointers and Initialization. The * which stands in front of ptr_to_somevar is the way C declares ptr_to_somevar as a pointer to an integer.*chp. Notice that only at this stage does is become a pointer to the particular variable somevar. 5. say. Up:Pointers
Uses for Pointers
It is possible to have pointers which point to any type of data whatsoever.*ip.C Programming Tutorial 1.*sp.
Pointers are extremely important objects in C. scanf ("%d". Declare an int type variable called somevar. Print out "the contents of the location pointed to by ptr_to_somevar" in other words somevar itself. They are far more important in C than in. This is the same as the more direct statement:
somevar = 56. This gives a value to ptr_to_somevar. float x. It is a bit like the reverse of printf(). scanf() is for getting information from the keyboard. 2. Pascal or BASIC (PEEK. Some examples are given below. So this will be just 42.

Node:Pointers and Initialization. scanf will probably corrupt a program. It is incorrect. but it is not inconceivable that in developing some program a programmer would want to know the actual address. It could be printed out as an integer:
type *ptr: printf ("Address = %d". It is not often a useful thing to do. Think about what happens when the following statement is written. Next:Types Casts and Pointers. This kind of initialization cannot possibly work and will most likely crash the program or corrupt some other data. Next:Example 8. A compiler will probably not prevent this however because there is nothing incorrect about it as far as syntax is concerned. One reason why one might want to know this would be for debugging. This statement is really talking about two different storage places in the memory:
int *a = 2. Up:Pointers
Example Listing
/**********************************************/ /* */ /* Swapping Pointers */ /* */ /**********************************************/
Pointers and Initialization
66
. a only contains garbage so the 2 could be stored anywhere.C Programming Tutorial
The & sign or the * sign is vital. Nothing has been said about that yet. because no statement like
a = &someint. There may not even be a variable at the place in the memory which a points to. This is one reason why this important function has been ignored up to now. what is declared is a pointer. This is doomed to faliure.
First of all. The & operator is flexible enough to allow this to be found. 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. Previous:Uses for pointers.
has yet been encountered which would give it a value.(int) ptr). so space for a `pointer to int' is allocated by the program and to start off with that space will contain garbage (random numbers). If it is forgotten. with the value 2. Previous:Pointers and Initialization. Up:Pointers
Pointers and Initialization
Something to be wary of with pointer variables is the way that they are initialized. Node:Example 8. to initialize pointers in a declaration. pointed to by a. It will then attempt to fill the contents of some variable. logically.

C Programming Tutorial
/* Program swaps the variables which a.b */ /* point to. /* Declr ptrs */ /* Declare storage */ /* Initialize storage */
/* Initialize pointers */
printf ("%d %d\n". There are occasions however when it is actually necessary to convert one kind of pointer into another.*b.*c.*a.B.*b). A = 12. Previous:Example 8. Not pointless really ! */ #include <stdio.*a. b = c. int A.h>
main () { int *a. The cast operator for variables.*b). b = &B. int *i. printf ("%d %d\n". } /* swap pointers */
Node:Types Casts and Pointers. i = (int *) ch. c = a.
Example Listing
67
. Next:Function pointers. Up:Pointers
Types. When this situation comes about. This might happen with a type of variable called "unions" or even functions which allocate storage for special uses. a = b. the cast operator has to be used to make sure that pointers have compatible types when they are assigned to one another. 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. a = &A. B = 9. This is not necessarily the case. Compilers distinguish between pointers to different kinds of objects. See The Cast Operator. 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. These objects are met later on in this book.

{ int i. Node:Function pointers. The reason is that you must declare function2() explicitly like this:
int function2(). The reason that pointers have to be `cast' into shape is a bit subtle and depends upon particular computers. In machine code circles this is sometimes called indirection or vectoring. Previous:Types Casts and Pointers. 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). 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 []. Pointers to functions enable you to tell any function which sub-ordinate function it should use to do its job. the name of the function without the round brackets works as a pointer to the start of the function. i = (*a)(parameters). In practice it may not actually do anything. You do not have to rewrite any code. You might want to omit it on first reading. Up:Pointers
Pointers to functions
This section is somewhat outside of the main development of the book. 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. The function which accepts a function pointer as an argument looks like this:
function1 (a) int (*a)().
If the function returns a different type then clearly the declaration will be different but the form will be the same. 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.C Programming Tutorial This copies the value of the pointer ch to the pointer i. Let's now consider pointers to functions as opposed to variables. The declaration can be placed together with other declarations. a stream of compilation errors will be the result. since a function is a global object regardless. It is not important whether the variable is declared locally or globally.
Types. This is an advanced feature which should be used with more than a little care. Next:Calling functions by pointer. When we come to look at arrays. For functions. What is important is that we declare specifically a pointer to a function which returns a type (even if it is void). Casts and Pointers
68
. 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. The cast operator makes sure that the pointers are in step and not talking at cross purposes.
If you try this as it stands.

textscreen(). Next:Questions 13. you would do it like this:
typename (*fnptr)(). Up:Pointers
Calling a function by pointer
Given a pointer to a function how do we call the function? The syntax is this:
variable = (*fnptr)(parameters). char (*fnptr)(). case 3: fnptr = windows.
A pointer to a function can be used to provide a kind of plug-in interface to a logical device. case 2: fnptr = textscreen. break. }
Pointers to functions
69
.e.
but we can also do the same thing with a pointer to the function.
int i.
Node:Calling functions by pointer. function(). fnptr = function.
An example let us look at a function which takes an integer and returns a character.
Normally this function is called using the statement:
ch = function(i). a way of choosing the right function for the job. First define
char function(). break.
void printer(). char ch. Previous:Function pointers.windows(). i.C Programming Tutorial
}
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.
then call the function with
ch = (*fnptr)(i). switch (choice) { case 1: fnptr = printer.

Next:Assignments Expressions and Operators. How is a variable declared to be a pointer? 3. Previous:Pointers. What data types can pointers "point to"? 4. The C++ language provides an abstract form of this with a more advanced syntax. Make sure that your pointers are assigned before you use them! Node:Questions 13. Previous:Calling functions by pointer. but this is the essence of virtual function methods in object oriented languages.65.fnptr).C Programming Tutorial
Output(data. ? Node:Standard Output and Standard Input. 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. Local variables are never initialized by the compiler in C. BEWARE! A pointer to a function is an automatic local variable. What is a pointer? 2. Why is it incorrect to declare: float *number = 2. It is useful in dealing with unions and memory allocation functions. you will come quickly to grief. Up:Top
Calling a function by pointer
70
. If you inadvertently forget to initialize the pointer to a function.
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). Up:Pointers
Questions
1.) 5.

because C does it automatically. meaning standard input and standard output and standard error file.Standard Output and Standard Input
Talking to the user. They are called stdin. The C library functions covered by stdio. In order of importance. stdout is usually the screen. Some files can only be read. Stdin is the input which usually arrives from the keyboard of a computer. Without input and output computers would be quite useless. Usually this means the keyboard and the screen. Also the programmer never has to open or close these. They are simplified versions of the functions that can be used on any kind of file. most of the time. Getting information in and out of a computer is the most important thing that a program can do. A file is really just an abtraction: a place where information comes from or can be sent to. depending upon how the user ran the program. See Files and Devices. In other situations files are called I/O streams. C has three files (also called streams) which are always open and ready for use. 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.. stdout and stderr. C treats all its output as though it were reading or writing to different files. The keyboard and screen are referred to as the standard input/output files because this is what most people use. others can be both read from and written to. This is only `usually' because the situation can be altered.h provides some methods for working with stdin and stdout. but it can also be redirected to a printer or to a disk file or to a modem etc. some can only be written to. they are:
printf () scanf () getchar() putchar() gets () puts ()
• printf again: • Example 9: • Output 9: • Formatting with printf: • Example 10: • Output 10: • Special Control Characters again: • Questions 15: • scanf: • Conversion characters: • How does scanf see the input: • First account of scanf: • The dangerous function: • Keeping scanf under control: • Example 11: • Matching without assigning: Standard Output and Standard Input 71
. stderr is the route by which all error messages pass: usually the screen.

The printf function has general form:
printf ("string... printf ("%d". Up:Standard Output and Standard Input
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..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.".").2. 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. Next:Example 9. printf ("Some number = %d".number). which says something about the size of the blank space and the type of number or string which will be filled into that space. Previous:Standard Output and Standard Input.C Programming Tutorial • Formal Definition of scanf: • Summary of points about scanf: • Questions 15b: • Low Level Input/Output: • Questions 15c: Node:printf again. Often the string is called the control string because it contains these control characters."). Making text look good on screen is important in programming..
The next simplest case that has been used before now is to print out a single integer number:
int number = 42. C makes this easy by allowing you to decide how the text will be printed in the available space. followed by some coded information. These fields are introduced by using a % character.
The result of this last example is to print out the following on the screen:
Some number = 42
printf
72
.number).variables. printf ("Testing 1.3.
The two can be combined:
int number = 42. The simplest use of printf is to just print out a string with no blank fields to be filled:
printf ("A pretty ordinary string.

C Programming Tutorial The text cursor is left pointing to the character just after the 2. Previous:printf again. i). Notice the way that %d is swapped for the number 42. Next:Output 9. There are other kinds of data than integers though. So it better had be an integer or things will go wrong! Other characters are used for other kinds of data. Up:Standard Output and Standard Input
Example Listing
/*******************************************************/ /* */ /* printf Conversion Characters and Types */ /* */ /*******************************************************/ #include <stdio. The example program and its output below give some impression of how they work: Node:Example 9. char *string_ptr = "any old string". unsigned int ui = 10. %d defines a field which is filled in with the value of the variable.h> main () { int i = -10.
Example Listing
73
. whichever is shorter The best way to learn these is to experiment with different conversion characters.52. printf ("signed integer %d\n". double y = 3. %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. char ch = 'z'.56.ui). printf ("unsigned integer %u\n". float x = 3. d signed denary integer u unsigned denary integer x hexadecimal integer o octal integer s string c single character f fixed decimal floating point e scientific notation floating point g use f or e. Here is a list if the different letters for printf. Any kind of variable can be printed out with printf.

The conversion specifiers in the printf string can be extended to give more information.
%-10. 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.x.i). If the size is bigger than the object to be printed. Next:Example 10.520000 ditto 3.y). printf ("character wrong!"). printf (" ditto %g %g\n".y). printf ("Hexadecimal %x %x\n". That extra information takes the form:
% [-] [fwidth] [. printf (" ditto %e %e\n". [fwidth] This is a number which specifies the field width of this "blank field".560000 3. }
Node:Output 9.C Programming Tutorial
printf ("This is wrong! %u".i. The % and the character type act like brackets around the extra information.560000E+00 3.x. Next:Formatting with printf. printf ("See what happens when you get the "). Previous:Output 9. printf ("Float and double %f %f\n". In other words.ch). e. Previous:Example 9. printf ("single character %c\n". which carries some more information.p] X
where the each bracket is used to denote that the item is optional and the symbols inside them stand for the following.3f
is an extended version of %f. Output 74
. Up:Standard Output and Standard Input
Formatting with printf
The example program above does not produce a very neat layout on the screen. printf ("whole string -> %s".i.y).x. Up:Standard Output and Standard Input
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.string_ptr).560000 3.520000E+00 ditto 3.ui).ui).520000 single character z whole string -> any old string
Node:Formatting with printf.g. printf ("Octal %o %o\n". the rest of the field will be filled out with spaces.

71828 2.
Object to be printed 42 42 324 -1 -1 'z' 'z' 2.71828e+00| | 2.C Programming Tutorial [-] If this included the output will be left justified. or aligned with the right hand margin of the field "box".71828 "printf" "printf" "printf" "printf" "printf" "printf" Control Spec.71828 2.71| |printf| | printf| |printf|(overspill) | pri| |pri | |pri|
Node:Example 10.7180| | 2.71800| |2.5f %10e %10.71828 2. Next:Output 10.4f %10.2g %s %10s %2s %5. This means it will be aligned with the left hand margin of the field created with [fwidth]. The width of a field is draw in by using the | bars.p] This has different meanings depending on the object which is to be printed.7182|(overspill) |2.3s
| 42| |42 | | 324| |-1 | |-1|(overspill) | z| |z | | 2.71828 2.2f %2.71828| | 2.6f
The table below helps to show the effect of changing these format controls. Previous:Formatting with printf. Normally all numbers are right justified. For a floating point type (float or double) p specifies the number of decimal places after the point which are to be printed.4f %.21s %2.71| |2. [.718 2.2f %25.2e %10.71828 2.3s %-5.718 2.2f %-10.71 | |2. Some valid format specifiers are written below here.71828 2. Actual Output
%6d %-6d %10d %-10d %1d %3c %-3c %10f %10.3s %.
%10d %2. For a string it specifies how many characters are to be printed.17e+00| | 2. Up:Standard Output and Standard Input
Example Listing
/***********************************************/ /* */ /* Multiplication Table */ /* */ /***********************************************/
Formatting with printf
75
.

Here are some valid uses of scanf:
int i. This makes quite a difference to the way that scanf works. See Character Conversion Table. try printing an integer with %f or a floating point number with %c. e. Write a program which simply prints out: 6. Previous:Questions 15. 4. Investigate what happens when you type the wrong conversion specifier in a program. What is wrong with the following statements? 1. Hint: if you don't know. Previous:Special Control Characters again. 3. try them in a program! Node:scanf. This is bound to go wrong . printf (x). but pointers to variables.
with one important exception: namely that it is not variables which are listed after the control string. In fact it is probably the most difficult to understand of all the C standard library functions. Node:Questions 15.pointers). printf ("%d"). The actual mechanics of scanf are very similar to those of printf in reverse
scanf ("string.".but how will it go wrong? 3.
Special Control Characters
77
.C Programming Tutorial \v vertical tab \" double quote \' single quote character ' \\ backslash character \ \ddd character ddd where ddd is an ASCII code given in octal or base 8. printf ("Number = %d"). Remember that C treats its keyboard input as a file.g. Up:Standard Output and Standard Input
scanf
scanf is the input function which gets formatted input from the file stdin (the keyboard). 2. Next:Conversion characters. This is a very versatile function but it is also very easy to go wrong with. char ch. float x. Next:scanf. See Character Conversion Table. Up:Standard Output and Standard Input
Questions
1.. printf ().23e+00 2.. \xddd character ddd where ddd is an ASCII code given in hexadecimal or base 16.

char *ch. Next:How does scanf see the input. The other possibility is that a program might already have pointers to a particular set of variables in that case the & is not needed.
Notice the & characters which make the arguments pointers. Node:Conversion characters. x). Up:Standard Output and Standard Input
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. i. &ch. Previous:scanf. float *x. { scanf ("%d %c %f". &i.x) int *i. }
In this case it would actually be wrong to write the ampersand & symbol.ch. Also notice the conversion specifiers which tell scanf what types of data it is going to read. d denary integer (int or long int) ld long decimal integer x hexadecimal integer o octal integer h short integer f float type lf long float or double e float type le double c single character s character string
scanf
78
. For instance:
function (i. ch. &x).C Programming Tutorial
scanf ("%d %c %f".

If it is found that a program's input seems to be behaving strangely. such as a group of numbers all between 0 and 9. char ch. |'\n'| --------------------------------------
As far as scanf is concerned. Up:Standard Output and Standard Input
How does scanf see the input?
When scanf is called in a program it checks to see what is in the input file. Keyboard input is usually buffered.
int i. There is an important point here: although scanf will start scanning through characters as soon as they are in the buffer. This means that the characters are held in a kind of waiting bay in the memory until they are read. If the buffer is empty scanf will wait for some characters to be put into it. it will look for some characters which make up a valid integer. The buffer can be thought of as a part of the input file stdin. scanf ("%d %c %f".. It then tries to find some characters which fit the Conversion characters 79
.
Here is a simplified. If the buffer has some characters in it.chars. scanf will start to look through these. it checks to see what the user has typed in at the keyboard. check these carefully. In other words.. 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.C Programming Tutorial The difference between short integer and long integer can make or break a program. &x). Next:The dangerous function. A line is a bunch of characters ending in a newline character \n. scanf looks at the control string and finds that the first conversion specifier is %d which means an integer. it is useful to think of the input as coming in `lines'. Previous:Conversion characters. Previous:How does scanf see the input. Next:First account of scanf. it will wait for some characters to be put into the buffer.738/. (See the section on Errors and Debugging for more about this. 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. ideal view of what happens. float x. that is. To understand how scanf works. &ch. This can be represented by a box like the one below:
-------------------------------------| some. &i. if not. Up:Standard Output and Standard Input
First account of scanf
Consider the example which was give above. the input is entirely made out of a stream of characters. holding some characters which can be scanned though. If the user just wants a character then any character will do! Node:First account of scanf.) Node:How does scanf see the input. 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 terminal.

C Programming Tutorial 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! Node:The dangerous function, Next:Keeping scanf under control, Previous:First account of scanf, Up:Standard Output and Standard Input

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... Node:Keeping scanf under control, Next:Example 11, Previous:The dangerous function, Up:Standard Output and Standard Input

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 sophisticated 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:

First account of scanf

80

C Programming Tutorial
int i; scanf("%d",&i); skipgarb();

The programmer must police user-garbage personally by using a loop to the effect of:
while (inputisnonsense) { printf ("Get your act together out there!!\n"); scanf (..) skipgarb(); }

It is usually as well to use skipgarb() every time. Node:Example 11, Next:Matching without assigning, Previous:Keeping scanf under control, Up:Standard Output and Standard Input

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 which scanf `misunderstands' what the user has typed in and also the values which the variables had before the scanf function.
Input : 1x2.3 Output: 1 x 2.300000

The input gets broken up in the following way:
------------------

Keeping scanf under control

81

C Programming Tutorial
| 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: 1 0.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> ---

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); printf ("%c %c %c",ch1,ch2,ch3); }

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:

Matching without assigning

83

C Programming Tutorial
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 several 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);

In this last case scanf aborted before matching anything. Node:Formal Definition of scanf, Next:Summary of points about scanf, Previous:Matching without assigning, Up:Standard Output and Standard Input

Matching without assigning

84

C Programming Tutorial

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

[*] the optional assignment suppression character. [n] this is a number giving the maximum field width to be accepted by scanf for a particular item. That is, the maximum number of 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);

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. Formal Definition of scanf 85

What is a white space character? 2.
high level: printf() / low level: putchar() | | | | | | | scanf() \ getchar()
getchar gets a single character from the input file stdin. Previous:Questions 15b. Up:Standard Output and Standard Input
Questions
1. • White space characters in Node:Questions 15b. For example:
char ch. no value will be assigned to the corresponding variable. 5. Write a program which fetches two integers from the user and multiplies them together. Previous:Summary of points about scanf. Print out the answer. in fact. Up:Standard Output and Standard Input
Low Level Input/Output
• getchar and putchar: • gets and puts: Node:getchar and putchar. Next:Questions 15c.
Summary of points about scanf
86
. Next:Low Level Input/Output. scanf always takes pointer arguments. 4. • White space characters are ignored for all conversion characters except %c. 3. putchar writes a single character to the output file stdout. Up:Low Level Input/Output
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. See Preprocessor. These functions are called getchar() and putchar() but. getchar returns a character type: the next character on the input file. Try to make the input as safe as possible. Write a program which just echoes all the input to the output. True or false? Node:Low Level Input/Output. Only a %c type can contain a white space character. C also provides some functions for dealing with input and output at a lower level: character by character. Write a program which strips spaces out of the input and replaces them with a single newline character. Previous:Low Level Input/Output. Next:gets and puts. they might not be functions: they could be macros instead. ch = getchar().C Programming Tutorial • If an element is not matched.

while (ch != '\n') { ch = getchar(). into the variable ch. but it is something to watch out for. It is a low level function and does not `know' anything about data types other than characters.
These two alternatives have the same effect. where a program responds to keys as they are pressed without having to wait for return to be pressed. This function was written in a very compact way. as in the first line. Node:gets and puts. Up:Low Level Input/Output getchar and putchar 87
. putchar() writes a character type and also returns a character type. rather than functions. Another way of writing it would be as below:
skipgarb () { char ch. skipgarb(). ch = getchar(). 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. putchar (ch). getchar was used in the function skipgarb() to tame the scanf() function. This function keeps on getchar-ing until it finds the newline character and then it quits. and then it simply skips over it in one go! The effect is that it waits for RETURN to be pressed. putchar() is not much use without loops to repeat it over and over again. This can simply be discarded. For example:
char ch = '*'. In other words it just hands the same value back again.C Programming Tutorial This places the next character. } } /* skip garbage corrupting scanf */
The != symbol means "is not equal to" and the while statement is a loop. For example:
printf("Press RETURN to continue\n"). This means that it might not be possible to use functions as parameters inside them:
putchar( function() ). Previous:getchar and putchar.
skipgarb() does not receive any input until the user presses RETURN. what ever it might be.
This depends entirely upon the compiler. An important point to remember is that putchar() and getchar() could well be implemented as macros. ch = putchar (ch). One of these is to copy immediate keypress statements of languages like BASIC. The value returned by putchar() is the character which was written to the output. This function has many uses. Notice that no conversion to different data types can be performed by getchar() because it deals with single characters only.

Up:Top
gets and puts
88
. Previous:Standard Output and Standard Input. Node:Questions 15c. Up:Standard Output and Standard Input
Questions
1. Is the following statement possible? (It could depend upon your compiler: try it!)
putchar(getchar()). For instance:
char *string[length].
More information about these is given later. Next:Decisions.) 2. Previous:Low Level Input/Output. Their purpose is either to read a whole string from the input file stdin or write a whole string to the output stdout. Re write the statement in question 1. Node:Assignments Expressions and Operators.
What might this do? (Hint: re-read the chapter about the pre-processor. puts(string). string = gets(string).C Programming Tutorial
gets and puts
Two functions which are similar to putchar() and getchar() are puts() and gets() which mean putstring and getstring respectively. See Strings. Strings are groups or arrays of characters. assuming that putchar() and getchar() are macros.

The more abstruse operators are looked at in another chapter. equal to. e. An operator is something which takes one or more values and does something useful with those values to produce a result. In fact the second group is a subset of the first. It operates on them. divided up arbitrarily according to what they do with their operands. 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. say. operation The action which was carried out upon the operands by the operator! There are lots of operators in C. This is more than. Some of them may already be familiar:
+ * / = & ==
Most operators can be thought of as belonging to one of three groups. +. • Expressions and values: • Example 12: • Output 12: • Parentheses and Priority: • Unary Operator Precedence: • Special Assignment Operators ++ --: • More Special Assignments: • Example 13: • Output 13: • The Cast Operator: • Expressions and Types: • Summary of Operators and Precedence: • Questions 16: Assignments. Expressions and Operators 89
. The majority of operators fall into the first group. in which the result of the operation is a boolean value of either true of false. Working things out.. operand Each thing which is operated upon by an operator is called an operand. e. The terminology of operators is the following: operator Something which operates on someting. C has no less than thirty nine different operators.g. less than.g. • Operators which produce new variable types: like the cast operator. They make a result from their operands. These rough groupings are thought of as follows: • Operators which produce new values from old ones. Pascal and BASIC put together! The operators serve a variety of purposes and they can be used very freely.Assignments. Expressions and Operators
Thinking in C. greater than. The object of this chapter is to explain the basics of operators in C.

C Programming Tutorial Node:Expressions and values. x = 2. Next:Example 12. Up:Assignments Expressions and Operators
Expressions and values
The most common operators in any language are basic arithmetic operators. For example:
double x. which is useful to know because compilers tend to use this when handing out error messages. variables and numbers. An expression is simply the name for any string of operators.
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.356.y. The assignment operator can be summarized in the following way:
lvalue = expression. As usual there is some standard jargon for this. In C these are the following: + plus (unary) 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.
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. Perhaps the most important operator then is the assignment operator:
= assignment operator
This has been used extensively up to now. x = x + 2 + 3/5. y = x. All of the following could Expressions and values 90
. Previous:Assignments Expressions and Operators.

It could be interpreted in several ways:
((a + b) / 4) * 2
or
(a + b)/ (4 * 2)
or
a + (b/4) * 2
and so on.C Programming Tutorial Node:Output 12. By using parentheses. Up:Assignments Expressions and Operators
Output
Arithmetic Operators i = int int int 6. Parentheses are said to have a higher priority than + * or / because they are evaluated as "sealed capsules" before other operators can act on them. although their position is a bit unclear.250000
Node:Parentheses and Priority. 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. Up:Assignments Expressions and Operators
Parentheses and Priority
Parentheses are classed as operators by the compiler.1 5 * 2 is : -6 = 3 = 4 = 10
9 div 4 = 2 remainder 1: int 9 / 4 = 2 int 9 4 = 1 double 9 / 4 = 2. such as:
a + b / 4 * 2
it is not clear what is meant by this. Next:Unary Operator Precedence. but it does not alter than fact that
a + b / 4 * 2
Output
92
. Putting parentheses in may remove the ambiguity of expressions. Next:Parentheses and Priority. Previous:Example 12. any doubt about what the expression means is removed. Previous:Output 12. If an expression is written out in an ambiguous way. -i 1 + 2 5 .

Node:Unary Operator Precedence. (character types too. Otherwise. Next:Special Assignment Operators ++ --. 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. A table of all operators and their priorities is given in the reference section. or ++variable.
Parentheses and Priority
93
. Up:Assignments Expressions and Operators
Unary Operator Precedence
Unary operators are operators which have only a single operand: that is. The convention is that some operators are stronger than others and that the stronger ones will always be evaluated first. Previous:Parentheses and Priority. Use parentheses to be sure. Normally. 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. Previous:Unary Operator Precedence. 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. Node:Special Assignment Operators ++ --. with care.
would do ++ before *. this is accomplished by writing:
variable = variable + 1. they operate on only one object. but there is a much better way of doing this:
variable++. in other languages.
In C this would also be quite valid.C Programming Tutorial is ambiguous.) They are used to simply add or subtract 1 from a variable. For instance:
++ -+ &
The precedence of unary operators is from right to left so an expression like:
*ptr++. Next:More Special Assignments. This is a subject in which it becomes important to think in C and not in other languages. Up:Assignments Expressions and Operators
Special Assignment Operators ++ and -C has some special operators which cut down on the amount of typing involved in a program.

or
--variable. There is a handful of these
<operation>=
operators: one for each of the major operations which can be performed. but in the more advanced uses of C operators. Similarly:
variable = variable . Consider the statement:
variable = variable + 23. It could be done more simply using the general increment operator: +=
variable += 23.
Notice particularly that these two operators can be placed in front or after the name of the variable. Next:Example 13. Node:More Special Assignments. Up:Assignments Expressions and Operators
More Special Assignments
Here are some of the nicest operators in C.these are short ways of writing longer expressions. There is. which appear later in this book. In some cases the two are identical.
This performs exactly the same operation.
and so on. one for subtraction too: Special Assignment Operators ++ and -94
. Like ++ and -. Similarly one could write:
variable1 = variable1 + variable2.
as
variable1 += variable2. there is a subtle difference between the two.
is equivalent to:
variable--. naturally.C Programming Tutorial would do the same thing more neatly. Previous:Special Assignment Operators ++ --.
In C this would be a long winded way of adding 23 to variable.1.

A similar thing would happen with float:
float x. j = 2. See Variables. y = 2. Up:Assignments Expressions and Operators
Expressions and Types
There is a rule in C that all arithmetic and mathematical operations must be carried out with long variables: that is.
would also be incorrect for the same reasons as above. the types
double long float int long int
If the programmer tries to use other types like short or float in a mathematical expression they will be cast into long types automatically by the compiler.5.
A compiler will claim that there is a type mismatch between i and the expression on the right hand side of the assignment. so that the expression is automatically converted into long type or int type. x = y * 2. The programmer can get around this by using the cast operator to write:
short i. 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! Node:Expressions and Types. So the right hand side is int type and the left hand side is short type: hence there is indeed a type mismatch. It will always produce some value. i = (int) ch. even though it appears to be wrong. The subtlety is that arithmetic cannot be done in short type variables.3. i = (short) j * 2 + 1. whatever the conversion: however remotely improbable it might seem. j = 2.
This operator was introduced earlier. This can cause confusion because the compiler will spot an error in the following statement:
short i. Next:Summary of Operators and Precedence. Previous:The Cast Operator. i = j * 2 + 1.
The Cast Operator
97
.C Programming Tutorial
char ch. int i. The compiler is perfectly correct of course.

so i would be false.
would be false. which is about decisions and comparisons. but they are themselves operators and expressions can be built up with them. The values which they produce are called true and false. These comparison operators are used for making decisions. "true" and "false" are not defined normally in C.
1 == 1
has the value "true" (which could be anything except zero). Comparisons are often made in pairs or even in groups and linked together with words like OR and AND. In other words. The statement:
int i. i = (1 == 2). The relevance of these operators will quickly become clear in the next chapter.C Programming Tutorial Comparisons and Logic
Comparisons and Logic
Six operators in C are for making logical comparisons. i would be zero. As words. some test might want to find out whether:
(A is greater than B) AND (A is greater than C)
Expressions and Types
98
. For instance. 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 a part of the first group of operators too. 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.

thequestion is therefore always true. The logical operators. whatever the value of a variable tobe the result of
thequestion = tobe || !tobe
must always be true. as they are called. Fortunately this is not a matter of life or death! Node:Summary of Operators and Precedence. The NOT operator always creates the logical opposite: !true is false and !false is true.
Operator () [] ++ -(type) * & ~ ! Operation parentheses square brackets increment decrement cast operator the contents of the address of unary minus one's complement logical NOT Evaluated. Previous:Expressions and Types. 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
Comparisons and Logic
99
. On or the other of these must be true. Next:Questions 16. 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. Up:Assignments Expressions and Operators
Summary of Operators and Precedence
The highest priority operators are listed first.C Programming Tutorial C does not have words for these operations but gives symbols instead.

5. What is an operand? 2. Write a short statement which assigns the remainder of 5 divided by 2 to a variable called "rem". print out "Thank goodness for mathematics!" Node:Decisions. Previous:Summary of Operators and Precedence. Up:Assignments Expressions and Operators
Questions
1. 3. 4.C Programming Tutorial
* / % + >> << > >= <= < == != & ^ | && || = += -= *= /= %= >>= <<= &= ^= |= 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 AND exclusive OR inclusive OR AND OR 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 right right right right right right right right right right right to to to to to to to to to to to to to to to to right right right right right left left left left left left left left left left left
assign add assign subtract assign multiply assign divide assign remainder assign right shift assign left shift assign AND assign exclusive OR assign inclusive OR assign
Node:Questions 16. Up:Top
Summary of Operators and Precedence
100
. Previous:Assignments Expressions and Operators. Write in C: if 1 is not equal to 23. Write a statement which prints out the remainder of 5 divided by 2. Write a statement which subtracts -5 from 10. Next:Loops.

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

. In fact. do what is in the braces.
or
if (condition) { compound statement }
Notice that. Up:Decisions
if
The first form of the if statement is an all or nothing choice.C Programming Tutorial
/* do something else */
3:
switch (choice) { case first_possibility : /* do something */ case second_possibility : /* do something */ . as well as a single statement. a whole block of statements can be written under the if statement.. if some condition is satisfied.. Some of the following examples help to show this:
int i. }
• if: • example f1: • if else: • Nested ifs and logic: • Example 14: • Stringing together if. Next:example f1.
if
102
. this is written:
if (condition) statement. like the ones discussed in the previous chapter.else: • switch: • Example 15: • To try: Node:if. If the condition has the value `true' then the statement or compound statement following the condition will be carried out. A condition is usually some kind of comparison. It must have a value which is either true or false (1 or 0) and it must be enclosed by the parentheses ( and ). Formally.. otherwise just skip what is in the braces. A compound statement is a block of single statements enclosed by curly braces. otherwise it will be ignored. a compound statement will do instead. Previous:Decisions. there is an unwritten rule of thumb in C that wherever a single statement will do.

described after some example listings. } if (i < 0) { printf ("The number was negative"). scanf ("%ld". Next:if else. if (i > 0) printf ("The number was positive"). even when they are not strictly required. }
The same code could be written more briefly.. Node:example f1.C Programming Tutorial
printf ("Type in an integer"). It also has the appeal that it makes if statements look the same as all other block statements and it makes them stand out clearly in the program text. } if (i > 0) { printf ("The number was positive"). if (i == 0) printf ("The number was zero").&i). #1 */ /* */ /*****************************************/
Example Listings
103
.
The preference in this book is to include the block braces. Up:Decisions
Example Listings
/*****************************************/ /* */ /* If.&i). This is the purpose of the else statement. scanf ("%ld". for instance. it could not decide for the traveller whether to take the left fork or the right fork of his road. but perhaps less consistently in the following way:
int i. This does no harm. but very often you will find that some extra statements have to go into those braces. printf ("Type in an integer").. 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. so it is as well to include them from the start.. it could only tell him whether to get up and go at all. if (i == 0) { printf ("The number was zero"). if (i < 0) printf ("The number was negative"). It is no more or less efficient. To do much more for programs it needs to be extended. Previous:if.

The if. Next:Example 14. scanf ("%ld". When it is executed.else structure. the condition is evaluated and if it has the value `true' (i. The weary traveller above might make a decision such as:
if (rightleg > leftleg) { take_left_branch(). if (i > 0) { printf ("That number was positive!").C Programming Tutorial
}
The if.. }
or:
if (i > 2) { if (i < 4) { printf ("i is three"). That is.e.. }
Nested ifs and logic
106
. If the condition is `false' (or zero) then statement2 is executed. } else { take_right_branch(). Previous:if else. that block would not have been executed unless i were NOT greater than zero.. For instance:
int i. } else { printf ("That number was negative or zero!").else statement is a two way branch: it means do one thing or the other. }
It is not necessary to test whether i was negative in the second block because it was implied by the if. Up:Decisions
Nested ifs and logic
Consider the following statements which decide upon the value of some variable i.i). not zero) then statement1 is executed. Their purposes are exactly the same.
if ((i > 2) && (i < 4)) { printf ("i is three").else construction often saves an unnecessary test from having to be made. }
Node:Nested ifs and logic.

if i is also less than 4. so I can stop worrying about that now. Now. do what is in the curly braces. In many cases though it is too difficult to think in terms of these operators and the sealed capsule idea begins to look attractive. Inside these curly braces i is always less than 4. This is how programmers learn to think in a structured way. It says: If i is greater than 2. The aim of this demonstration is to show that there are two ways of making multiple decisions in C. Up:Decisions
Example Listing
/***********************************************/ /* */ /* If demo #3 */ /* */ /***********************************************/ #include <stdio. 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. So i is 3. || (AND. if the program gets into the "i is less than 4" braces as well.else. Next:Stringing together if. but they do it in different ways. Using the logical comparison operators &&. Inside these curly braces i is always greater than 2 because otherwise the program would never have arrived inside them. 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. Previous:Nested ifs and logic.. The block braces are like regions of grace: they cannot be penetrated by anything which does not satisfy the right conditions. 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. This is another advantage of using the curly braces: it helps the programmer to see that if statements and if. then i has to be 3.OR) and so on. The second method is more complicated.C Programming Tutorial
}
Both of these test i for the same information. so. then do what is inside the new curly braces. 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. There is only one integer which is bigger than 2 and less than 4 at the same time: it is 3. then both facts must be true at the same time. 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.. Think carefully. Node:Example 14. several multiple tests can be made.else statements are made up of sealed capsule parts.h> /***********************************************/
Example Listing
107
. both at the same time..

It can be used as a different way of writing a string of if .. each of which is followed by a statement or compound statement. break....else. but choosing one path would preclude others. The nested if statements make sure that the second two tests are only made if the first one failed. }
/* optional line */
When a switch statement is encountered. The switch statement can be written more specifically for integers:
switch (integer value) { case 1:
statement1. the expression in the parentheses is evaluated and the program checks to see whether the result of that expression matches any of the constants labelled with case. 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.. break. }
/* optional */
It has an expression which is evaluated and a number of constant `cases' which are to be chosen from. statement2.. in the best possible case. Previous:Stringing together if. Similarly the third test is only performed if the first two failed.C Programming Tutorial
The answer is that the second of these programs can be more efficient. An extra statement called break can also be incorporated into the block at any point. one after the other. Up:Decisions
switch: integers and characters
The switch construction is another way of making a program path branch into lots of different limbs. The second program does not necessarily do this however. If a match is made (for instance. (See the diagram.") execution will start just after that case statement and will carry on until either the closing brace } is switch: integers and characters 110
.. default: default statement break. break is a reserved word. Program one makes every single test.. break..) The switch statement has the following form:
switch (int or char expression) { case constant : statement. Nested loops make a program branch into lots of possible paths. This because it uses the else form of the if statement which in turn means that few things have to be calculated. So the second program could end up doing a third of the work of the first program. Nesting decisions like this can be an efficient way of controlling long lists of decisions like those above.
/* optional line */
case 2:
/* optional line */
. if the expression is evaluated to 23 and there is a statement beginning "case 23 : . Node:switch. . because the program meets every if statement. Next:Example 15.

If there had been a break statement after case 'y' then control would not have been able to reach case 'Y' as well. case 8 : printf break. The return statement does more than break out of switch.C Programming Tutorial
case 4 : printf break. break this gives a way of jumping out of a switch quickly. the function returns false.}. ("-. a break statement is used. Think of a function yes() which gets a character from the user and tests whether it was 'y' or 'Y'.
The program selects one of the printf statements using a switch construction..").. 2. ("... case 9 : printf } } (". ("----. At every case in the switch.."). Try to write a program incorporating the statement if (yes()) {.-"). testing the cases in turn.. Try to make a counter which is reset to zero when it reaches 9999. 3.. Write a program to get a lot of numbers from the user and print out the maximum and minimum of those. If break were not included it would go right on executing the statements to the end. case 7 : printf break. Up:Top
Things to try
112
.. The default option ensures that whatever else the character is."). case 5 : printf break. Node:Loops. it breaks out of the whole function. ("--.").. ("---...").
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.... Next:Arrays. Up:Decisions
Things to try
1. There might be cases where it is not necessary or not desirable to jump out of the switch immediately.. so in this case break was not required. Previous:Example 15. Node:To try. Previous:Decisions. case 6 : printf break. This causes control to jump straight out of the switch statement to its closing brace }.

.. If the expression evaluates to false (or 0) then the instructions in the braces are ignored and the entire while loop ends. in which "while" does not have its commonplace meaning. with some condition deciding when they will stop. They allow the programmer to build a sequence of instructions which can be executed again and again. The first important thing about this loop is that has a conditional expression (something like (a > b) etc. If the value of the expression is true..Loops
Controlling repetitive processes. They are called: • while • do .
Loops
113
. Previous:Loops.) which is evaluated every time the loop is executed by the computer. There are three kinds of loop in C. then it will carry on with the instructions in the curly braces. }
and the statements in the curly braces are executed while the condition has the value "true" ( 1 ). In common language while has a fairly obvious meaning: the while-loop has a condition:
while (condition) { statements. The computer then moves onto the next statement in the program. however. so it is worthwhile explaining the steps which take place in a while loop. Nesting loops Decisions can also be used to make up loops. Next:Example 16. Up:Loops
while
The simplest of the three loops is the while loop. while • for These three loops offer a great amount of flexibility to programmers and can be used in some surprising ways! • while: • Example 16: • Example 17: • do while: • Example 18: • for: • The flexible for loop: • Quitting Loops and Hurrying Them Up!: • Nested Loops: • Questions 18: Node:while.. There are dialects of English. Loops free a program from the straitjacket of doing things only once.

the computer attempts to evaluate the expression inside the parentheses. if the expression is true. not at the end. When the loop is encountered. inside the parentheses. so it calls getchar() which fetches the next character from the input. 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. through and through. getchar() then takes on the value of the character which it fetched from the input file. 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.and it will evaluate the expression on every pass of the loop to check whether or not it is true. This is quite valid: the loop will merely do nothing a certain number of times. This means that there is a comparison to be made. Previous:while. Something which is immediately obvious from listing is that the while loop in skipgarb() is empty: it contains no statements. If they are equal the expression is false. in order to write the skipgarb() function which complemented scanf(). The idea of this function was introduced in the previous section..C Programming Tutorial The second thing to notice about this loop is that the conditional expression comes at the start of the loop: this means that the condition is tested at the start of every `pass'. When the expression eventually becomes false the loop will quit. What happens is the following. the statements inside the braces will not get executed at all . The reason that this is important is this: if the condition has the value false before the loop has been executed even once. Next:Example 17. the while statement will loop and start again . Now. Next the computer finds the != "is not equal to" symbol and the newline character \n. That was:
skipgarb () /* skip garbage corrupting scanf */
{ while (getchar() != '\n') { } }
This is a slightly odd use of the while loop which is pure C.not even once. One example was sneaked into an earlier chapter before its time. It is one instance in which the programmer has to start thinking C and not any other language. It uses a while loop which is always true to repeat the process of getting a response from the user. Node:Example 16. When the response is either yes or no it quits using the return function to jump right out of the loop. The best way to illustrate a loop is to give an example of its use. while 114
.. Up:Loops
Example Listing
Another use of while is to write a better function called yes(). it finds a function call to getchar(). There. The computer compares the character fetched by getchar() with the newline character and if they are `not equal' the expression is true.

printf ("Type in a line of text\n").C Programming Tutorial
skipgarb () { while (getchar() != '\n') { } } /* end */
Node:Example 17.h> main () { char ch. Previous:Example 17.
/***********************************************/ /* */ /* while loop */ /* */ /***********************************************/ /* count all the spaces in an line of input */
#include <stdio. It quits when there is no more input left and printf out the number of spaces.while loop resembles most closely the repeat. short count = 0. Next:do while. } } printf ("Number of space = %d\n"... }
Node:do while. Up:Loops
do. Previous:Example 16. Up:Loops
Example Listing
This example listing prompts the user to type in a line of text and it counts all the spaces in that line..while
The do. Next:Example 18. The do loop has the form:
do
Example Listing
116
. while ((ch = getchar()) != '\n') { if (ch == ' ') { count++.count).until loops of Pascal and BASIC except that it is the `logical opposite'.

This program gets a line of input from the user and checks whether it contains a string marked out with "" quote marks.. the program prints out the contents of the string only. This is the only difference between while and do.. then the two are identical.
/**********************************************/ /* */ /* do .while.while loop will always be executed at least once.. Up:Loops
Example Listing
Here is an example of the use of a do. until" of other languages in the following sense: if the condition is NOTed using the ! operator.while loop is like the "repeat .
The output would then be:
Here we go round the. Next:for. If a string is found.
repeat == until(condition) while (!condition) do
This fact might be useful for programmers who have not yet learned to think in C! Node:Example 18.e. Previous:do while.. before the test is made to determine whether it should continue.h>
do.... while demo */ /* */ /**********************************************/ /* print a string enclosed by quotes " " */ /* gets input from stdin i.. keyboard */ /* skips anything outside the quotes */
#include <stdio.while loop. A do.
If the string has only one quote mark then the error message `string was not closed before end of line' will be printed. } while (condition)
Notice that the condition is at the end of this loop.while
117
. A typical input line might be:
Onceupon a time "Here we go round the....."what a terrible. This means that a do.C Programming Tutorial
{ statements.

Up:Loops
for
The most interesting and also the most difficult of all the loops is the for loop. ch = skipstring().. } } while (ch != '\n') { } } /*************************************************/ /* Level 1 */ /*************************************************/ char skipstring () /* skip a string ".skipstring().. Previous:Example 18.C Programming Tutorial
/*************************************************/ /* Level 0 */ /*************************************************/ main () { char ch. The name for is a hangover from earlier days and other languages. }
Node:for. if (ch == '\n') { printf ("\nString was not closed "). Next:The flexible for loop. do { if ((ch = getchar()) == '"') { printf ("The string was:\n"). putchar(ch). do { ch = getchar(). printf ("before end of line\n")." */ { char ch. } } while (ch != '"') { } return (ch). It is not altogether appropriate for C's version of for. break. The name comes from the typical description of a classic for loop: Example Listing 118
.

i < 20. i <= n.5) { }
The C translation looks peculiar in comparison because it works on a subtly different principle. In C it can be absolutely anything. i = 0. In languages such as Pascal this always means adding or subtracting 1 from the variable. e. A for loop normally has the characteristic feature of controlling one particular variable. The condition is evaluated at the beginning of every loop and the loop is only carried out while this expression is true. these expressions have the following significance. repeat the following sequence of commands. statement2 This is some kind of expression for altering the value of the control variable. Compare a C for loop to the BASIC for loop. statement1 This is some kind of expression which initializes the control variable. It does not contain information about when it will stop. The form of the for loop is:
for (statement1. x <= 10.. e. This statement is only carried out once before the start of the loop. i++ or i *= 20 or i /= 2.g.. 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.. Here is an example in which the loop counts from 0 to 10 in steps of 0. The for loop has plenty of uses.C Programming Tutorial For all values of variable from value1 to value2 in steps of value3.g. That variable is somehow associated with the loop. instead it contains information about when it should be looping.5:
FOR X = 0 TO 10 STEP 0. called the control variable.. as the BASIC one does. statement2) { }
For normal usage. The result is that a C for loop often has the <= symbol in it. For example it might be a variable which is used to count "for values from 0 to 10" or whatever. condition This is a condition which behaves like the while loop. It could be used to find the sum of the first n natural numbers very simply:
sum = 0.. i++) {
for
119
. x += 0.5 NEXT X
for (x = 0. condition. it is actually based upon the while construction.3 . e.g. for (i = 0.

Next:Quitting Loops and Hurrying Them Up!. x <= 1000. Look at the loop again:
for (statement1. }
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 = '*'. The loop starts off by initializing ch with a star character.. j= %d\n".i. j--) { printf("i = %d.. ch != '\n'. Consider the following loop:
for (x = 2. to describe what goes into a for loop. It is also possible to combine several incremental commands in a loop:
for (i = 0. It checks that ch != '\n' (which it isn't. i < j. Up:Loops
The flexible for loop
The word `statement' was chosen carefully. } } return TRUE.j). x = x * x) { . ) { } /* while ?? */
Here there are two empty statements. which are just wasted. C will accept any statement in the place of those above. including the empty statement. This flexibility can be put to better uses though.. statement2) { }
Statement really means what it says. Previous:for. }
Node:The flexible for loop. first time around) and proceeds with the loop. condition. ch is reassigned by calling the function getchar(). i++. above. ch = getchar()) { }
This could be used to make yet another different kind of skipgarb() function. On each new pass.
The flexible for loop
121
.C Programming Tutorial
{ if (i % j == 0) { return FALSE. The while loop could be written as a for loop!
for (. j=10. condition.

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. Up:Loops
Quitting Loops and Hurrying Them Up!
C provides a simple way of jumping out of any of the three loops above at any stage. The statement which performs this action is the same statement which was used to jump out of switch statements in last section. i++) { if (i == 12) { break. but in the right hands. if (ch == '\n')
Quitting Loops and Hurrying Them Up!
122
. Next:Nested Loops. } }
This is so nerve shattering that many languages forbid it outright. It is not only the statements which are flexible.
If this statement is encountered a loop will quit where it stands. There is no good rule for when to do this. i++) { if (i == 9) { number = 30. } }
Still another way of making skipgarb() would be to perform the following loop:
while (TRUE) { ch = getchar().
break. Node:Quitting Loops and Hurrying Them Up!. number = 20. Previous:The flexible for loop.
int i. is not often a very good idea to use this facility.C Programming Tutorial
}
Statement2 can be any statement at all which the programmer would like to be executed on every pass of the loop. whether it has finished or not. Why not put that statement in the curly braces? In most cases that would be the best thing to do. except to say: make you code as clear as possible. an expensive way of assigning i to be 12 would be:
for (i = 1. it is a powerful one to have around. for (i = 0. To be sure. i <= 20. For instance. but in special instances it might keep a program tidier or more readable to put it in a for loop instead. i <= number.

This might be useful to avoid dividing by zero in a program:
for (i = -10.C Programming Tutorial
{ break.
Nested Loops
123
. A for loop controls the number of times that a particular set of statements will be carried out.h> #define SIZE 10
/*****************************************/ main () { int i. The idea of nested loops is important for multi-dimensional arrays which are examined in the next section. } printf ("%d".j. } }
Of course. i <= 10. a programmer might want to hurry a loop on to the next pass: perhaps to avoid executing a lot of irrelevant statements. Up:Loops
Nested Loops
Like decisions.
When a continue statement is encountered. }
Node:Nested Loops. which jumps right out of a whole function. the example below shows how a square could be printed out using two printf statements and two loops. so it is less drastic. loops will also nest: that is. Another outer loop could be used to control the number of times that a whole loop is carried out. another way to do this would be to use the return() statement.
/*****************************************/ /* */ /* A "Square" */ /* */ /*****************************************/ #include <stdio. C gives a statement for this too. i++) { if (i == 0) { continue. it is most commonly used with the for loop. because this is easiest to control. 20/i). Although this feature will work with any loop at all. Previous:Quitting Loops and Hurrying Them Up!. Next:Questions 18. a loop will stop whatever it is doing and will go straight to the start of the next loop pass. loops can be placed inside other loops. break only jumps out of the loop. As well as wanting to quit a loop. called:
continue. To see the benefit of nesting loops. for instance.

Node:Arrays. Previous:Loops. } printf ("\n"). Which of the loops is always executed once? 4. Write a program to get 10 numbers from the user and add them together. Next:Strings. j++) { printf("*"). Up:Top
Questions
124
. Write a program which copies all input to output line by line. When is the condition tested in each of the loops? 3. i <= SIZE. } }
The output of this program is a "kind of" square:
********** ********** ********** ********** ********** ********** ********** ********** ********** **********
Node:Questions 18. j <= SIZE. 5.C Programming Tutorial
for (i = 1. Previous:Nested Loops. Up:Loops
Questions
1. and what are they? 2. How many kinds of loop does C offer. i++) { for (j = 1.

they can be one dimensional. This usually means that all the elements in the array will be set to zero. to initialize the array before the program truly begins. • Why use arrays?: • Limits and The Dimension of an array: • Arrays and for loops: • Example 19: • Arrays Of More Than One Dimension: • Arrays and Nested Loops: • Example 20: • Output of Game of Life: • Initializing Arrays: • Arrays and Pointers: • Arrays as Parameters: Arrays 125
.Arrays
Rows and tables of storage. storage is allocated for them. When arrays are declared inside a function. It is usually necessary. Arrays are like pigeon holes or chessboards. to prepare it for use.
Notice that there is no space between the square bracket [ and the name of the array.) The reason for this will become clear later.
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. with each compartment or square acting as a storage place. This statement would cause space for three integers type variables to be created in memory next to each other as in the diagram below. two dimensional or more dimensional! An array is defined using square brackets []. but that storage space is not initialized: that is. Arrays are a convenient way of grouping a lot of variables under a single variable name. 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. For example: an array of three integers called "triplet" would be declared like this:
int triplet[3]. Also notice that every element in an array is of the same type as every other. the memory space contains garbage (random values). therefore. It is not (at this stage) possible to have arrays which contain many different data types.

/***********************************************/ /* */ /* Census */ /* */ /***********************************************/ #include <stdio. It is far easier to initialize an array than it is to initialize twenty or so variables. in cases where it would be completely impractical to have a different name for every storage space in the memory.g. arrays can be initialized very easily indeed. Previous:Arrays. index < 3. they could easily be linked to the elements in an array. The first of these reasons is probably the most important one. e. as far as C is concerned. By defining macros for the names of the different cars. for (index = 0. Next:Limits and The Dimension of 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. 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. since information can be stored in other ways with equally simple initialization facilities in C. Up:Arrays
Why use arrays?
Arrays are most useful when they have a large number of elements: that is. index++) { type[index] = 0. int index. One example of the use of an array might be in taking a census of the types of car passing on a road.C Programming Tutorial • Questions 19: Node:Why use arrays?. } while (NOTFINISHED) {
Why use arrays?
126
. • In C.h> #define #define #define #define NOTFINISHED CAR AUTO BIL 1 0 1 2
/************************************************/ main () { int type[3].

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

Up:Arrays
Arrays and for loops
Arrays have a natural partner in programs: the for loop. array[size]. Consider a one dimensional array called array. Node:Arrays and for loops. 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 Arrays and for loops 128
.
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 | ---------------------------------------
The for loop can be used to work on an array sequentially at any time during a program. i++) { array[i] = i. i < SIZE. Writing over the bounds of an array is a common source of error. array[SIZE]. Previous:Limits and The Dimension of an array. Consider:
#define SIZE main () { int i. } } 10. so that all its elements contain zero:
#define SIZE main () { int i. } } 10.C Programming Tutorial The second of these tends to be the result on operating systems with proper memory protection. The example listing below shows an example of how this might work for a one dimensional array. The for loop provides a simple way of counting through the numbers of an index in a controlled way. This sieve is an array which is used for weeding out prime numbers. not only when it is being initialized. A for loop can be used to initialize the array. that is: numbers which cannot be divided by any number except 1 without leaving a remainder or a fraction. Remember that the array limits run from zero to the size of the array minus one. i++) { array[i] = 0. i < size.
It could equally well be used to fill the array with different values. for (i = 0. Next:Example 19. for (i = 0. called an Eratosthenes sieve.

) An array of two dimensions could be declared as follows:
float numbers[SIZE][SIZE]. to the number of indicies which an array can have.sieve[SIZE]. } else { printf ("%5d". for (j = i*2. i < SIZE. j < SIZE. } } /* end */ /* Delete. { short j. (Though there is a limit to the amount of memory available for their storage. Next:Arrays and Nested Loops.. of an integer */ /* Print out array */
Node:Arrays Of More Than One Dimension.sieve[i]). mult = 2.sieve) short i.
Arrays Of More Than One Dimension
130
. in principle. for (i = 2.C Programming Tutorial
{ continue. Previous:Example 19. j = i * (mult++)) { sieve[j] = DELETED. } } /***********************************************************/ PrintPrimes (sieve) short sieve[SIZE]. i++) { if (sieve[i] == DELETED) { continue.sieve). { short i. Up:Arrays
Arrays Of More Than One Dimension
There is no limit. } DeleteMultiplesOf(i. } } } /***********************************************************/ /* Level 2 */ /***********************************************************/ DeleteMultiplesOf (i.

because a two-dimensional array is always thought of as. For instance to set the element 2.C Programming Tutorial SIZE is some constant.. 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 second index varies fastest.3 to the value 12. one would write:
array[2][3] = 12. The elements are accessed by giving the coordinates of the element in the grid. or two labels in square brackets. It has (SIZE * SIZE) or size-squared elements in it. like a chess board. .
-----------------------------------| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | .. which form an imaginary grid.. (Rows go along. (up to SIZE) -----------------------------------| 1 | | | | | | | | | -----------------------------------| 2 | | | | | | | | | -----------------------------------| 3 | | | | | | | | | -----------------------------------| 4 | | | | | | | | | -----------------------------------| 5 | | | | | | | | | -----------------------------------| 6 | | | | | | | | | -----------------------------------| 7 | | | | | | | | | -----------------------------------. (The sizes of the two dimensions do not have to be the same.
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. Arrays are therefore stored in rows. Arrays Of More Than One Dimension 131
.. in which every square is a variable or storage area.) An array cannot be stored in the memory as a grid: computer memory is a one dimensional thing. columns hold up the ceiling. (up to SIZE)
Every element in this grid needs two indicies to pin-point it.) This is called a two dimensional array because it has two indicies.

Up:Arrays
Arrays and Nested Loops
Arrays of more than one dimension are usually handled by nested for loops. i < SIZE1. like a cube or a cuboid. Node:Arrays and Nested Loops. That means the column index goes from 0.C Programming Tutorial
array[row][column]
so for every row stored.k. j++) { array[i][j] = 0. j < SIZE2.SIZE inside every row. so it is changing faster as the line of storage is followed.
or with different limits on each dimension:
short notcubic[2][6][8]. for (i = 0. could also be defined in the same kind of way:
double cube[SIZE][SIZE][SIZE]. float array[SIZE1][SIZE2].
Three dimensional arrays are stored according to the same pattern as two dimensional arrays. i++) { for (j = 0. Next:Example 20. A two dimensional array might be initialized in the following way:
main () { int i. A three dimensional array.j. k++) {
Arrays and Nested Loops
132
. Previous:Arrays Of More Than One Dimension. i < SIZE1.j. float array[SIZE1][SIZE2][SIZE3].. They are kept in computer memory as a linear sequence of variable stores and the last index is always the one which varies fastest. there will be lots of columns inside that row. j < SIZE2. } } }
In three dimensions. j++) { for (k = 0. three nested loops would be needed:
main () { int i. i++) { for (j = 0. for (i = 0. k < SIZE3.

The aim is to mimic something like cell reproduction by applying some rigid rules to a pattern of dots .\n"). Up:Arrays
Example Listing
/*********************************************************/ /* */ /* Game of Life */ /* */ /*********************************************************/ /* /* /* /* /* /* /* Based upon an article from Scientific American in 1970. BuildNextGeneration(array.
Example Listing
133
.count). RETURN to continue.C Programming Tutorial
array[i][j][k] = 0. 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. or there won't be enough food! */ */ */ */ */ */ */
#include <stdio.++generation). Simulates the reproduction of cells which depend on one another.count). UpdateDisplay(array. } } } }
An example program helps to show how this happens in practice. printf ("Game of Life\n\n\n"). Node:Example 20.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]. and stars *. The rules are that cells will only survive if they have a certain number of neighbours to support them but not too many. printf ("\n\nQ for quit. while (NORESPONSE) { CountNeighbours(array. InitializeArray(array). if(quit()) break. Previous:Arrays and Nested Loops. The rules will be clear from the listing. Next:Output of Game of Life. int generation = 0. The example below demonstrates the so-called "Game of Life". 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.

....
Initializing Arrays
139
........ Try experimenting with different starting patterns...........*.... . ..... ......
Q for quit.......0} }... ...... A 3 by 3 array could be initialized in the following way:
static int array[3][3] = { {10.0}...C Programming Tutorial
Generation 4 ... C provides another method.. .......* *....* *.....
or perhaps with the aid of one or more for loops........*.....*. ....*****...... The first way is by assigning every element to some value with a statement like:
array[2] = 42. ........................... . ........ This method only works for static variables and external variables.. to initialize the values of each element to as different value. ........... Node:Initializing Arrays........*. {1. ......***... to say the least..................* .23...............................*....... .... ....42}. . RETURN to continue... Recall that arrays are stored row-wise or with the last index varying fastest....***.*. array[3] = 12............ {40652.................................. *.. Previous:Output of Game of Life.....*.....654...22..*... ...************. Because it is tedious......*.*........... not to mention uneconomical. which employs a single assignment operator = and curly braces { }. Next:Arrays and Pointers..................*.............. Up:Arrays
Initializing Arrays
Arrays can be initialized in two ways...
etc.....**......*..

or by writing
*numbers = 22. An array can be accessed with pointers as well as with [] square brackets. Note that. but help to distinguish the rows from the columns. Node:Arrays and Pointers.23.
Take care to include the semicolon at the end of the curly brace which closes the assignment. For example: if an array is declared
float numbers[34].) So the first element of the array could be accessed by writing:
numbers[0] = 22.C Programming Tutorial The internal braces are unnecessary.0 40652.
For character arrays.0 }. if there are not enough elements in the curly braces to account for every single element in an array.42.
char arrayname[5]. whereas auto or local storage is created at run time. Next:Arrays as Parameters. 1. char *ptr. There is another way of looking at arrays which follows the BCPL idea of an array as simply a block of memory. The same thing could be written:
int array[3][3] = { 10. 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.
then numbers is a pointer to the first floating point number in the array. numbers is a pointer in its own right. standing alone. is actually a pointer to the first element in the array. (In this case it is type `pointer to float'. The name of an array variable. Static variables are always guaranteed to be initialized to zero anyway.3. this gives an alternative way of getting at the elements in the array.3. Up:Arrays
Arrays and Pointers
The information about how arrays are stored was not included just for interest.654. ptr++) {
Arrays and Pointers
140
. the remaining elements will be filled out with zeros. for (ptr = arrayname.22. ptr <= arrayname+4. Previous:Initializing Arrays. which are dealt with in some depth in chapter 20.

Next:Questions 19. if the microcomputer is multi-tasking.
Be warned! Node:Arrays as Parameters. So
ptr = arrayname + 5. 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. because the name of the array is a pointer to the array. This is a simple matter. }
The code above sets the array arrayname to zero. Arrays can be passed as parameters. or the program is running on a larger system which has a limited manager. Notice from that program how the declarations for the parameters are made. function (array)..
might not be a pointer to the fifth character in a character array. Previous:Arrays and Pointers.. This could be found instead using the & operator. ... where it can find it. If a program is running on a normal microcomputer. but only as variable ones. passed as a parameter. then memory ceases to be something which can be thought of as a sequence of boxes standing next to one another. The Game of Life program above does this. { }
Any function which writes to the array. } function (arrayformal) char arrayformal[23]. will affect the original copy.C Programming Tutorial
*ptr = 0. then there should be few problems with this alternative method of handling arrays. Up:Arrays
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. A multi-tasking system shares memory with other programs and it takes what it can find.
main () { char array[23]. This method of getting at array data is not recommended by this author except in very simple computer environments. Array parameters are always variable parameters Arrays as Parameters 141
. On the hand. A pointer to the fifth element can be reliably found with:
ptr = &(arrayname[5]).

Next:Putting together a program.C Programming Tutorial Node:Questions 19. how would you find a pointer to the start of it? 2. What numbers can be written in the indicies of the array? Node:Strings. Write a statement which declares an array of type double which measures 4 by 5. 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? 3. Previous:Arrays as Parameters. Previous:Arrays. Up:Top
Questions
142
. Given any array. Up:Arrays
Questions
1.

Here are some declarations for strings which are given without immediate explanations. \r. char arraystr[SIZE].."
• Conventions and Declarations: • Strings Arrays and Pointers: • Arrays of Strings: • Example 21: • Strings from the user: • Handling strings: • Example 22: • String Input/Output: • Example 23: • Questions 20: Node:Conventions and Declarations. on the hand.
main () { char *auto_string.Strings
Communication with arrays.g.
/**********************************************************/ /* */ /* String Declaration */ /* */ /**********************************************************/ #define SIZE 10
char *global_string1..... In C a string is represented as some characters enclosed by double quotes.
"This is a string"
A string may contain any character. Previous:Strings.. are enclosed by double quotes e." and have the type "pointer to char" (char *) or array of char. The convention is that single characters are enclosed by single quotes e.g. Up:Strings
Conventions and Declarations
There is an important distinction between a string and a single character in C. Strings are pieces of text which can be treated as values for variables.
"Beep! \7 Newline \n. Next:Strings Arrays and Pointers. Strings. "string.
Strings
143
. \7 etc. * and have the type char. including special control characters. char global_string2[SIZE]. such as \n.

if there is time to waste.
/**********************************************************/ /* */ /* String Initialization */ /* */ /**********************************************************/ char *global_string1 = "A string declared as a pointer". The explanation follows. the other is as a pointer to some pre-assigned array. not one which in typed in by the user. Previous:Conventions and Declarations. Up:Strings
Strings. 'o'. but it is so laborious that C provides a special initialization service for strings. main () { char string[LENGTH]. one of these is as an array of characters. 'u'.) They are written into a short compilable program below. The fact that a string is an array of characters might lead you to write something like:
#define LENGTH 9. char global_string2[] = "Declared as an array". }
This method of handling strings is perfectly acceptable. '\0'. which bypasses the need to assign every single character with a new assignment!. Next:Arrays of Strings. 'i'. Strings can be declared in two main ways.C Programming Tutorial
static char *stat_strng.
printf ("%s". '!'. string[0] string[1] string[2] string[3] string[4] string[5] string[6] string[7] string[8] = = = = = = = = = 'T'. string).
Conventions and Declarations
144
. Think of how a string called string might be used to to store the message "Tedious!". 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. 's'. Arrays and Pointers
A string is really an array of characters. The end marker is called the zero (or NULL) byte because it is just a byte which contains the value zero: \0. (A constant string is one which is actually typed into the program. }
Node:Strings Arrays and Pointers. 'd'. Programs rarely gets to see this end marker as most functions which handle strings use it or add it automatically. 'e'. Perhaps the simplest way of seeing how C stores arrays is to give an extreme example which would probably never be used in practice. static char statarraystr[SIZE]. There are six ways of assigning constant strings to arrays.

Static variables are assigned storage space in the body of a program when the compiler creates the executable code. global_string1.. so they can be initialized at compile time. } /* end */
The details of what goes on with strings can be difficult to get to grips with. This is a slightly round about way of defining the string constant. Notice that there is no size declaration in the square brackets. filling the string into that space. printf ("%s %s". global_string2).". Remember also that the name of the array is a pointer to the first character.. it is a pointer to a global. In fact this is what is done in the fourth example. This means that they are saved on disk along with the program code. so. printf ("%s %s %s". It is a good idea to get revise pointers and arrays before reading the explanations below. adds a zero byte to the end of it and assigns a pointer to its first character to the variable called global_string1. Notice the diagrams too: they are probably more helpful than words. */ char arraystr[20]. but this is just a matter of style. More correctly. The first statement allocates space for a pointer to an array. in fact. Notice that. IS ILLEGAL! */ /* This is because the array is an "auto" type */ /* which cannot be preinitialized.. Arrays and Pointers
145
. only this time.. the declaration is inside the function main() so the type is not static but auto. statarraystr)..
/* char arraystr[] = "initializer. the pointer is created and when it ends. The normal thing to do would be to declare the string pointer as being static... 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. static variable. is typed into the program. with the exception that. which is to be initialized. stat_strng.. the compiler can also allocate space for that in the executable file too. 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. 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. static array.C Programming Tutorial
main () { char *auto_string = "initializer. static char statarraystr[] = "initializer. along with its end marker as it goes..". In fact the compiler stores the string.. auto_string. the two methods are identical. The third expression is the same kind of thing. The first of these assignments is a global. because the string which is to be assigned to it. The difference between this and the other two declarations is that this pointer variable is created every time the function main() is called. but."..
Strings. That is the reason for the rule which says that only static arrays can be initialized with a constant expression in a declaration.". it is destroyed. The string which initializes it is stored in the executable file of the program (because it is typed into the text).. The second statement works almost identically. this time the compiler sees the declaration of a static array. static char *stat_strng = "initializer.

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. because there is nothing for the compiler to count the size of). Next:Example 21. " | ~~~~~~~~~~~~ |\n". or arrays of arrays! The same thing works well for strings which are declared static. " | (q) Quit |\n". " | |\n". 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. Up:Strings
Arrays of Strings
In the previous chapter we progressed from one dimensional arrays to two dimensional arrays. " | (1) Edit Defaults |\n". " | |\n".menutext(str_number)). auto-arrays cannot be initialized with a string because they would have to be re-initialized every time the array were created: that is. " | Please Enter Choice |\n". } } /*********************************************************/ char *menutext(n) int n. for (str_number = 0. each time the function were called. " | (4) Bill Calculator |\n". str_number++) { printf ("%s". but it is made in the interests of efficiency. 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! Node:Arrays of Strings. /* return n-th string ptr */
Arrays of Strings
146
. in practice to other static types.\n".C Programming Tutorial The fifth example is again identical. The array declared is of type auto: this means that the whole array is created when the function is called and destroyed afterwards. " | ++ MENU ++ |\n". " | (3) Print Log Sheet |\n". Previous:Strings Arrays and Pointers. { static char *t[] = { " -------------------------------------. " | (2) Print Charge Sheet |\n". but is written as an `open' array with an unspecified size. Here an auto array of characters is declared (with a size this time. if the programmer were inclined to do so. The sixth example is forbidden! The reason for this might seem rather trivial. str_number < 13. The final example could be used to overcome this.

See Example 15. Up:Strings
Example Listing
/************************************************/ /* */ /* static string array */ /* */ /************************************************/ /* Morse code program.h> #define CODE 0 /*************************************************/ main () { short digit. Morse (digit). Node:Example 21.\n" }.. /* print out Morse code */
/* index starts at 0 */
Example Listing
147
. { static char *code[] = { "dummy". return (t[n])..9"). Enter a number and */ /* find out what it is in Morse code */ #include <stdio. return (CODE). Next:Strings from the user. }
Notice the way in which the static declaration works. scanf ("%h".&digit). It is initialized once at compile time. } /************************************************/ Morse (digit) short digit.9"). This function retains the pointer information from call to call. " -------------------------------------.C Programming Tutorial
" | |\n". printf ("Enter any digit in the range 0. The Morse coder program could be rewritten more economically using static strings. if ((digit < 0) || (digit > 9)) { printf ("Number was not in range 0. Previous:Arrays of Strings. so there is effectively only one statement in this function and that is the return statement. } printf ("The Morse code of that digit is ").

The function strlen() is a standard library function which is described below. Up:Strings
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.. printf ("%s\n".code[digit]). • Use the string in that array.. so it has not been necessary to worry about where they were stored. The function filename() asks the user to type in a filename....". 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. which can be used by an application.. But how will the program know in advance how much array space to allocate to these strings? The answer is that it won't.--". }
Node:Strings from the user.-".. "----.. ". } while (strlen(filenm) == 0)... ".. do { printf ("Enter filename :")...filenm). }
The string is made static and given an initializing expression and this forces the compiler to make some space for the string...". ".. skipgarb()..". }...----".... "-. Often though we would like to fetch a string from the user and store it somewhere in the memory for later use.."...".---". "... "--..C Programming Tutorial
"-----". It makes exactly 24 characters plus a zero byte in the program file. but that it doesn't matter at all! One way of getting a simple.. it returns the Strings from the user 148
. for loading or saving by and return it to a calling function...
char *filename() { static char *filenm = "... scanf ("%24s".. return (filenm). Notice that the conversion string in scanf prevents the characters from spilling over the bounds of the string. Next:Handling strings. Previous:Example 21.. Another way is to define a static string with an initializer as in the following example.....". "---. single string from the user is to define an array and to read the characters one by one. It might even be necessary to get a whole bunch of strings and store them all. ".

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

s1 > s2. not including the NULL byte end marker.*from. char *string. Node:Handling strings. char *s1. An example is:
int len. again. strcmp() This function compares two strings and returns a value which indicates how they compared. value = strcmp(s1. Note that numbers come before letters in the ASCII code sequence and also that upper case comes before lower case.from). An example is
char *to. An example:
int value. First of all. it really depends upon the compiler. Chances are. Next:Example 22. alphabetically. Here is a short list of some common ones which are immediately relevant (more are listed in the following chapter). but.
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. Previous:Strings from the user.C Programming Tutorial • The return code is zero if the release was successful. Use this function in preference to custom routines: it is set up to handle any peculiarities in the way data are stored. this function indicates the (ASCII) alphabetical order of the two. strstr() Tests whether a substring is present in a larger string
Handling strings
150
.s2).
The value returned is 0 if the two strings were identical. If s1 < s2 then the value is < 0. If the strings were not the same. then the value is > 0. An example of how strings can be created using malloc() and free() is given below. len = strlen (string). some explanation of Standard Library Functions is useful to simplify the program. strlen() This function returns a type int value.*s2. Up:Strings
Handling strings
The C Standard Library commonly provides a number of very useful functions which handle strings. a good compiler will support a lot more than those listed below. to = strcpy (to.
strcpy() This function copies a string from one place to another. which gives the length or number of characters in a string.

(Notice that this block of memory is one byte longer than the value returned by strlen(). Node:Example 22. such as de-allocating the memory which is no longer needed. It works as follows: The user is prompted for a string which he/she types into a buffer. Check the compiler manual! */ #define NOOFSTR #define BUFSIZE #define CODE 10 255 0
/******************************************************/ /* Level 0 */ /******************************************************/
Example Listing
151
. }
strncpy() This function is like strcpy. Next:String Input/Output. but limits the comparison to no more than n characters.
/******************************************************/ /* */ /* String storage allocation */ /* */ /******************************************************/ #include <stdio. This process is repeated for each of the 10 strings.) malloc() returns a pointer to the space allocated. if (n = strstr(s1.s2)) { printf("s2 is a substring of s1. Finally the strings is copied from the buffer to the new storage with the library function strcpy(). but limits the copy to no more than n characters. while at the same time remembering to perform all a programmer's duties. because strlen() does not count the end of string marker \0. The length of the string is tested with strlen() and a block of memory is allocated for it using malloc(). starting at %d". More string functions are described in the next section along with a host of Standard Library Functions.C Programming Tutorial
int n. Up:Strings
Example Listing
This program aims to get ten strings from the user. The strings may not contain any spaces or white space characters.n). QuitSafely() uses the function exit() which should be provided as a standard library function. exit() allows a program to end at any point. strncmp() This function is like strcmp. Previous:Handling strings.h> /* #include another file for malloc() and */ /* strlen() ???. which is then stored in the array called array. char *s1.*s2. The reason for doing this is to exit from the program neatly. Notice that the program exits through a low level function called QuitSafely().

) is that it will read spaces in strings. Often it is implemented as a macro with poor bounds checking and can be exploited to produce memory corruption by system attackers. buffer[SIZE]. Up:String Input/Output
gets()
This function fetches a string from the standard input file stdin and places it into some buffer which the programmer must provide. NOTE: there are valid concerns about using this function. Next:puts(). until it finds a NULL end of string marker. gets() quits reading when it finds a newline character: that is.
If the routine is successful in getting a string. Next:sprintf(). use fgets() instead. some special library functions for reading and writing are provided for them. when the user presses RETURN.C Programming Tutorial
String Input/Output
Because strings are recognized to be special objects in C. Node:puts(). strptr = gets(buffer). These make it easier to deal with strings. There are four of these functions:
gets() puts() sprintf() sscanf()
• gets(): • puts(): • sprintf(): • sscanf(): Node:gets().
#define SIZE 255
char *sptr. instead a newline character is written.
char *string. The advantage of gets() over scanf("%s". Previous:String Input/Output. The NULL byte is not written to stdout. it returns the value buffer to the string pointer strptr. In order to write more secure code. without the need for special user-routines.
String Input/Output
153
. Otherwise it returns NULL (==0). int returncode. returncode = puts(string). whereas scanf() usually will not. Previous:gets(). Up:String Input/Output
puts()
puts() sends a string to the output file stdout..

C Programming Tutorial 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. Node:sprintf(), Next:sscanf(), Previous:puts(), Up:String Input/Output

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 carefully 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......));

Node:sscanf(), Previous:sprintf(), Up:String Input/Output

sscanf()
This function is the complement of sprintf(). It reads its input from a string, as though it were an input file.
int n; char *sp; n = sscanf (sp,"control string", pointers...);

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 '\0'). 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(). Node:Example 23, Next:Questions 20, Previous:String Input/Output, Up:Strings

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. Node:Putting together a program, Next:Special Library Functions and Macros, Previous:Strings, Up:Top

Example Listing

155

Putting together a program
Putting it all together. • argc and argv: • getopt: • envp: Node:argc and argv, Next:getopt, Previous:Putting together a program, Up:Putting together a program

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 programs. 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] cc argv[1] Putting together a program 156

Node:getopt, Next:envp, Previous:argc and argv, Up:Putting together a program

Processing options
getopt Node:envp, Previous:getopt, Up:Putting together a program

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 argument vector

157

C Programming Tutorial 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. 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);

Special Library Functions and Macros
Checking character types. Handling strings. Doing maths. C provides a repertoire of standard library functions and macros for specialized 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. • Character Identification: • Example 24: • Output 24: • String Manipulation: • Example 25: • Mathematical Functions: • Examples 26: • Maths Errors: • Example 27: • Questions 21: Node:Character Identification, Next:Example 24, Previous:Special Library Functions and Macros, Up:Special Library Functions and Macros

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 Preprocessor. 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. Alphabetic 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)

Special Library Functions and Macros

159

isascii(ch). i. alphabetic or digit. a TAB character or a newline. such as digits. Node:Example 24.. toupper(ch) This converts the character ch into its upper case counterpart. /* A criminally long main program! */
Character Identification
160
.h> #include <ctype. Previous:Character Identification. or characters which do not have a particular case.. if the character is printable (excluding the space) iscntrl(ch) Returns true if the character is a control character.e.e. ch++
/********************************************************/ main () { char ch.127. Next:Output 24. it has a code in the range 0. isascii(ch) Returns true if the character is a valid ASCII character: that is. isalnum(ch) Returns true if a character is alphanumeric: that is.. isprint(ch) Returns true if the character is printable: that is.. It does not affect characters which are already lower case. ispunct(ch) Returns true if ch is a punctuation character.h> #define ALLCHARS
/* contains character utilities */ ch = 0.9 or a letter a.127: that is. tolower(ch) This converts a character into its lower case counterpart. a valid ASCII character.f or A.C Programming Tutorial Returns true if the character was a valid hexadecimal digit: that is. This does not affect characters which are already upper case. a number from 0.. ASCII values 0 to 31 and 127. toascii(ch) This strips off bit 7 of a character so that it is in the range 0. isgraph(ch) Returns true if the character is graphic. Up:Special Library Functions and Macros
Examples
/********************************************************/ /* */ /* Demonstration of character utility functions */ /* */ /********************************************************/ /* prints out all the ASCII characters which give */ /* the value "true" for the listed character fns */ #include <stdio.F. the character is not a control character. that is: a space. iscsym(ch) Returns true if the character was a character which could be used in a C identifier. printf ("VALID CHARACTERS FROM isalpha()\n\n"). i. isspace(ch) Returns true if the character was a white space character.

this).
strcpy() This function copies a string from one place to another. }
buffer would then contain "string onestring two".s2).) To join two static strings together.
is to join the string this onto the string onto.*this. which gives the length or number of characters in a string. tagged onto the end. it joins them together into one string. the following code is required:
char *s1 = "string one". Use this function in preference to custom routines: it is set up to handle any peculiarities in the way data are stored. The effect of:
char *new.C Programming Tutorial
String Manipulation
The following functions perform useful functions for string handling. to = strcpy (to. onto[255]. (In some programs the user might get away without declaring enough space for the "onto" string. not including the NULL byte end marker. strcmp() String Manipulation 163
. or even a crashed machine. new = strcat(onto.
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. main () { char buffer[255]. it is identical to onto. strcat(buffer. See Strings. new is a pointer to the complete string. An example is:
int len. Memory is assumed to have been allocated for the starting strings.*from. char *s2 = "string two". but in general the results will be garbage. len = strlen (string).s1).from). If it is not then unpredictable effects will result. An example is
char *to. strcat(buffer. char *string. strlen() This function returns a type int value. strcat() This function "concatenates" two strings: that is. The string which is to be copied to must be large enough to accept the new string.

value = strcmp(s1. alphabetically. If s1 < s2 then the value is < 0. to = strncpy (to.
char *onto.n).*s2. Note that numbers come before letters in the ASCII code sequence and also that upper case comes before lower case. int n. If the strings were not the same.this. char *s1.*s2.
atof() ASCII to floating point conversion.s2. An example:
int value.n). then the value is > 0.
strncpy() This function copies the first n characters of a string from one place to another
char *to. value = strcmp(s1.*from. without needing to use sscanf().C Programming Tutorial This function compares two strings and returns a value which indicates how they compared. this function indicates the (ASCII) alphabetical order of the two.
The value returned is 0 if the two strings were identical.
strncmp() This function compares the first n characters of two strings
int value.
String Manipulation
164
. s1 > s2.
The following functions perform conversions between strings and floating point/integer types. These enable the programmer to perform the same actions with the first n characters of a string: strncat() This function concatenates two strings by copying the first n characters of this to the end of the onto string. new = strncat(onto.*this. They take a pre-initialized string and work out the value represented by that string. char *s1. There are also variations on the theme of the functions above which begin with strn instead of str.s2).*new.n).from.

The variables used are all to be declared long
int i. or in a separate file) and any constants must be written in floating point form: for instance.0 instead of just 7. All of C's mathematical capabilities are written for long variable types. Returns the unsigned value of the value in parentheses.y. It should be noted that all of these functions work with double or long float type variables. /* long int */ /* long float */
The functions themselves must be declared long float or double (which might be done automatically in the mathematics library file.2) is 3 */
Mathematical Functions
166
. double x. Here is a list of the functions which can be expected in the standard library file.
i = ceil(x). This is like rounding up.C Programming Tutorial
if (result == 0) { printf ("Those strings were the same!\n").). } } }
Node:Mathematical Functions. Up:Special Library Functions and Macros
Mathematical Functions
C has a library of standard mathematical functions which can be accessed by #including the appropriate header files (math. fabs() Find the absolute or unsigned value of the value in parentheses:
result = fabs(x). } if (result > 0) { printf ("string1 > string2\n"). Previous:Example 25.
ABS() MACRO. See fabs() for a function version. Next:Examples 26. the integer which is just above the value in parentheses. write 7.h etc. } if (result < 0) { printf ("string1 < string 2\n").result.
ceil() Find out what the ceiling integer is: that is. /* ceil (2.

Mathematical Functions
167
. It does not have to be declared specifically as unsigned. result = log10(10000).
log10() Find the base 10 logarithm. /*raise x to the power y */ result = pow(x.2 .
log() Find the natural (Naperian) logarithm.71828). It does not have to be declared specifically as unsigned. e. it must be greater than zero.0).2). The value used in the parentheses must be unsigned: that is.
pow() Raise a number to the power.7).3.
result = log10(x). The value used in the parentheses must be unsigned: that is.
result = sqrt(x).
result = log(x). /* floor(2.y). it must be greater than zero. result = sqrt(2.C Programming Tutorial
floor() Find out what the floor integer is: that is. result = exp(2.. the integer which is just below the floating point value in parentheses
i = floor(x).
result = exp(x). /* find 2 to the power 3.2) is 2 */
exp() Find the exponential value.
result = pow(x.0..*/
sqrt() Find the square root of a number. result = log(2.2). /*find x-squared */ result = pow(2.g.

0.C Programming Tutorial sin() Find the sine of the angle in radians.
result = cos(x).
atan2() This is a special inverse tangent function for calculating the inverse tangent of x divided by y.
result = asin(x). result = acos(1.
acos() Find the arccosine or inverse cosine of the value which must lie between +1. result = atan(200. This function is set up to find this result more accurately than atan().
result = atan2(x.0).0).14).
asin() Find the arcsine or inverse sine of the value which must lie between +1.y).
tan() Find the tangent of the angle in radians.0. result = sin(3.14).14).
result = atan(x).
Mathematical Functions
168
. result = tan(3.
result = sin(x).
atan() Find the arctangent or inverse tangent of the value.0 and -1.
result = tan(x).
cos() Find the cosine of the angle in radians. result = cos(3.0).
result = acos(x). result = asin(1.0 and -1.

What happens then when an erroneous statement is executed? Some sort of error condition would certainly have to result. might simply return zero or it might return the maximum value of the computer. matherr() uses a "struct" type variable called an "exception" to diagnose faults in mathematical functions. for any of the reasons above. Be careful to check this. 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). in addition to these "pathological" cases. The mathematical functions. The function responds by returning a value which gives information about the error.3). (Division by zero and underflow probably return zero. The exact details will depend upon a given compiler.
would also be nonsense. Domain error Illegal value put into function Division by zero Dividing by zero is nonsense.0). this is not the case. so an expression such as:
x = sqrt(-2. mathematical operations can fail just because the numbers they deal with get too large for the computer to handle. Loss of accuracy No meaningful answer could be calculated Errors are investigated by calling a function called matherr(). Although it is not possible to generalize. like the ones above. matherr() returns 0. This can be examined by programs which trap their errors dutifully. whereas overflow returns the maximum value which the computer can handle. errors. are terminal: they cause a program to stop without any option to recover the damage. Overflow Number became too large Underflow Number became too small. the following remarks about the behaviour of mathematical functions may help to avoid any surprises about their behaviour in error conditions. There are other errors which can occur too.C Programming Tutorial
y = asin (25. or too small. 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.
is nonsense and it cannot possibly produce a value for y. because dividing by zero is "mathematical nonsense" . division by zero is illegal. whilst still maintaining firm control of a program.) • Some functions return the value NaN. because none exists. For instance a hypothetical example: if the error could be recovered from. Finally. It is possible (in principle) to recover from any error. listed above. • A function which fails to produce a sensible answer. there is no simple number which is the square root of a negative value. In C. For example. as the case may be. Similarly. This doesn't stop the programmer from writing these statements though and it doesn't stop a faulty program from straying out of bounds. as the reader might have come to expect.it can be done. this stands for `Not a Maths Errors 171
. Not a form of Indian unleavened bread. Information about this structure must be found in a given compiler manual. call this function automatically when an error is detected. otherwise it returns -1. In many languages. See Structures and Unions.

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. /* Increment width . Find out what it is and trap errors! • Obviously. Next:Questions 21. The integral is found between the limits 0 and 5 and the exact answer is 25. y < LIMIT. no sensible result could be calculated. for ( y = inc/2. } printf ("Integral value = %. the programmer should try to stop errors from occurring in the first place. This is the exception structure (a special kind of C variable) which gives information about the last error which occurred. Up:Special Library Functions and Macros
Example
Here is an example for the mathematically minded. } /***************************************************************/ /** LEVEL 1 **/ /***************************************************************/ double integrand (y) double y.integral). double twopi.arbitrary */
/***********************************************************/ /** LEVEL 0 */ /***********************************************************/ main () { double y.10f \n".h> #define LIMIT 5 double inc = 0. although.integrand().001.
Example
172
. it is impossible for the numbers to overflow. • Some method of signalling errors must clearly be used. y += inc ) { integral += integrand (y) * inc. in this simple case.0).
/**********************************************************/ /* */ /* Numerical Estimation of Integral */ /* */ /**********************************************************/ #include <stdio.) The particular compiler used for this program returns the largest number which can be represented by the computer when numbers overflow. (See diagram.C Programming Tutorial Number'. wherever possible. i. twopi = 4 * asin(1.h> #include <math. Node:Example 27.h> #include <limits.e. Previous:Maths Errors. double integral = 0.

value = 2*y. True or false? 3. Previous:Special Library Functions and Macros. Previous:Example 27. Next:More on Data Types. What action is performed by strcat()? 5. All calculations are performed using long variables. Up:Special Library Functions and Macros
Questions
1. if (value > 1e308) { printf ("Overflow error\n"). What type of data is returned from mathematical functions? 2. Name five kinds of error which can occur in a mathematical function. What information is returned by strlen()? 4. }
Node:Questions 21. Node:Hidden Operators. exit (0). } return (value).C Programming Tutorial
{ double value. Up:Top
Questions
173
.

are unary operators: that is. no statement is an island". at first glance. These innocent looking operators can be used in some surprising ways which make C source code very neat and compact.. do not have to be thought of as being complete and sealed off from the rest of a program. on the other hand. This has some important implications... and similarly:
c = 5. Entire assignment statements have values too. Take. the following operators
= ++ -+= -= etc. c++. have a value which is one greater / or one less than the value of the variable which they act upon. has the value 4.
has the value which is the value of the assignment. they are applied to a single variable and they affect that variable alone. for example. 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. increment and decrement operators. To paraphrase a famous author: "In C. • Extended and Hidden =: • Example 28: Hidden operators and values 174
. The first thing to notice is that ++ and -.
The second of these statement c++. in the sense that it works out only one expression. has the unusual position of being both unary. c--. A statement such as:
c = 5.
The second of these statements c--. has the value 6. Increment/decrement operator statements. in C. What does this mean? It means that certain kinds of statement. So:
c = 5. taken as a whole.
the assignment. Both kinds of operator have one thing in common however: both form statements which have values in their own right. 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. The assignment operator. They therefore produce one unique value each time they are used. So the example above has the value 5.. A statement can be taken as a whole (as a "black box") and can be treated as a single value.Hidden operators and values
Concise expressions Many operators in C are more versatile than they appear to be.

of course: it can be used anywhere where a value can be used. The uses are manifold.
These equivalent statements set b and c to the value zero. could be assigned to a variable b:
b = (c = 0).
or simply:
b = c = 0.. The function below gets a character from the input stream stdin and passes it to a function called ProcessCharacter():
Extended and Hidden =
175
. any number of these assignments can be strung together:
a = (b = (c = (d = (e = 5))))
or simply:
a = b = c = d = e = 5. provided b and c are of the same type! It is equivalent to the more usual:
b = 0. For instance. c = 0. Previous:Hidden Operators.. Up:Hidden Operators
Extended and Hidden =
The idea that assignment statement has a value.) • As an index for arrays..
This very neat syntax compresses five lines of code into one single line! There are other uses for the valued assignment statement.C Programming Tutorial • Hidden ++ --: • Arrays Strings and Hidden Operators: • Example 29: • Cautions about Style: • Example 30: • Ques 21: Node:Extended and Hidden =. Next:Example 28.
Indeed. can be used to make C programs neat and tidy for one simple reason: it means that a whole assignment statement can be used in place of a value. Consider how an assignment statement might be used as a parameter to a function. For instance: • In other assignments (as above) • As a parameter for functions • Inside a comparison (== > < etc.. the value c = 0.

This is a perfectly valid statement in C.C Programming Tutorial
ProcessCharacter (ch = getchar()). It would not make sense the other way around. So.
The two methods are entirely equivalent. Next:Hidden ++ --.. Starry Night.until it is re-assigned by a new assignment statement.. /= etc. Up:Hidden Operators
Example
/************************************************/ /* */ /* Hidden Assignment #1 */ /* */ /************************************************/ main () { do { switch (ch = getchar()) { default : putchar(ch). If there is any doubt.. }
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. because. break. because the hidden assignment statement passes on the value which it assigns. *=. case 'Q' : /* Quit */ } } while (ch != 'Q').. The fact that the assignment was hidden inside another statement does not make it any less valid. Node:Example 28."). All the same remarks apply about the specialized assignment operators +=. if (ch == '*') { printf ("Starry. Previous:Extended and Hidden =. in fact. then there would be no value to pass on as a parameter. examine a little more of this imaginary character processing program:
ProcessCharacter(ch = getchar()).. } /* end */
Example
176
. this is a more compact way of writing:
ch = getchar(). ProcessCharacter (ch). The actual order of events is that the assignment is carried out first and then the function is called.

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. 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. the operation is performed after the function call. both a value before the ++ operator acts upon it. the value 3 is passed to the function and when the function returns. and a different value afterwards. while ((x += 0. The alternative is to write:
int i = 3. Previous:Example 28. the value of i is incremented to 4.
Hidden ++ and --
177
.0) { printf ("%lf".C Programming Tutorial
/************************************************/ /* */ /* Hidden Assignment #2 */ /* */ /************************************************/ main () { double x = 0. then. like assignment expressions. If the operator is used as a postfix. Up:Hidden Operators
Hidden ++ and -The increment and decrement operators also form statements which have intrinsic values and. } } /* end */
Node:Hidden ++ --.x). Next:Arrays Strings and Hidden Operators.
The increment operator is hidden in the parameter list of the function PrintNumber(). PrintNumber (i++). the operation is performed before the function call. they can be hidden away in inconspicuous places. because the variable i has. In the example above.2) < 20. PrintNumber (++i). Look at the following example:
int i = 3. This example is not as clear cut as the assignment statement examples however. If the operator is used as a prefix.

Previous:Arrays Strings and Hidden Operators. *(ptr++) != '\0'. Notice that the postfixed form of the increment operator is used. Strings and Hidden Operators
Arrays and strings are one area of programming in which the increment and decrement operators are used a lot. Up:Hidden Operators
Example
/*********************************************************/ /* */ /* Hidden Operator Demo */
Arrays. Strings too can benefit from hidden operators. Hiding operators inside array subscripts or hiding assignments inside loops can often make light work of tasks such as initialization of arrays. Node:Arrays Strings and Hidden Operators. for (i = 0. for (ptr = string. then it would be a simple matter to write the function
strlen (string) char *string. array[SIZE]. Next:Cautions about Style. Consider the following example of a one dimensional array of integers. Previous:Hidden ++ --. array[i++] = 0) { }
This is a neat way of initializing an array to zero. Next:Example 29. The same remarks apply to the decrement operator. { char *ptr. Strings and Hidden Operators
178
. count++) { } return (count). } /* count the characters in a string */
This function increments count while the end of string marker \0 is not found. i < SIZE. This prevents the element array[0] from assigning zero to memory which is out of the bounds of the array.C Programming Tutorial
in which case the value 4 is passed to the function PrintNumber().
#define SIZE 20
int i. int count = 0. Node:Example 29. Up:Hidden Operators
Arrays. If the standard library function strlen() (which finds the length of a string) were not available.

Next:Example 30. but. it can make programs harder to understand. Never forget that programming is communication to other programmers and be kind to the potential reader of a program. Up:Hidden Operators
Example
/******************************************************/ /* */ /* Arrays and Hidden Operators */ /* */ /******************************************************/ #include <stdio. Previous:Example 29.C Programming Tutorial
{ return (0).
There is always a happy medium in which to settle on a readable version of the code. } /* end */
Node:Cautions about Style. The statement above might perhaps be written as:
i = (int) ch++. Up:Hidden Operators
Cautions about Style
Hiding operators away inside other statements can certainly make programs look very elegant and compact. (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++. as with all neat tricks. Previous:Cautions about Style.h> #define SIZE 10
Cautions about Style
180
. i = (int)ch. if (i <= comparison) { } comparison--. if (i <= --comparison) { }
Node:Example 30. Next:Ques 21.

by placing the letter L after the constant. Constant values can be declared explicitly as long values. variable = 236526598L. Since C allows you to define new data types we shall not be able to cover all of the possiblities. Previous:More on Data Types. It is worth making a brief remark about some special ways of writing integer constants. until a programmer needs to know how to do something special with them. The most important of these are FILE The type which files are classified under enum Enumerated type for abstract data void The "empty" type volatile New ANSI standard type for memory mapped I/O const New ANSI standard type for fixed data struct Groups of variables under a single name union Multi-purpose storage areas for dynamical memory allocation • Special Constant Expressions: • FILE: • enum: • Example 31: • Example 32: • Suggested uses for enum: • void: • volatile: • const: • struct again: • union: • typedef: • Questions 23: Node:Special Constant Expressions. in fact. only the most important examples. Up:More on Data Types
Special Constant Expressions
Constant expressions are often used without any thought. for the latter half of this book.
long int variable = 23L. Next:FILE. Up to now the distinction between long and short integer types has largely been ignored.
More on data types
183
.More on data types
This section is about the remaining data types which C has to offer programmers.

the files stdin. That type is called FILE and file pointers have to be declared "pointer to FILE". Next:enum. FILE *fopen(). Notice that. We shall return to look more closely at files soon. files. often find it convenient to work with hexadecimal or octal numbers since these number bases have a special relationship to binary. Files are distinguished by filenames and by file pointers. Node:FILE. cannot be any greater than 255. These special files are always handled implicitly by functions like printf() and scanf(): the programmer never gets to know that they are. Previous:FILE. writing systems software. like these. /* 77 octal */ /* FFEF hex */
This kind of notation has already been applied to strings and single character constants with the backslash notation. hex_value = 0xFFEF. Next:Example 31. Previous:Special Constant Expressions. strictly speaking. in fact.
File handling functions which return file pointers must also be declared as pointers to files. stdout and stderr alone have been used in programs. 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. Up:More on Data Types
Special Constant Expressions
184
. If ddd is a value. being variables. File pointers are variables which pass the location of files to file handling functions.C Programming Tutorial Advanced programmers. A constant in one of these types is declared by placing either 0 (zero) or 0x in front of the appropriate value. but a structure which is only defined by the header file stdio. FILE *fp = stdin. they have to be declared as being some data type. Node:enum.
The values of character constants.h and so. For example:
FILE *fp. 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. Up:More on Data Types
FILE
In all previous sections. instead of the leading zero character:
ch = '\ddd'. it is not a reserved word itself. then:
Octal number Hexadecimal number 0ddd 0xddd
For example:
oct_value = 077. ch = '\xdd'.

enum is short for enumerated data. Norge. Previous:enum. Ireland. which the compiler generates itself. variable = England.C Programming Tutorial
enum
Abstract data are usually the realm of exclusively high level languages such as Pascal. Up:More on Data Types
Example
/**********************************************************/ /* */ /* Enumerated Data */ /* */ /**********************************************************/ #include <stdio. These words are given substitute integer numbers by the compiler which are used to identify and compare enum type data. In fact. Danmark. Next:Example 32. For this reason. Scotland. main () { enum countries variable. enum is a way of incorporating limited "high level" data facilities into C. which uses the countries above to make a kind of airport "help computer" in age of electronic passports! Node:Example 31.
enum
185
. For example:
enum countries { England. The user defines a type of data which is made up of a fixed set of words. Here is an example. Eire. instead of numbers or characters. Wales. they have a natural partner in programs: the switch statement. Deutschland }. Enumerated data are called abstract because the low level number form of the words is removed from the users attention.h> enum countries { England. Scotland. enumerated data are made up of integer constants. and simply concentrate on the logic of using them. like the ones above. }
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. Sverige.

names of roads or types of train. Next:volatile. Node:void.
• A function call can be cast (void) in order to explicitly discard a return value (though this is done by the compiler anyway). so this was not a problem in practice. bottom. void 189
. For instance. top. void *ptr. If you recall. the default is for C functions to return a value of type int. void variable. beauty }. charmed. The words `novalue' or `notype' would have been better choices. Up:More on Data Types
void
void is a peculiar data type which has some debatable uses.
Other suggestions: colours. Previous:Suggested uses for enum. • A function which is declared void has no return value and returns simply with:
return.
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. strange. The void datatypes was introduced in order to make C syntactically consistent. even though there is no point. truth. The word void is perhaps an unfortunate choice. 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 data type was introduced with functions in mind but the grammar of C allows us to define variables of this type also. scanf() returns the number of items it matches in the control string. 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 `invalid'. enum quarks { up. (void) returnvalue().C Programming Tutorial
skin.
void function(). malnutrition. but this is usually discarded. circulatory }. The value returned by a function did not have to be specified could always be discarded. since it has several implicit meanings none of which really express what is intended. A variable or function can be declared void in the following ways. down.

It replaces the meaning of void from `no type or value' to `no particular type'. Variables which are declared volatile will be able to have their values altered in ways which a program does not explicitly define: that is. • A void pointer can point to to any kind of object. The volatile datatype has found another use since the arrival of multiprocessor.
volatile
190
. either because they are in ROM (for example) or because they are data whose values must not be corrupted. This is also rather dubious. Next:const.. Node:volatile. 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.. multithreaded operating systems. 2. Node:const. The idea behind this type is to allow memory mapped input/output to be held in C variables. const int one = 1.&ch). This is also a highly questionable feature of the ANSI draft. interrupts etc. regardless of its type. it can be initialized by the compiler. In other words.
Few programmers would do this since it merely clutters up programs with irrelevant verbiage.
const int array[] = { 1. Up:More on Data Types
volatile
volatile is a type which has been proposed in the ANSI standard. by external influences such as clocks. 3. It allows assignments between incompatible pointer types without a cast operator.14. it should always reread its value. Previous:void.C Programming Tutorial
scanf ("%c".
Since a constant array only exists at compile time.&ch). 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. 4 }. external ports. This means that any pointer can be assigned to a void pointer. Next:struct again. Independent processes which share common memory could each change a variable independently. 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.
or
(void) scanf("%c". hardware. Previous:volatile. Up:More on Data Types
const
The reserved word const is used to declare data which can only be assigned once.

at different times. With const you have to keep track of constant values personally. and so on. Any attempt to assign values to const types will result in compilation errors. however.
Constant types and enumerated data are therefore just different aspects of the same thing. array[1] has the value 2 . Up:More on Data Types
struct
Structures are called records in Pascal and many other languages. four = 4. since they are connected in a very simple way. which can hold a variety of different variable types.
const
191
. This statement is used as follows:
typedef type newtypename. Structures are described in detail in chapter 25.. Up:More on Data Types
union
Unions are often grouped together with structures. Node:struct again. The compiler makes a container which is large enough to take any of these. four }. Previous:const. one = 1. Up:More on Data Types
typedef
C allows us to define our own data types or to rename existing ones by using a compiler directive called typedef. two = 2. Node:typedef. Enumerated data provide a convenient way of classifying constants. but they are quite unlike them in almost all respects. Previous:union. It is worth comparing the const declaration to enumerated data. one. They are like general purpose storage containers. Next:union. Next:Questions 23. Previous:struct again. See Structures and Unions. while the compiler keeps track of the values and types.
and
const const const const const zero = 0. three. The following two sets of of statements are the same:
enum numbers { zero. They are packages of variables which are all wrapped up under a single name. two. Node:union. Next:typedef.. three = 3.C Programming Tutorial array[0] then has the value 1.

/**************************************************/ /* Program */ /**************************************************/ typedef int newname1. True or false? Node:Machine Level Operations. Variables declared const can be of any type. Previous:typedef. True or false? 4. main () { typedef char newname2.
The typedef statement may be written inside functions or in the global white space of a program. What type might a timer device be declared if it were to be called by a variable name? 6. Next:Files and Devices. for example. by their very definition. Structures and unions can. Previous:More on Data Types.C Programming Tutorial
So. typedef makes dealing with these simple because it means that the user can define a structure or union with a simple typename. Does void do anything which C cannot already do without this type? 5. function(). be all kinds of shape and size and their names can become long and tedious to declare. Is FILE a reserved word? If so why is it in upper case? 2. Write a statement which declares a file pointer called fp. Write a statement which declares a new type "real" to be like the usual type "double". Up:Top
typedef
192
. }
This program will compile and run (though it will not do very much).
The compiler type checking facilities then treat byte as a new type which can be used to declare variables:
byte variable. 7. 3. Node:Questions 23. It is not very often that you want to rename existing types in the way shown above. we could define a type called byte. Enumerated data are given values by the compiler so that it can do arithmetic with them. The most important use for typedef is in conjunction with structures and unions. Up:More on Data Types
Questions
1. which was exactly one byte in size by redefining the word char:
typedef unsigned char byte.

Flags/messages. Many of the facilities which are available for bit operations need not concern the majority of programs at all. Programmers are interested to know about the contents of bits in these registers. systems programmers. in more detail. These days we are used to working at such a high level that it is easy to forget them. frequently find that they need to handle bits directly in order to make efficient use of memory when using flags. Since this book is an introductory text.Machine Level Operations
Bits and Bytes. Shifting. Bits (or binary digits) are the lowest level software objects in a computer: there is nothing more primitive. bits are grouped together and are interpreted to have a collective meaning. Previous:Machine Level Operations. C. on the other hand. 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. 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. Up:Machine Level Operations
Bit Patterns
All computer data. let alone manipulate them. in a status register they are thought of as being independent. For the most part. are bit patterns. below even the operating system are bits of memory. Next:Flags registers. it is rare for high level languages to even acknowledge the existence of bits. Other uses for bit patterns are listed below here:
Machine Level Operations
193
. In an ordinary byte of data. A flag is a message which is either one thing or the other: in system terms. You may wish to consult a book on assembly language programming to learn about low level memory operations. A status register is a group of bits (a byte perhaps) in which each bit signifies something special. perhaps to find out what happened in a program after some special operation is carried out. the flag is said to be `on' or `off' or alternatively set or cleared. we shall treat bit operations only superficially. however. For precisely this reason. which is held in memory). Down in the depths of your computer. of any type. Manipulating bit patterns is usually the preserve of assembly language programmers. it is quite unnecessary to think of computer data as bit patterns. 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. • Bit Patterns: • Flags registers: • Bit Operators and Assignments: • Bit operators: • Shift Operations: • Truth Tables and Masking: • Example 33: • Output 33: • Example 34: • Example 35: • Questions 24: Node:Bit Patterns. 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.

perhaps with the aid of a typedef statement.
typedef char byte. Next:Bit Operators and Assignments. A pseudo register is merely a register which is created by the programmer in computer memory (it does not exist inside a processor). Registers and Messages
A register is a place inside a computer processor chip. Previous:Bit Patterns. where data are worked upon in some way.. there may be call for "pseudo registers" in addition to "real" ones. Messages are a very compact way of passing information to low level functions in a program.C Programming Tutorial • Messages sent between devices in a complex operating environment use bits for efficiency. • Handling bit-planes in screen memory. Up:Machine Level Operations
Bit Operators and Assignments
C provides the following operators for handling bit patterns:
<< Bit shift left (a specified number or bit positions) >> Bit shift right(a specified number of bit positions) Bit Patterns 194
. have the values 1 or 0. A status register is a register which is used to return information to a programmer about the operations which took place in other registers. bitpattern variable. typedef int bitpattern. Next:Bit operators. Flags. (Raster ports and devices) • Performing fast arithmetic in simple cases. Node:Flags registers. Up:Machine Level Operations
Flags. Messages are just like pseudo status registers: they are collections of flags which signal special information between different devices and/or different programs in a computer system. pseudo-registers and messages are all treated as bit patterns. Status registers contain flags which give yes or no answers to questions concerning the other registers. Node:Bit Operators and Assignments. Previous:Flags registers. depending upon whether they are on or off (set or cleared). Messages do not necessarily have fixed locations: they may be passed a parameters. In advanced programming. Programmers who are interested in performing bit operations often work in hexadecimal because every hexadecimal digit conveniently handles four bits in one go (16 is 2 to the power 4).
The flags or bits in a register/message. registers. byte message. • Serially transmitted data. A program can test for this by using combinations of the operators which C provides. A program which makes use of them must therefore be able to assign these objects to C variables for use. A bit pattern would normally be declared as a character or some kind of integer type in C..

if they were interpreted collectively as an integer. to the optimum: as soon as the truth or falsity of an expression is known. Previous:Bit Operators and Assignments. Node:Bit operators. A bitwise operator would continue operating to the last before the final result were known. the numbers inside represent their values. a logical comparison operator quits. ||. Node:Shift Operations. Every 0 or 1 undergoes the operations individually. OR) can be used in place of logical operators (&&. in an expression. Previous:Bit operators.C Programming Tutorial | Bitwise Inclusive OR ^ Bitwise Exclusive OR & Bitwise AND ~ Bitwise one's complement &= AND assign (variable = variable & value) |= Exclusive OR assign (variable = variable | value) ^= Inclusive OR assign (variable = variable ^ value) >>= Shift right assign (variable = variable >> value) <<= Shift left assign (variable = variable << value) The meaning and the syntax of these operators is given below. but they are less efficient. Below is a brief summary of the operations which are performed by the above operators on the bits of their operands. Up:Machine Level Operations
Shift Operations
Imagine a bit pattern as being represented by the following group of boxes.. Next:Shift Operations. because logical operators are designed to reduce the number of comparisons made. Up:Machine Level Operations
The Meaning of Bit Operators
Bitwise operations are not to be confused with logical operations (&&. Next:Truth Tables and Masking. Every box represents a bit.) A bit pattern is made up of 0s and 1s and bitwise operators operate individually upon each bit in the operand. The values written over the top are the common integer values which the whole group of bits would have. Bitwise operators (AND.||).
128 64 32 16 8 4 2 1
------------------------------| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | -------------------------------
= 1
Bit Operators and Assignments
195
..

Next:AND. integerwise.g. Up:Machine Level Operations
Truth Tables and Masking
The operations AND. The same operation is then carried out for all the bits in the variables which are operated upon. Truth tables indicate what the results of all possible operations are between two single bits. They are binary or dyadic operators. Node:Truth Tables and Masking. So:
1 2 2 n >> >> >> >> 1 1 2 n == == == == 0 1 0 0
A common use of shifting is to scan through the bits of a bitpattern one by one in a loop: this is done by using masks. • Complement ~: • AND: • OR: • XOR: Node:Complement ~. (1 & 0) Truth Tables and Masking 197
. If a bit reaches the edge of the group of boxes then it falls out and is lost forever.C Programming Tutorial Notice that every shift left multiplies by 2 and that every shift right would divide by two. Previous:Complement ~. Up:Truth Tables and Masking
AND &
This works between two values. Up:Truth Tables and Masking
Complement ~
The complement of a number is the logical opposite of the number.
~1 has the value 0 ~0 has the value 1 (for each bit)
As a truth table this would be summarized as follows:
~value 0 1 == result 1 0
Node:AND. Previous:Shift Operations. Next:OR. OR (inclusive OR) and XOR/EOR (exclusive OR) perform comparisons or "masking" operations between two bits. C provides a "one's complement" operator which simply changes all 1s into 0s and all 0s into 1s. Another operation called COMPLEMENT is a unary operator. Previous:Truth Tables and Masking. e. Next:Example 33. The operations performed by these bitwise operators are best summarized by truth tables.

A mask is as a thing which fits over a bit pattern and modifies the result in order perhaps to single out particular bits. usually to cover up part of a bit pattern. This is done by deliberately inventing a value which only allows the particular flag of interest to have a non-zero value and then ANDing that value with the flag register. Previous:OR. Node:XOR. For example: in symbolic language:
MASK = 00000001 VALUE1 = 10011011 VALUE2 = 10011100 MASK & VALUE1 == 00000001
AND &
198
. where a programmer wishes to know if one particular flag is set or not set and does not care about the values of the others. Bit patterns and logic operators are often used to make masks. Node:OR. e. This is particularly pertinent for handling flags. e.g. Up:Truth Tables and Masking
XOR/EOR ^
Operates on two values.C Programming Tutorial
value 1 0 0 1 1 & value 2 0 1 0 1 == result 0 0 0 1
Both value 1 AND value 2 have to be 1 in order for the result or be 1.g. Up:Truth Tables and Masking
OR |
This works between two values. Next:XOR. (1 ^ 0)
value 1 0 0 1 1 ^ value 2 0 1 0 1 == result 0 1 1 0
The result is 1 if one OR the other (but not both) of the values is 1. Previous:AND. (1 | 0)
value 1 0 0 1 1 | value 2 0 1 0 1 == result 0 1 1 1
The result is 1 if one OR the other OR both of the values is 1.

printf ("Enter any number less than 128: "). Next:Output 33. The second program gets a value from the user in binary and converts it into hexadecimal. masks can be built up by specifying several flags:
FLAG1 = 00000001 FLAG2 = 00000010 FLAG3 = 00000100 MESSAGE = FLAG1 | FLAG2 | FLAG3 MESSAGE == 00000111
It should be emphasized that these expressions are only written in symbolic language: it is not possible to use binary values in C.
XOR/EOR ^
199
. &i).. Previous:Truth Tables and Masking. } printf ("Binary value = ").j. short MASK = 0x80. Node:Example 33.h> #define NUMBEROFBITS
8
/****************************************************/ main () { short i. return (0). scanf ("%h". Up:Machine Level Operations
Example
A simple example helps to show how logical masks and shift operations can be combined. if (i > 128) { printf ("Too big\n"). Alternatively.bit. octal or denary first.
/***************************************************/ /* */ /* Bit Manipulation #1 */ /* */ /***************************************************/ /* /* /* /* Convert denary numbers into binary */ Keep shifting i by one to the left */ and test the highest bit. This does*/ NOT preserve the value of i */
#include <stdio. The programmer must convert to hexadecimal. The first program gets a denary number from the user and converts it into binary. (See the appendices for conversion tables).C Programming Tutorial
MASK & VALUE2 == 00000000
The zeros in the mask masks off the first seven bits and leave only the last one to reveal its true value.

The reason for this slightly indirect method of working is that channels/portals hide operating system dependent details of filing from the programmer. In order to examine the contents of a file or to write information to a file.Files and Devices
Files are places for reading data from or writing data to. A program opens a file by calling a standard library function and is returned a file pointer. a program has to go through the following routine: • Open a file for reading or writing. This is extremely simple to work in practice. A program which writes information does no more than pass that information to one of these portals and the operating system's filing subsystem does the rest. • Files Generally: • File Positions: • High Level File Handling Functions: • Opening files: • Closing a file: • fprintf: • fscanf: • skipfilegarb?: • Single Character I/O: • getc and fgetc: • ungetc: • putc and fputc: • fgets and fputs: • feof: • Printer Output: • Example 36: • Output 36: • Converting example: • File Errors: • Other Facilities for High Level Files: • fread() and fwrite(): • ftell and fseek: • rewind: • fflush: • Low Level Filing Operations: • File Handles: Files and Devices 203
. This includes disk files and it includes devices such as the printer or the monitor of a computer. • Close the file to free the operating system "portal" for use by another program or file. An operating system allows a program to see files in the outside world by providing a number of channels or `portals' (`inlets' and `outlets') to work through. (Reserve a portal and locate the file on disk or whatever. a program has to open one of these portals. by the operating system. To use a file then. A program which reads data simply reads values from its file portal and does not have to worry about how they got there. Think of it as a protocol. C treats all information which enters or leaves a program as though it were a stream of bytes: a file.) • Read or write to the file using file handling functions provided by the standard library. but more sophisticated programs need to be able to read or write to files which are found on a disk or to the printer etc. The most commonly used file streams are stdin (the keyboard) and stdout (the screen). which allows a program to address that particular file and to distinguish it from all others.

the data which go into the files are exactly what would be seen on the screen. except that they are stored in a file instead. High level text files are also read back as character files. in raw memory format. but they are likely to work faster. These are more efficient. The alternative to these high level functions. at filing data as they can store data in large lumps. In fact. Up:Files and Devices
Files Generally
C provides two levels of file handling. integers. floating point types. This is true whether a file is meant to store characters. Previous:Files and Devices. • File Positions: • High Level File Handling Functions: • Opening files: • Closing a file: • fprintf: • fscanf: • skipfilegarb?: • Single Character I/O: • getc and fgetc: • ungetc: • putc and fputc: • fgets and fputs: • feof: • Converting example: • File Errors: • Other Facilities for High Level Files: • fread() and fwrite(): • ftell and fseek: • rewind: • fflush: • Low Level Filing Operations: Files Generally 204
. which is written to by high level file handling functions. character by character. Next:File Positions.C Programming Tutorial • open: • close: • creat: • read: • write: • lseek: • unlink remove: • Example 37: • Questions 25: Node:Files Generally. these can be called high level and low level. without converting to text files first. Any file. High level files are all treated as text files. is obviously low level functions. This all means that high level file functions are identical in concept to keyboard/screen input/output. ends up as a text file which could be edited by a text editor. in the same way that input is acquired from the keyboard. Low level input/output functions have the disadvantage that they are less `programmer friendly' than the high level ones. in principle.

putc() fgetc() fputc() feof()
File Positions
205
. Up:Files and Devices
High Level File Handling Functions
Most of the high level input/output functions which deal with files are easily recognizable in that they start with the letter `f'. Next:Opening files. For example:
fopen() fclose() getc() ungetc().C Programming Tutorial • File Handles: • open: • close: • creat: • read: • write: • lseek: • unlink remove: Node:File Positions. For instance:
fprintf() fscanf() fgets() fputs()
These are all generalized file handling versions of the standard input/output library. In most cases it is not necessary and it should be avoided. Previous:Files Generally. Other filing functions will not look so familiar. a program can control the way in which that position changes with functions such as ungetc() if need be. they process data in an identical way to their standard I/O counterparts. Up:Files and Devices
File Positions
When data are read from a file. Some of these functions will appear strikingly familiar. Previous:File Positions. the operating system keeps track of the current position of a program within that file so that it only needs to make a standard library call to `read the next part of the file' and the operating system obliges by reading some more and advancing its position within the file. The file versions differ only in that they need an extra piece of information: the file pointer to a particular portal. Next:High Level File Handling Functions. until it reaches the end. Each single character which is read causes the position in a file to be advanced by one. Node:High Level File Handling Functions. Although the operating system does a great deal of hand holding regarding file positions. since complex movements within a file can cause complex movements of a disk drive mechanism which in turn can lead to wear on disks and the occurrence of errors. They work with generalized files. This is passed as an extra parameter to the functions. as opposed to the specific files stdin and stdout which printf() and scanf() use.

A read only file is opened. returnpointer is a pointer to the file portal. if. for whatever reason.h> is included. If the file could not be opened. with some program code such as:
FILE *fp."mode"). The fopen() function performs both of these services and. Next:Closing a file. Secondly the file must be physically located on a disk or as a device or whatever. *mode."r")) == NULL) { printf ("File could not be opened\n"). This should be tested for. in fact. error_handler(). The operation mode is also a string. Node:Opening files. Previous:High Level File Handling Functions. If the file (which was named) opened successfully when fopen() was called.C Programming Tutorial Before any work can be done with high level files. that file is created anew. this pointer is set to the value NULL. returnpointer is a pointer to a FILE structure which is the whole object of calling this function. the file it attempts to open does not exist. char *fname.
or
FILE returnpointer. Finally. returnpointer = fopen("filename".mode).
The filename is a string which provides the name of the file to be opened. for example. because it would not make sense to attempt to write to a file which could not be opened or created. these functions need to be explained in some detail. returnpointer = fopen(fname.
High Level File Handling Functions
206
. There are two stages to opening a file: firstly a file portal must be found so that a program can access information from a file at all. Filenames are system dependent so the details of this must be sought from the local operating system manual. The syntax of the fopen() function is:
FILE *returnpointer. chosen from one of the following: r Open file for reading w Open file for writing a Open file for appending rw Open file for reading and writing (some systems) This mode string specifies the way in which the file will be used. if ((fp = fopen ("filename". Up:Files and Devices
Opening files
A file is opened by a call to the library function fopen(): this is available automatically when the library file <stdio.

.
char *filename() /* return filename */
{ static char *filenm = ".. FILE *fp.. char *filename().. Node:Closing a file. scanf ("%24s". if ((fp = fopen (filename(). Next:fprintf. Up:Files and Devices
Closing a file
A file is closed by calling the function fclose(). return (filenm). }
and then the user of the program would automatically be prompted for a filename.. a program needs to do something like the following:
Opening files
207
.. do { printf ("Enter filename :").". } while (strlen(filenm) == 0).filenm). When closing a file. skipgarb(). Recall the function filename() which was written in chapter 20. }
This function makes file opening simple...C Programming Tutorial
}
A question which springs to mind is: what happens if the user has to type in the name of a file while the program is running? The solution to this problem is quite simple. it can be read from or written to using the other library functions (such as fprintf() and fscanf()) and then finally the file has to be closed again.. returncode = fclose (fp). Previous:Opening files... fclose() prompts the file manager to finish off its dealings with the named file and to close the portal which the operating system reserved for it."r")) == NULL) { printf ("File could not be opened\n"). error_handler()... Once a file has been opened..... fclose() has the syntax:
int returncode...
fp is a pointer to the file which is to be closed and returncode is an integer value which is 0 if the file was closed successfully..... The programmer would now write something like:
FILE *fp.

Up:Files and Devices
fscanf()
The analogue of scanf() is fscanf() and.C Programming Tutorial
if (fclose(fp) != 0) { printf ("File did not exist. The form of the fprintf() statement is as follows:
fprintf (fp. char ch = 's'. Next:fscanf. assuming that fp is a pointer to an open file:
Closing a file
208
. ch). this function differs from its standard I/O counterpart only in one extra parameter: a file pointer. string is a control string which is to be formatted and the variables are those which are to be substituted into the blank fields of the format string."string". error_handler().
where fp is a file pointer. float x = 2. Previous:Closing a file. int n. Its name is meant to signify "file-print-formatted" and it is almost identical to its stdout counterpart printf().variables).pointers). i. "%d %f %c". pointed to by fp:
int i = 12.
printf ("Hello world %d"."Hello world %d". assume that there is an open file."string".
where n is the number of items matched in the control string and fp is a pointer to the file which is to be read from. as with fprintf().
Node:fscanf. n = fscanf (fp. }
Node:fprintf. x. The form of an fscanf() statement is:
FILE *fp. Previous:fprintf. Next:skipfilegarb?. Up:Files and Devices
fprintf()
This is the highest level function which writes to files.356. fprintf (stdout. For example.
The conversion specifiers are identical to those for printf(). fprintf (fp. For example. 1). In fact fprintf() is related to printf() in a very simple way: the following two statements are identical. 1).\n").

&i. Previous:skipfilegarb?. float x = -2. &x. On the other hand. Is a function like skipgarb() necessary then? The answer is: probably not. Next:getc and fgetc. skipfilegarb() is written below. a programmer is at liberty to assume that any file which does not contain correctly formatted data is just nonsense: he/she does not have to try to make sense of it with a function like skipgarb(). or files which do not contain the data they are alleged to contain. the program could simply return an error message like "BAD FILE" or whatever and recover in a sensible way. Up:Files and Devices
skipfilegarb() ?
Do programs need a function such as skipgarb() to deal with instances of badly formatted input data? A programmer can assume a bit more about files which are read into a program from disk file than it can assume about the user's typed input. Up:Files and Devices
Single Character I/O
There are commonly four functions/macros which perform single character input/output to or from files.C Programming Tutorial
int i = 10. A disk file will presumably have been produced by the same program which generated it.356. This does not mean to say that a program does not need to check for "bad files". &ch).
The remarks which were made about scanf() also apply to this function: fscanf() is a `dangerous' function in that it can easily get out of step with the input data unless the input is properly formatted. { while (getc(fp) != '\n') { } }
Node:Single Character I/O. For comparison alone. or will be in a format which the program expects.
skipfilegarb(fp) FILE *fp. Previous:fscanf. It would probably not make sense to use a function like skipgarb() for files. char ch = 'x'. "%d %f %c". putc() fgetc() fputc()
fscanf()
209
. Node:skipfilegarb?. They are analogous to the functions/macros
getchar() putchar()
for the standard I/O files and they are called:
getc() ungetc(). fscanf (fp. Next:Single Character I/O.

C Programming Tutorial

Node:getc and fgetc, Next:ungetc, Previous:Single Character I/O, Up:Files and Devices

getc() and fgetc()
The difference between getc() and fgetc() will depend upon a particular system. It might be that getc() is implemented as a macro, whereas fgetc() is implemented as a function or vice versa. One of these alternatives may not be present at all in a library. Check the manual, to be sure! Both getc() and fgetc() fetch a single character from a file:
FILE *fp; char ch; /* open file */ ch = getc (fp); ch = fgetc (fp);

These functions return a character from the specified file if they operated successfully, otherwise they return EOF to indicate the end of a file or some other error. Apart from this, these functions/macros are quite unremarkable. Node:ungetc, Next:putc and fputc, Previous:getc and fgetc, Up:Files and Devices

ungetc()
ungetc() is a function which `un-gets' a character from a file. That is, it reverses the effect of the last get operation. This is not like writing to a file, but it is like stepping back one position within the file. The purpose of this function is to leave the input in the correct place for other functions in a program when other functions go too far in a file. An example of this would be a program which looks for a word in a text file and processes that word in some way.
while (getc(fp) != ' ') { }

The program would skip over spaces until it found a character and then it would know that this was the start of a word. However, having used getc() to read the first character of that word, the position in the file would be the second character in the word! This means that, if another function wanted to read that word from the beginning, the position in the file would not be correct, because the first character would already have been read. The solution is to use ungetc() to move the file position back a character:
int returncode; returncode = ungetc(fp);

putc() and fputc()
These two functions write a single character to the output file, pointed to by fp. As with getc(), one of these may be a macro. The form of these statements is:
FILE *fp; char ch; int returncode; returncode = fputc (ch,fp); returncode = putc (ch,fp);

The returncode is the ascii code of the character sent, if the operation was successful, otherwise it is EOF. Node:fgets and fputs, Next:feof, Previous:putc and fputc, Up:Files and Devices

fgets() and fputs()
Just as gets() and puts() fetched and sent strings to standard input/output files stdin and stdout, so fgets() and fputs() send strings to generalized files. The form of an fgets() statement is as follows:
char *strbuff,*returnval; int n; FILE *fp; returnval = fgets (strbuff,n,fp);

strbuff is a pointer to an input buffer for the string; fp is a pointer to an open file. returnval is a pointer to a string: if there was an error in fgets() this pointer is set to the value NULL, otherwise it is set to the value of "strbuff". No more than (n-1) characters are read by fgets() so the programmer has to be sure to set n equal to the size of the string buffer. (One byte is reserved for the NULL terminator.) The form of an fputs() statement is as follows:
char *str; int returnval; FILE *fp; returnval = fputs (str,fp);

Where str is the NULL terminated string which is to be sent to the file pointed to by fp. returnval is set to EOF if there was an error in writing to the file. Node:feof, Next:Printer Output, Previous:fgets and fputs, Up:Files and Devices

feof()

ungetc()

211

C Programming Tutorial This function returns a true or false result. It tests whether or not the end of a file has been reached and if it has it returns `true' (which has any value except zero); otherwise the function returns `false' (which has the value zero). The form of a statement using this function is:
FILE *fp; int outcome; outcome = feof(fp);

Most often feof() will be used inside loops or conditional statements. For example: consider a loop which reads characters from an open file, pointed to by fp. A call to feof() is required in order to check for the end of the file.
while (!feof(fp)) { ch = getc(fp); }

Translated into pidgin English, this code reads: `while NOT end of file, ch equals get character from file'. In better(?) English the loop continues to fetch characters as long as the end of the file has not been reached. Notice the logical NOT operator ! which stands before feof(). Node:Printer Output, Next:Example 36, Previous:feof, Up:Files and Devices

Printer Output
Any serious application program will have to be in full control of the output of a program. For instance, it may need to redirect output to the printer so that data can be made into hard copies. To do this, one of three things must be undertaken:

stdout must be redirected so that it sends data to the printer device. A new "standard file" must be used (not all C compilers use this method.) A new file must be opened in order to write to the printer device The first method is not generally satisfactory for applications programs, because the standard files stdin and stdout can only easily be redirected from the operating system command line interpreter (when a program is run by typing its name). Examples of this are:
type file > PRN

which send a text file to the printer device. The second method is reserved for only a few implementations of C in which another `standard file' is opened by the local operating system and is available for sending data to the printer stream. This file might be called "stdprn" or "standard printer file" and data could be written to the printer by switching writing to the file like this:
fprintf (stdprn,"string %d...", integer);

feof()

212

C Programming Tutorial The final method of writing to the printer is to open a file to the printer, personally. To do this, a program has to give the "filename" of the printer device. This could be something like "PRT:" or "PRN" or "LPRT" or whatever. The filename (actually called a pseudo device name) is used to open a file in precisely the same way as any other file is opened: by using a call to fopen(). fopen() then returns a pointer to file (which is effectively "stdprn") and this is used to write data to a computer's printer driver. The program code to do this should look something like the following:
FILE *stdprn; if ((stdprn = fopen("PRT:","w")) == NULL) { printf ("Printer busy or disconnected\n"); error_handler; }

Example
Here is an example program which reads a source file (for a program, written in C, Pascal or whatever...) and lists it, along with its line numbers. This kind of program is useful for debugging programs. The program provides the user with the option of sending the output to the printer. The printer device is assumed to have the filename "PRT:". Details of how to convert the program for other systems is given at the end.
/***************************************************************/ /* */ /* LIST : program file utility */ /* */ /***************************************************************/ /* List a source file with line numbers attached. Like */ /* TYPE only with lines numbers too. */

Previous:Converting example.size. Next:fread() and fwrite(). noread = fread (ptr. }
This function/macro does not shed any light upon the cause of errors. Up:Files and Devices
Other Facilities for High Level Files
Files which have been opened by fopen() can also be handled with the following additional functions:
fread() fwrite() ftell() fseek() rewind() fflush()
These functions provide facilities to read and write whole blocks of characters in one operation as well as further facilities to locate and alter the current focus of attention within a file. Node:Other Facilities for High Level Files.size. pointed to by fp:
FILE *fp. This is called ferror(). To check for an error in an open file. int noread. only whether errors have occurred at all.fp). Up:Files and Devices
Filing Errors
The standard library provides an error function/macro which returns a true/false result according to whether or not the last filing function call returned an error condition. low level filing operations for files which have been opened for high level use! Node:fread() and fwrite(). A detailed diagnosis of what went wrong is only generally possible by means of a deeper level call to the disk operating system (DOS). if (ferror(fp)) { error_handler(). Previous:Other Facilities for High Level Files.C Programming Tutorial Node:File Errors. Up:Files and Devices
fread() and fwrite()
These functions read and write whole blocks of characters at a time.n. They offer.n. char *ptr. The form of fread() is as follows:
FILE *fp. Next:Other Facilities for High Level Files. essentially. Next:ftell and fseek.
Converting the example
217
. Previous:File Errors.

which is in some state of being read or written to. pos = ftell(fp). size is the size of a "block" of data in characters. This should be noted carefully when developing programs which use these calls. but in the event of their being insufficient. nowritten is a return value which indicates the actual number of blocks which was written. to be written to a file. Up:Files and Devices
File Positions: ftell() and fseek()
ftell() tells a program its position within a file. In some systems this can be difficult to arrange (in multi-tasking systems in particular) and almost impossible to guarantee. Next:rewind. The call to fseek() looks like this:
long int pos. pos is a long integer value which describes the position in terms of the number of characters from the beginning of the file. ptr is a pointer to the start of a block of memory at which the data are stored.
fp is an open file. will be found.
fread() and fwrite()
218
. FILE *fp. The form of the function calls is:
long int pos. (The disk might be corrupted or the file might have been altered in some way.
This time the parameters in parentheses provide information about where the data. int nowritten. Previous:fread() and fwrite(). fseek() seeks a specified place within a file. Node:ftell and fseek.fp). n is the number of blocks of data to be read. these two routines can be used. this should be checked. n is the number of blocks of data to be read. ptr is a pointer to the start of a block of memory which is to store the data when it is read.n. Aligning a file portal with a particular place in a file is more sophisticated than simply taking note of the current position.) fwrite() has an identical call structure to fread():
FILE *fp. fp is a pointer to an open file. nowritten = fread (ptr. A caution about these functions: each of these block transfer routines makes an important assumption about the way in which data are stored in the computer system. opened by fopen().C Programming Tutorial
The parameters in parentheses provide information about where the data will be stored once they have been read from a file. side by side. that is. size is the size of a block of data in characters.returncode. in sequential memory locations. It is important to check that the number of blocks expected is the same as the number received because something could have gone wrong with the reading process. fp is a pointer to an open file. opened by fopen(). Memory which is allocated in C programs by the function malloc() does not guarantee to find contiguous portions of memory on successive calls.n. Finally noread is a return value which indicates the number of blocks which was actually read during the operation.size. int mode. FILE *fp.size. char *ptr. It is assumed that the data are stored contiguously in the memory. Normally high level read/write functions perform as much management over positions inside files as the programmer wants. Again.

mode).C Programming Tutorial
returncode = fseek (fp. Node:rewind. FILE *fp. 2 pos is an offset measured relative to the end of the file. mode is an integer which specifies the way in which pos is to be interpreted. based upon fseek().0L. pos is some way of describing the position required within a file. Previous:ftell and fseek. Finally.0). } /* find beginning of file */ /* find the end of a file */ /* move 10 char's forward */
The L's indicate long constants.0L.0). fseek(fp. 1 pos is an offset measured relative to the current position.pos.mode) != 0) /* find 50th character */ { printf("Error!\n"). Some examples help to show how this works in practice:
long int pos = 50.pos. fp is a pointer to a file opened by fopen(). } fseek(fp.
The parameters have the following meanings. Previous:rewind. Next:Low Level Filing Operations. if (fseek (fp. 0 pos is an offset measured relative to the beginning of the file.g.0). Next:fflush. if (fseek (fp. Up:Files and Devices
rewind()
rewind() is a macro.1) != 0) { printf("Error!\n"). rewind(fp). int mode = 0. returncode is an integer whose value is 0 if the operation was successful and -1 if there was an error.
FILE *fp. Up:Files and Devices
File Positions: ftell() and fseek()
219
. /* = rewind() */
Node:fflush.2L.returncode.10L. e. fseek(fp. which resets a file position to the beginning of the file.

(They might concern special graphics facilities or windowing systems or provide ways of writing special system dependent data to disk files. Previous:Low Level Filing Operations. It flushes the output buffer which means that it forces the characters in the output buffer to be written to the file. A file handle is essentially the number of a particular file portal in an array. but with integers known as file handles or file descriptors. no conversion from characters to floating point or integers or any type at all take place. These are:
open() close() creat() read() write() rename() unlink()/remove() lseek()
These low level routines work on the operating system's end of the file portals. If used on files which are open for reading. fflush(fp). delete and rename files but they are restricted to the reading and writing of untranslated data: there are no functions such as fprintf() or fscanf() which make type conversions. but there may be times when C's predilection for handling all high level input/output as text files. files are not handled using file pointers. In other words. Previous:fflush. Data are treated as a raw stream of bytes.) Node:File Handles. Up:Files and Devices
File descriptors
At the low level. since high level file handling functions often make calls to the low level functions. programs can create. becomes a nuisance. The data which they deal with is untranslated: that is. Next:File Handles.C Programming Tutorial
fflush()
This is a macro/function which can be used on files which have been opened for writing or appending. either in a compiler manual. for all fflush() 220
. Working at the low level. or in an operating system manual. depending upon the system concerned. These will be documented. As well as the functions listed above a local operating system will doubtless provide special function calls which enable a programmer to make the most of the facilities offered by the particular operating environment. Next:open. Example:
FILE *fp.
Node:Low Level Filing Operations. A program can then use a set of low level I/O functions which are provided by the standard library. Up:Files and Devices
Low Level Filing Operations
Normally a programmer can get away with using the high level input/output functions. Low level functions should not be used on any file at the same time as high level routines. They should be regarded as being advanced features of the language because they are dangerous routines for bug ridden programs. it causes the input buffer to be emptied (assuming that this is allowed at all). such as date/time stamps etc.

The form of this function call is:
int fd. The integer mode is a message or a pseudo register which passes the necessary information to open(). The normal procedure is to open a file using one of the first three modes. Node:open. mode. O_RDONLY).C Programming Tutorial the different terminology.mode). A program can give more information to this function than it can to fopen() in order to define exactly what open() will do. For example:
#define FAILED -1 main() { char *filename().
The macro definitions of these flags will be included in a library file: find out which one and #include it in the program. For example:
int fd.
where filename is a string which holds the name of the file concerned. char *filename. by using the following flags:
O_RDONLY O_WRONLY O_RDWR Read access only Write access only Read/Write access
and on some compilers:
O_CREAT O_TRUNC O_APPEND O_EXCL Create the file if it does not exist Truncate the file if it does exist Find the end of the file before each write Exclude. fd = open (filename. they describe the same thing.
would declare a file handle or descriptor or portal or whatever it is to be called. mode is a value which specifies what the file is to be opened for and fd is either a number used to distinguish the file from others. fd = open(filename(). Up:Files and Devices
open()
open() is the low level file open function. Previous:File Handles. Next:close. or -1 if an error occurred. if (fd == FAILED) { printf ("File not found\n").
File descriptors
221
. Force create to fail if the file exists. int fd.

Thus after a call to creat. Some systems allow a more flexible way of opening files.
filename must be a valid filename. Previous:creat. Like all other filing functions. Next:creat. In the absence of any information about pmode. it returns the value 0 if it performs successfully and the value -1 if it fails. you should close the file descriptor. to emulate the fopen() function a program could opt to create a file if it did not already exist:
fd = open (filename(). with error checking. char *filename. Previous:open. } }
This opens up a read-only file for low level handling.C Programming Tutorial
error_handler (failed). this parameter can be set to zero. Up:Files and Devices
open()
222
. The bitwise OR operator is the vertical bar "|". fd = creat(filename. Node:read. The form of this function call is:
int fd. pmode. Node:close. Note that.
#define FAILED -1 if (close(fd) == FAILED) { printf ("ERROR!"). }
Node:creat. pmode is a flag which contains access-privilege mode bits (system specific information about allowed access) and fd is a returned file handle. e.g. For example. the action of creating a file opens it too. Up:Files and Devices
creat()
This function creates a new file and prepares it for access using the low level file handling functions. Previous:close. its contents are discarded. Next:write. The four appended modes are values which can be bitwise ORed with one of the first three in order to get more mileage out of open(). Up:Files and Devices
close()
close() releases a file portal for use by other files and brings a file completely up to date with regard to any changes that have been made to it. If a file which already exists is created.pmode). Next:read.
open() sets the file position to zero if the file is opened successfully. O_RDONLY | O_CREAT).

error_handler (). so it actually reads the next block of bytes from the current file position. Previous:write. fd. char *buffer.) Node:write. Up:Files and Devices
write()
This function is the opposite of read(). The following example reads n bytes from a file:
int returnvalue. be checked for errors: -1 Error n Number of bytes written Node:lseek. fd. Next:unlink remove. n. (If all went well this should be equal to n. It writes a block of n bytes from a contiguous portion of memory to a file which was opened by open(). The data are loaded directly into memory. The user must provide a place for them (either by making an array or by using malloc() to reserve space). again.n). as a sequence of bytes. Up:Files and Devices
read()
223
.n).
The return value should be checked. Previous:read. read() keeps track of file positions automatically.
The return value should. } returnvalue = read (fd. returnvalue = write (fd. Next:lseek.buffer.C Programming Tutorial
read()
This function gets a block of information from a file. if ((buffer = malloc(size)) == NULL) { puts ("Out of memory\n"). Its values are defined as follows:
0 End of file -1 Error occurred n the number of bytes actually read. n.buffer. The form of this function is:
int returnvalue. char *buffer.

2 Offset measured relative to the end of the file. Node:unlink remove. (It is impossible to delete the printer!) rename() lseek() 224
. 1 Offset measured relative to the current position.
#define FAILED -1 int returnvalue. and -1 (long) if an attempt was made to read past the end of the file. if (unlink (filename) == FAILED) { printf ("Can't delete %s\n". char *filename.offset. } if (remove (filename) == FAILED) { printf ("Can't delete %s\n". if ((pos = fseek (fd. int mode.filename). The values which mode can take are: 0 Offset measured relative to the beginning of the file.filename). They return -1 if the action failed. This function can fail if a file concerned is protected or if it is not found or if it is a device. Once deleted.mode)) == FAILED) { printf("Error!\n"). Previous:lseek.returncode. }
filename is a string containing the name of the file concerned. files are usually irretrievable. Up:Files and Devices
unlink() and remove()
These functions delete a file from disk storage.offset. }
pos gives the new file position if successful.fd.
#define FAILED -1L
long int pos. Next:Example 37.C Programming Tutorial
lseek()
Low level file handing functions have their equivalent of fseek() for finding a specific position within a file. This is almost identical to fseek() except that it uses the file handle rather than a file pointer as a parameter and has a different return value. The constants should be declared long int. or simply long.

C Programming Tutorial This function renames a file.h> #include <ctype. return (CODE). it returns the value -1 if the action fails.old.*new. The programmer specifies two filenames: the old filename and a new file name. The idea is to present a kind of file or "project" menu for creating. Node:Example 37.new). Up:Files and Devices
Example
This example strings together some low level filing actions so as to illustrate their use in a real program.h> #define #define #define #define #define #define CODE SIZE FNMSIZE TRUE FALSE FAILED 0 255 30 1 0 -1
/* defines O_RDONLY etc..getkey(). or because one of the filenames given was not valid. Next:Questions 25. A rather feeble text editor allows the user to enter 255 characters of text which can be saved. /***************************************************************/ /* Level 0 */ /***************************************************************/ main () { char *data.
/***************************************************************/ /* */ /* LOW LEVEL FILE HANDLING */ /* */ /***************************************************************/ #include <stdio. renaming files. if ((data = malloc(SIZE)) == NULL) { puts ("Out of memory\n").
unlink() and remove()
225
. */
/* Max size of filenames */
#define CLRSCRN() putchar('\f') #define NEWLINE() putchar('\n') int fd. An example illustrates the form of the rename() call:
#define FAILED -1 char *old. if (rename(old. }
rename() can fail because a file is protected or because it is in use. As usual.new) == FAILED) { printf ("Can't rename %s as %s\n".h> #include <fcntl. deleting. Previous:unlink remove.*malloc().

Write a statement which opens a low level file for writing. Print out all the header files on your system so that you can see what is defined where! Node:Structures and Unions. Up:Top
Questions
231
. Write a program which checks for illegal characters in text files. File handle 2. 5.126. File pointer 3..13. 4. What statement performs formatted writing to text files? 7. What is the difference between high and low level filing? 3. Write a statement which opens a high level file for reading. Anything else is illegal for programs. What are the following? 1.and 32. Next:Data structures. 6.C Programming Tutorial
Questions
1. Previous:Files and Devices. Valid characters are ASCII codes 10. File name 2.

so that all of these actions very simply by grouping variables together in convenient packages for handling. but it is not complete either. where they are protected from the reach of certain parts of a program. Node:struct. Up:Structures and Unions
Structures and Unions
232
. independent variables or arrays are no longer enough. Tidy programs are a blessing to programmers. As programs become increasingly complex. If a program were visualized schematically what would it look like? A traditional flow diagram? No: a computer program only looks like a flow diagram at the machine code level and that is too primitive for C programmers. A program has to manipulate data: it has to look at them. to make sealed capsules . All of these things would be very difficult if data were scattered about liberally. One way of visualizing a program is illustrated by the diagram over the page. What one then needs is a data structure. with no particular structure. Tidy data are just as important.not of program code .Structures and Unions
Grouping data. Next:struct. a record. These capsules are called structures.but of program data. For this reason C has the facility. This is where a new type of variable comes in: it is called a struct type. struct types or structures are usually lumped together with another type of variable called a union. Local data are operated upon inside sealed function capsules. move them around and copy them from place to place. Global data are wide open to alteration by any part of a program. Next:Declarations again. Previous:Black Box Data. or in other languages. • Black Box Data: • struct: • Declarations again: • Scope again: • Using Structures: • Arrays of Structures: • Example 38: • Structures of Structures: • Pointers to Structures: • Example 39: • Pre-initializing Static Structures: • Creating Memory for Dynamical struct Types: • Unions: • Questions 26: Node:Black Box Data. their data also grow in complexity and single. In fact their purposes are quite different. within it. Previous:Structures and Unions. This imaginative idea is not a bad picture of a computer program. This shows a program as a kind of society of sealed function capsules which work together like a beehive of activity upon a honeycomb of program data. Up:Structures and Unions
Organization: Black Box Data
What is the relationship between a program and its data? Think of a program as an operator which operates on the memory of the computer. Tidying up programs.

For example:
struct PersonalData x. Next:Scope again. The distinction is maintained here in places where confusion might arise. called an aggregate type. }. a string called address and three integers called YearOfBirth. but are a way of naming the parts which make up a structure variable. (Note: a variable which has been declared to be of type struct something is usually called just a structure rather than a structure variable. Older compilers did not support this luxury. Any particular structure type is given a name. Finally. either at the head of a program or at the start of a block. called a structure-name and the variables (called members) within a structure type are also given names. int MonthOfBirth. The list of variable components which make up the structure are called the members of the structure: the names of the members are not the names of variables.) The names of members are held separate from the names of other identifiers in C.C Programming Tutorial
struct
A structure is a package of one or usually more variables which are grouped under a single name. int DayOfBirth. Any variable which is declared to be of type struct PersonalData will be collectively made up of parts like these.
This purpose of this statement is to create a model or template to define what a variable of type struct PersonalData will look like. Up:Structures and Unions
Declarations
A structure is declared by making a blank template for a variable package. It is not any single type: the purpose of structures is to offer a tool for making whatever shape or form of variable package that a programmer wishes. Having defined this type of structure. This plethora of names is not really as complicated as it sounds. no storage has been given over to a variable. char address[addresssize]. so it belongs with other declarations.
struct
233
. This is most easily seen with the help of an example. Structures are not like arrays: a structure can hold any mixture of different types of data: it can even hold arrays of different types. the programmer can declare variables to be of this type. Previous:struct. The following statement is actually a declaration. Node:Declarations again. MonthOfBirth and DayOfBirth. At this stage. int YearOfBirth. nor has any variable been declared: only a type has been defined. however. every variable which is declared to be a particular structure type has a name of its own too.
struct PersonalData { char name[namesize]. so it is quite possible to have variable names and struct member names which are the same. The word struct is a reserved word in C and it represents a new data type. A structure can be as simple or as complex as the programmer desires. It says: define a type of variable which collectively holds a string called name.

A variable can be declared immediately after the template definition. int DayOfBirth. char address[addresssize]. it is worth pausing to show the different ways in which structures can be declared. typedef can be used to cut down a bit on typing in the long term.
struct PersonalData { char name[namesize]. Node:Scope again. This type definition is made once at the head of the program and then subsequent declarations are made by using the new name:
typedef struct { char name[namesize]. Next:Using Structures. x is certainly not a very good name for any variable which holds a person's personal data.C Programming Tutorial declares a variable called x to be of type struct PersonalData. } x. } PersonalData. char address[addresssize].
then declare:
PersonalData x.
main () { struct ONE
Declarations
234
. depending upon where the declaration is made. int YearOfBirth. Up:Structures and Unions
Scope
Both structure types and structure variables obey the rules of scope: that is to say. int MonthOfBirth. Similarly if a structure type variable is declared locally it is only valid inside the block parentheses in which it was defined. int MonthOfBirth. int DayOfBirth. Before moving on to consider how structures can be used. /* variable identifier follows type */
Alternatively. The method shown above is probably the most common one. however there are two equivalent methods of doing the same thing. int YearOfBirth.
Any one of these methods will do. but it contrasts well with all the other names which are abound and so it serves its purpose for now. Previous:Declarations again. a structure type declaration can be local or global.

C Programming Tutorial
{ int a.
struct Personal x. Although this sounds highly restrictive. it did account for the most frequent uses of structures. Next:Arrays of Structures. as parameters to functions and functions can return structure values. placed very severe restrictions upon what a program could do with structures.y) struct PersonalData x. }. struct ONE x. Modern compilers allow more flexible use of structures: programs can assign one structure variable to another structure variable (provided the structures match in type).y. { }
Finally. Early C compilers. structure variables can be passed. Essentially.
The whole bundle of members is copied in one statement! Structures are passed as parameters in the usual way:
function (x. some of which still exist today. x = y. the members of a structure could be assigned values and pointers to individual structures could be found.y). } function () { struct ONE x. This makes structures extremely powerful data objects to have in a program. float b. a function which returns a structure variable such as: Scope 235
. whole. Up:Structures and Unions
Using Structures
How does a program use the variables which are locked inside structures? The whole point about structures is that they can be used to group data into sensible packages which can then be treated as single objects.
The function then has to be declared:
function (x. /* This line is illegal. Previous:Scope again.y. A structure is assigned to another structure by the following statements. since ONE */ /* is a local type definition */ /* Defined only in main() */
}
Node:Using Structures.

x.address). }
where FillArray() is a hypothetical function which copies the string in the first parameter to the array in the second parameter.C Programming Tutorial
{ struct PersonalData x.name). Whenever a program needs to access the members of a structure. C provides a special member operator for pointers. The general form of a member reference is:
structure variable. x. The dot between the variable and the names which follow implies that the statements in this brief program are talking about the members in the structure variable x. the compiler never gets to know this information because it is hidden Using Structures 236
. this dot operator can be used. Members of actual structure variables are always accessed with this dot operator. }
would be declared in the following way:
struct PersonalData function () { }
Notice that the return type of such a function must also be declared in the function which calls that it. Although a structure contains many different types.member name
This applies to any type of structure variable. x. x. which has the type struct PersonalData. FillArray ("Some name". x. dot character. Next:Example 38. Previous:Using Structures. so are arrays of a given type of structure. This new operator is described below.function(). x = function().YearOfBirth = 1987. Node:Arrays of Structures. FillArray ("Some address". This is a structure member operator.MonthOfBirth = 2. Up:Structures and Unions
Arrays of Structures
Just as arrays of any basic type of variable are allowed. in the usual way.DayOfBirth = 19. Consider the structure variable x. The members of x could be assigned by the following program:
main () { struct PersonalData x. The members of a structure are accessed with the . because they are used so often in connection with structures. including those accessed by pointers. The reader will begin to see that structure names account for a good deal of typing! The typedef statement is a very good way of reducing this burden. however. rather than the whole collective bundle.

array[i] = array[j].
The members of the arrays would then be accessed by statements like the following examples:
array[i] = x. This allows more convenient handling of real-life strings. */ #include <stdio.. Next:Structures of Structures.
/*********************************************************/ /* */ /* Structures Demo */ /* */ /*********************************************************/ /* Simple program to initialize some structures */ /* and to print them out again.MonthOfBirth. int MonthOfBirth. i = array[2]. Up:Structures and Unions
Example
This listing uses a structure type which is slightly different to PersonalData in that string pointers are used instead of arrays.h> #define #define #define #define NAMESIZE ADDRSIZE NOOFPERSONS NEWLINE() 30 80 20 putchar('\n').C Programming Tutorial away inside a sealed structure capsule.YearOfBirth = 1987.array[size]. Previous:Arrays of Structures. An array would be declared in the usual way:
int i. Does no error */ /* checking. struct PersonalData x. so be wary of string sizes etc. int DayOfBirth. int YearOfBirth.
Node:Example 38.
/*********************************************************/ typedef struct { char *Name. array[i]. /*********************************************************/ main () /* Make some records */
Arrays of Structures
237
. so it can believe that all the elements in the array have the same type. char *Address. even though that type is itself made up of lots of different types. } PersonDat.

For this one reason.
Notice the way in which the member operator . a structure member could be accessed by pointers with the following statements: Pointers to Structures 240
.fs.tag = 10. This nesting can.
ptr is then. ptr can be assigned to any other pointer of similar type and it can be used to access the members of a structure. Node:Pointers to Structures. x. however.number = 30. it is forbidden for a structure to contain an instance of itself.C Programming Tutorial
} x. Pointers to structures are quite invaluable.
These two structures are of different types. It is this: what happens if a structure contains an instance of itself? For example:
struct Regression { int i.value = 20. What is not forbidden. A word of caution is in order here. unless the compiler's target computer has an infinite supply of memory! References to variables of this type would go on for ever and an infinite amount of memory would be needed for every variable. These extremely valuable devices are described below.tag2. is for a structure to contain an instance of a pointer to its own type (because a pointer is not the same type as a structure: it is merely a variable which holds the address of a structure). yet the first of the two is included in the second! An instance of the second structure would be initialized by the following assignments. in principle.
are probably okay (though they do not reflect good programming). It is in the second of these actions that a new structure operator is revealed.fs. formally. a pointer to a structure of type Name only. for building data structures such as linked lists and trees. can be used over and over again. Next:Example 39. x. struct Regression tag.tag3. in fact. go on many times. According to the rules which have described so far. though some compilers might place restrictions upon this nesting level. Structures should nest safely a few times.tag1. }
There is simply no way that this kind of statement can make sense. Up:Structures and Unions
Pointers to Structures
A pointer to a structure type variable is declared by a statement like:
struct Name *ptr. There is a problem with the above scheme that has not yet been addressed. because the reference which is calculated by this operator is worked out from left to right.tag4 = something. Notice also that no parentheses are necessary. Previous:Structures of Structures.0. Statements such as:
variable. The structure variable name is x:
x.

C Programming Tutorial
struct PersonalData *ptr. Next:Pre-initializing Static Structures.
This says let the member YearOfBirth of the structure pointed to by ptr.
This statement is identical in every way to the first version.h> #define #define #define #define NAMESIZE ADDRSIZE NOOFPERSONS NEWLINE() 30 80 20 putchar('\n').YearOfBirth = 20. means the contents of the address which is held in ptr and notice that the parentheses around this statement avoid any confusion about the precedence of these operators. this */ /* uses variable parameters instead of value params */ #include <stdio. however. it is assumed that ptr has been assigned to the address of some pre-assigned structure: for example. Up:Structures and Unions
Example
/*********************************************************/ /* */ /* Structures Demo #2 */ /* */ /*********************************************************/ /* This is the same program.e. int MonthOfBirth. Node:Example 39. C provides this special operator to make the operation clearer. using a new operator: ->. when dealing with structures. i. (*ptr). int DayOfBirth. ptr->YearOfBirth = 20. This is an arrow made out of a minus sign and a greater than symbol and it is used simply as follows:
struct PersonalData *ptr. There is a better way to write the above statement. Notice that *ptr.
/*********************************************************/ typedef struct { char *Name. but since this kind of access is required so frequently. char *Address. int YearOfBirth. by itself. using pointer references */ /* instead of straight variable references. by means of a statement such as:
ptr = &x. In the statements above. have the value 20.
where x is a pre-assigned structure. Previous:Pointers to Structures.
Example
241
.

int DayOfBirth. } } /**********************************************************/ /* Toolkit */ /**********************************************************/ /* As before */
Node:Pre-initializing Static Structures. 5.YearOfBirth). Node:Creating Memory for Dynamical struct Types. Previous:Pre-initializing Static Structures. }. int YearOfBirth.DayOfBirth. 12 }. rec[pers]. 1965. int MonthOfBirth. Next:Unions.Address).rec[pers]. Up:Structures and Unions Pre-initializing Static Structures 243
.rec[pers]. main () { static struct PersonalData variable = { "Alice Wonderment". Static and external structures can also be pre-assigned by the compiler so that programs can set up options and starting conditions in a convenient way. Previous:Example 39. A static variable of type PersonDat (as in the example programs) could be declared and initialized in the same statement:
#define NAMESIZE 20 #define ADDRESSSIZE 22 struct PersonDat { char *name. NEWLINE(). char *address. /* rest of program */ }
The items in the curly braces are matched to the members of the structure variable and any items which are not initialized by items in the list are filled out with zeros.C Programming Tutorial
printf ("Address : %s\n". Up:Structures and Unions
Pre-initializing Static Structures
In the chapter on arrays it was shown how static and external type arrays could be initialized with values at compile time. "Somewhere in Paradise".MonthOfBirth. rec[pers]. Next:Creating Memory for Dynamical struct Types. printf("Date of Birth: %1d/%1d/%1d\n".

in fact. using the -> operator. which are reserved space by the compiler. sizeof(char) Works out the number of bytes occupied by a single character. but it does not generally know. one would write something like this:
char *malloc(). sizeof(struct PersonalData) works out the number of bytes needed to store a single structure variable. in fact. how many bytes long a structure is. A dynamical structure is built using the memory allocation function:
malloc()
and pointers.
There is a problem with this statement though: malloc() is declared as a function which returns a type `pointer to character' whereas. by providing a compile time operator called
sizeof ()
which calculates the size of an object while a program is compiling. it has a template for that structure. the programmer is interested in pointers of type "pointer to struct Something". are. ptr = malloc(10). How then does a program know how must space to allocate? The C compiler comes to the rescue here.
ptr is then a pointer to the start of that block of 10 bytes. The cast operator casts pointers with a general form:
(type *) value
Creating Memory for Dynamical struct Types
244
. Dynamical data are data which are created explicitly by a program using a scheme of memory allocation and pointers. since a structure may occupy more memory than the sum of its parts. to allocate ten bytes. malloc() was described in connection with strings: it allocates a fixed number of bytes of memory and returns a pointer to that data. In fact. For example: sizeof(int) Works out the number of bytes occupied by the type int. in advance. it is seldom possible to know this information. This equals 1. static data structures because they do not change during the course of a program: an integer is always an integer and an array is always an array: their sizes cannot change while the program is running. Obviously this tool is very useful for working with malloc(). Normal program data. here. For instance. which was used to define it. The idea is to create the memory space for a new structure as and when it is needed and to use a pointer to access the members of that structure. malloc() has to be forced to produce a pointer of the correct type then and this is done by using the cast operator to mould it into shape. The memory allocation statement becomes something like:
ptr = malloc(sizeof(type name)).C Programming Tutorial
Creating Memory for Dynamical struct Types
Probably the single most frequent use of struct type variables is in the building of dynamical data structures. *ptr. When a program wants to create the space for a structure.

This is a natural strategy in system programming where many instances of different kinds of variables with a related purpose and stored dynamically. • Declaration of union: • Using unions: Node:Declaration of union.
This rather laboured statement provides both the memory and the location of that memory in a legal and type-sensical way.
struct SomeStruct *ptr. A union can hold any one of its members but only at different times. By having a standard data size we can guarantee that any hole left when dynamically allocated memory is freed will always be reusable by another instance of the same type of union. which are used to mould the type of object concerned. }.
This declares a type template. Next:Questions 26. Only one member can be stored in such an object at any one time. Clearly they cannot all be there at the same time. Unions behave like specially sized storage containers which can hold many different types of data. The next section of this book discusses what we can do with dynamically allocated structures. or it would be overwritten by another.z.y. The real purpose of unions is to prevent memory fragmentation by arranging for a standard size for data in the memory.
union IntOrFloat { int ordinal. The compiler arranges that a union type variable is big enough to handle the job. Variables are then declared as:
union IntOrFloat x. Up:Structures and Unions
Unions
A union is like a structure in which all the `members' are stored at the same address. Next:Using unions. Previous:Unions. Previous:Creating Memory for Dynamical struct Types. float continuous. Up:Unions
Declaration
A union is declared in the same way as a structure.C Programming Tutorial Consider the following example of C source code which allocates space for a structure type called SomeStruct and creates a correctly aligned pointer to it. ptr = (struct SomeStruct *) malloc(sizeof(struct Somestruct)). Node:Unions. It has a list of members. char *malloc().
Unions
245
. called ptr.

union_pointer->member. When the variables are referred to as
x. }. The dot .
the program sees x as being an integer type. One way to overcome this is to keep a variable which signals the type currently held in the variable. x is just a box for the different members to share.
This could be accompanied by an enumerate declaration such as:
enum Types { INT. Consider the following kind of union:
union WhichType { int ordinal. At other times (when x is referred to as x. char letter. CHAR }. operator selects the different members for variable and the arrow -> selects different values for pointers. FLOAT. This is done very easily with the aid of enumerated data.
which would make union type-handling straightforward:
switch (x_status) {
Declaration
246
.y and z as being either integers or float types. The form of such statements is:
union_variable.ordinal = 1. enum Types x_status. Up:Unions
Using unions
Unions are coded with the same constructions as structures.continuous) it takes on another aspect: its alter ego. Previous:Declaration of union.member. since a program has no automatic way of knowing what type of member is currently stored in the union type.
Variables could then go in pairs:
union WhichType x. Node:Using unions. the float type.C Programming Tutorial At different times the program is to treat x. float continuous. Notice that x by itself does not have a value: only its members have values.
Unions are seldom very useful objects to have in programs.

23. What is a member? 3. case FLOAT : x. What is the difference between a structure and a union? 2. Up:Top
Using unions
247
. case CHAR : x. Next:Recursion. Node:Questions 26. how would you find out the value of a member called mem. If ptr is a pointer to a structure.ordinal = 2. 4.C Programming Tutorial
case INT : x.
which would then require statements such as:
var. If x is a variable.
and so on.ordinal = 12. enum Types x_status. } var. Previous:Structures and Unions. break. ptr->x.x_status = CHAR. how would you find out the value of a member called mem. break..letter = '*'.continuous = 12. }
These variables could even be grouped into a structure:
struct Union_Handler { union WhichType x. 5. Previous:Unions. var.. A union is a group of variables in a single package. Up:Structures and Unions
Questions
1. True or false? Node:Data structures.x.ordinal = 2.

The aim of a data structure is to model real life patterns with program data. it must be drawn as a structure diagram. Putting these two together creates the idea of a `town structure' containing pointers to neighouring villages which lie on roads to the North.) The diagram shows such a simplified map of the surrounding land. consider an application example in which a program stores a map of the local countryside. South. (See figure 1. or share a tabular structure. with towns' vital statistics written on it. In order to make the map into a more computer-like picture. To see this. the structure diagram just shows how program data are related to each other. as well as the information about the town itself. If the map is ever going to be stored in a computer it will need to look more mechanical. • Data Structure Diagrams: • Tools: • Programme For Building Data Structures: • Setting Up A Data Structure: • Example Structures: • Questions 27: Node:Data Structure Diagrams. This sort of map is. so a chess game would naturally use a two dimensional array to store the positions of pieces on the chess board. Most real application programs require a more complex data structure than C variables can offer. Up:Data structures
Data Structure Diagrams
Now examine figure 2. The arrows tend to suggest that pointers will play a role in the data structure. This diagram is a data structure diagram: it is a diagram which shows how boxes of data must relate to one another in order to solve the problem of the towns map. ideally. For example. This town structure might look something like this:
struct Town { struct Town *north.Data Structures
Uses for struct variables. often arrays are not suitable structures for a given application. Arrays are good for storing patterns of information which look like tables. It has been drawn. The handicap is that the map does not look very computerish. The purpose of building a data structure is to create a pattern of information which models a particular situation clearly and efficiently. The blocks tend to suggest that sealed capsules or struct type data will also play a role. East and West of the town. A transformation is needed. In this case. Data structures are organized patterns of data. Most often a structure diagram shows how a problem is connected up by relating all the parts which go together to make it up. a chess board looks like a two dimensional array. quite deliberately. In real life. Structure diagrams. This program has to store information about individual towns and it has to be able to give directions to the user about how to get to particular towns from some reference point.
Data Structures
248
. all of this information is most easily conveyed by means of a map. Take the simplest kind of data structure: the array. A structure diagram is a picture which shows how something is connected up. just what a computer ought to be able to store. in a way which is intended to conjure up some particular thoughts. Previous:Data structures. Next:Tools.

Town *east. which were drawn in chapter 7. which models the way we think. to a particular storage space. one begins to see how structures can be fashioned. Programs which behave exactly like their data operate very simply. Links A link is a pointer to a new struct type. in computer memory. because pointers in data structures are invariably chained together to make up the structure. and the computer will be able to find it at any time after that. Node:Tools. Sometimes the shape of data structures and programs are identical.
Assume for now that LocalInfo is a structure which contains all the information about a town required by the program. so does a program really gain anything from working with pointers? The answer is yes. to look exactly like the problems which they represent. Up:Data structures
The Tools: Structures. so storage places do not need names: all a program needs to do is to keep a record of a pointer. This part of the information is actually irrelevant to the structure of the data because it is hidden inside the sealed capsule. Town *south. Every chain has to start somewhere.C Programming Tutorial
struct struct struct struct }. The structure of a computer program is called a hierachy. Pointers and Dynamic Memory
The tools of the data structure trade are struct types and pointers. It is the pointers which are the main items of concern because it is pointers which contain information that enables a program to find its way around the map very quickly. Data structures are built out of dynamically allocated memory. By thinking of struct types and pointers in terms of pictures. LocalInfo help. The reader might object to this by saying that a pointer has to be stored in some C variable somewhere.
Data Structure Diagrams
249
. Links are used to chain structures together. the program would only have to refocus its attention on the new structure which was pointed to by the struct member north and similarly for other directions. Pointers are the keys which unlock a program's data. by connecting struct type variables together with pointers: these are the building blocks. The address of the next element in a chain structure is stored inside the previous structure. when this happens. If the user of this imaginary application program wished to know about the town to the north of one particular place. There is a simple reason for this similarity: computer programs are themselves just data structures in which the data are program instructions and the pointers and sealed boxes are function calls. like a model. Previous:Data Structure Diagrams. A data structure is built up. This is the reason why structure diagrams are so useful in programming: a structure diagram is a diagram which solves a problem and does so in a pictorial way. To understand this. Next:Programme For Building Data Structures. make a note of the following terms: Root This is a place where a data structure starts. a kind of optimum efficiency has been reached in conceptual terms. What is interesting about data structure diagrams is the way in which they resemble the structure diagrams of C programs. The address of the root of a data structure has to be stored explicitly in a C variable. Town *west.

• Think of a pattern which reflects the way in which the data are connected and add structure pointers to the struct definition. Up:Data structures
The Tools: Structures. The key phrase is dynamic storage: a program makes space for structures as new ones are required and deletes space which is does not require. The steps which a programmer would undertake in designing a data structure follow a basic pattern: • Group all the data. This is done with recourse to two example structures. accounts programs or database managers. The remaining parts of this section aim to provide a basic plan or formula for putting data structures together in C. which become two example programs in the next chapter. • Using pointers to connect structures means that they can be re-connected in different ways as the need arises. but more like a latticework. Up:Data structures
Programme For Building Data Structures
In writing programs which centre around their data. it is extremely important to plan data structures before any program code is written: changes in program code do not affect a data structure. can hold any number of pointers to other structures. there were four pointers in each structure. Pointers and Dynamic Memory
250
. Next:Setting Up A Data Structure. Structures. but alterations to a data structure imply drastic changes to program code. together and define a struct type to hold them. Remember that pointers alone do not create any storage space: they are only a way of finding out the contents of storage space which already exists.) • Data structures can be made up of lots of "lesser" data structures. We don't need to declare how much we shall use in advance. The limitations are few. Node:Programme For Building Data Structures. Next:Example Structures. We need to think about where and how data structures are going to be stored. Node:Setting Up A Data Structure. Previous:Programme For Building Data Structures. after all. which must be stored. Previous:Tools. for example. In fact. The functions which perform this memory allocation and release are:
malloc() and free()
There are some advantages which go with the use of dynamic storage for data structures and they are summarized by the following points: • Since memory is allocated as it is needed. link pointers and data storage.C Programming Tutorial Data structures do not have to be linear chains and they are often not. Only in some numerical applications does a data structure actually assist an algorithm rather than vice versa. each held inside struct type storage. such as word processors. so there is the potential to branch out into any number of new structures. the only restriction on data size is the memory capacity of the computer. a program must create its own space for data structures. • Design the programming algorithms to handle the memory allocation. (Data structures can be sorted. to connect them. so the chaining was not linear. In the map example above.

struct Town *west.
One of these is used to hold the root of the data structure and the other is used as a current pointer. The data structure is set up by repeating the following actions as many times as is necessary. ptr->south = NULL. For example:
struct Town { struct Town *north.*root.age = 56. the pointers can be assigned to them:
ptr = (struct Town *) malloc(sizeof(struct Town)). • Define a struct type.
• Declare two pointers to this type:
struct Town *ptr. root->south = NULL. struct Town *south.C Programming Tutorial
Setting Up A Data Structure
Once the basic mould has been cast for the building blocks. initialize members */ root->north = ptr. root will be NULL if no memory could be allocated..
This last statement connects the new structure onto the north branch of root.
Be careful to check for errors. struct Town *east.
/* if age is a member */ /* of struct LocalInfo */
This sets the pointers north and south to the value NULL. root->help. which conventionally means that the pointer does not point anywhere.
NULL pointer assignments tell the program handling the data structure when it has come to the edge of the structure: that is when it has found a pointer which doesn't lead anywhere. /* etc. by connecting structures together with pointers and filling them up with information. • Initialize the members of the structure with statements such as:
root->north = NULL. }. ptr->north = NULL. • When other structures have been created. • Allocate memory for one structure type:
root = (struct Town *) malloc(sizeof(struct Town)). Setting Up A Data Structure 251
. struct LocalInfo help. a program actually has to go through the motions of putting all the pieces together.

A binary tree structure has two pointers per struct type. They are simple by professional standards. usable application programs.
A linked list has only a single pointer per structure. without branching out. If the blocks were labelled A B C D E. Linked lists have two advantages over one dimensional arrays: they can be sorted easily (see diagram) and they can be made any length at all. Previous:Setting Up A Data Structure.) A linked list is a linear sequence of structures joined together by pointers. which points to the successor in the list. struct BinaryTree *right. The programs which handle these data structures are written in the form of complete..
struct BinaryTree { /* other info */ struct BinaryTree *left. Every separate struct type in a data structure has its own variable name. This is useful for classifying data on a greater than/less than basis. If a structure diagram were drawn of a linked list. True or false? 4. Up:Data structures
Example Structures
Two data structures of thids kind are very common: the linked list and the binary tree and both work upon the principles outlined above (In fact they are just different manifestations of the same thing. See Example Programs chapter. Right and left branches are taken to mean `greater than' and `less than' respectively. Up:Data structures
Questions
1. struct list *succ. How are the members of structures accessed in a data structure? Example Structures 252
. each of which branches out into two new ones. }. all the storage blocks in it would lie in a straight line. Next:Questions 27. What is a structure diagram? 2. along with their accompanying programmers' documentation. but they are long by book standards so they are contained in a section by themselves. A binary tree is a sequence of structures. C would be the successor of B and so on.C Programming Tutorial Node:Example Structures. Node:Questions 27.
struct list { double value. How are data linked together to make a data structure? 3. then B would be the successor of A. } *tree = NULL.. Previous:Example Structures.

6. Previous:Data structures. Next:Example Programs chapter. Write a statement which creates a new structure of type "struct BinaryTree" and finds its address.C Programming Tutorial 5. Write a small program which makes a linked list. three structures long and assigns all their data to be zero. Store that address in a variable which is declared as follows:
struct BinaryTree *ptr. Up:Top
Questions
253
. Can you automate this program with a loop? Can you make it work for any number of structures? Node:Recursion.

it is inevitably called again. when it has finished with a function call? It is suddenly thrown into a wildly different region of the memory and finds itself executing statements there. */ Well_Function (). Or is it? • Functions and The Stack: • Levels and Wells: • Tame Recursion and Self-Similarity: • Simple Example without a Data Structure: • Simple Example With a Data Structure: • Advantages and Disadvantages of Recursion: • Recursion and Global Variables: • Questions 28: Node:Functions and The Stack. What happens to a function which makes a call itself? Examine the function below:
Well_Function () { /* . This much is only normal: programs are designed to do this and the computer could do no more and no less. each time the function is called. It is apparently doomed to repeat the same procedure for ever.. control passes from one place in a program to another place.. This kind of function calling scenario is doomed to continue without end.. What happens to such a function when it is called in a C program? In the simple example above. When a function is called. The statements in this new region of the program are carried out and then control returns to a statement immediately following the one which made the function call. }
Well_Function() is said to be a recursive function. Up:Recursion
Functions and The Stack
We should think about the exact sequence of events which takes place when a function is called in a program. as.Recursion
The daemon which swallowed its tail. It then begins executing statements in Well_function(). Eventually the program comes upon the statement Well_Function(). It is defined in terms of itself: it contains itself and it calls itself. Next:Levels and Wells. from the beginning. The computer becomes totally consumed with the task of calling Well_Function() over and over. How can it get back again? A diagram does not answer this question: program structure diagrams hide Recursion 254
. begins executing the statements in the function. It swallows its own tail! The act of self-reference is called recursion. naturally. other statements ... inside the curly braces. The computer. something dramatic and fatal happens.. until it comes upon the statement Well_Function() and then it calls the function again. But how does the computer know where it must go back to. Previous:Recursion.. as though it were a new function. This section is about program structures which can talk about themselves. and it makes a call to that function again. This will help to cast some light on the mechanics of recursion and recursive functions.

control drops down to level one. Previous:Functions and The Stack. the messages never get taken off the stack and it just builds up. the stack grows and shrinks and all is well. organized in the memory. This is the reason for structure comments like:
/****************************************************/ /* Level 1 */ /****************************************************/
in the programs in this book. Up:Recursion
Levels and Wells
A stack is made up of frames or levels. As this process continues. Normally this method works perfectly. It then begins executing statements. it makes a new note of where it has to come back to and deposits it on top of the stack. no matter how many times functions are called. When it comes to the call Well_Function() again. it makes a new note and puts on the top of the stack. on the top of its program stack. without any problems at all: functions are called and they return again. Eventually the computer runs out of memory and the computer crashes or interrupts the program with a fatal error message.. If main() calls any functions at all. It then begins the function again and when it finds the function call. It does this by building a special data structure called a stack. The main() function is at level 0 because it is the root of the program.C Programming Tutorial this detail from view. it leaves itself a reminder. C management makes sure that it does not put anything else on top of that reminder to spoil the flow of control.. C organizes a stack structure when it runs a program and uses it for storing local variables and for keeping track of where it has to return to. Information is placed on top of a stack and taken from the top. first out (LIFO) structure because the last thing to go on the top of a stack is always the first thing to come off it.
function1() / / function2() / \ \ \ function3() / \
The answer to this puzzle is that the computer keeps a record of the addresses of the places to which it must return. When a level one function returns. Node:Levels and Wells. Since the function has no chance of returning control to its caller. the memory gets filled up with the program's messages to itself: the stack of messages gets larger and larger. it Functions and The Stack 255
. It is called a last in. the program is said to drop down a level. A stack is quite literally a pile of data. the program takes the first message from the top of the stack and carries on executing statements at the place specified by the message. Next:Tame Recursion and Self-Similarity. which tells it where it has to go to when it has finished executing that function. What happens when a recursive function.. C makes a note of the place it has to return to and puts that note on top of the stack. When it calls a function. like Well_Function() calls itself? The system works as normal. When a function is finished. Each time a function is called.

it provides perhaps the most powerful way of handling certain kinds of problem in programming. Up:Recursion
Tame Recursion and Self-Similarity
Recursion does not have to be so dramatically disastrous as the example given.else statement shields the program from the recursion when nooftimes equals zero. stopping the programming from plunging down the level well infinitely. A function like Well_Function() digs itself a well of infinite depth. These level numbers actually count the height of the program stack at any point in a program. Some problems are made up of many levels of detail (see the introduction to this tutorial) and the details are identical at all levels. Data structures are prime candidates for this because they are made up of identical structure types. For example. Notice the way in which the if. The aim is to control the number of times that recursion takes place by making a decision about what happens in the function: the decision about whether a function calls itself or not. } }
A call of WellFunction(4) would make this function drop down four stack levels and then return. Recursive functions can be tamed by making sure that there is a safe way exit them. Previous:Levels and Wells. The function is pathological because it causes the stack fill up the memory of the computer. } /* Causes stack to grow out of control */
Node:Tame Recursion and Self-Similarity. It effectively acts as a safety net. connected together in a way which make them look like programs connected up by function calls. The level number is the number of messages or reminders on the stack. so that recursion only happens under particular circumstances. this tends to suggest that recursion would be useful for dealing with these self-similar problems. Levels and Wells 256
. If recursion is tamed. A better name for this function would be:
StackOverflow() { StackOverflow(). particularly concerning data structures. it has no place in a levelled structure diagram. { if (nooftimes == 0) { return (0). it is easy to make Well_Function recurse four times only. Since recursion is about functions which contain themselves at all levels. It punches a great hole in a program.C Programming Tutorial hands control back to level zero. Next:Simple Example without a Data Structure. by making a test:
Well_Function(nooftimes) int nooftimes. } else { Well_Function(nooftimes-1). Earlier we remarked that programs and data structures aim to model the situation they deal with as closely as possible..

else statement. (Mathematicians also deal with recursive functions. Next:Simple Example With a Data Structure. For example:
factorial(4) == 1 * 2 * 3 * 4 factorial(6) == 1 * 2 * 3 * 4 * 5 * 6 == 24 == 720
Formally. This strange definition seems to want to lift itself by its very bootstraps! The second statement saves it. try following it through for n equals three. Previous:Tame Recursion and Self-Similarity. factorial() then tests whether n is zero (which it is not) so it takes the alternative branch of the if.) The factorial function is defined to be the "product" (multiplication) of all the natural (unsigned integer) numbers from 1 to the parameter of the function. Up:Recursion
Simple Example without a Data Structure
A completely standard example of controlled recursion is the factorial (or Gamma) function. } else { return (n * factorial(n-1)). The statement:
factorial (3). } }
To see how this works. This is a mathematical function which is important in statistics. { if (n == 0) { return (1). by giving it a reference value. as a controlled recursive function:
factorial (n) unsigned int n. the factorial function is defined by two mathematical statements:
factorial (n) = n * factorial(n-1)
and
factorial (0) = 1
The first of these statements is recursive. The value of n is set to three. because it defines the value of factorial(n) in terms of the factorial function of (n-1).C Programming Tutorial Node:Simple Example without a Data Structure..
causes a call to be made to factorial(). This instructs it to return the value of: Tame Recursion and Self-Similarity 257
. The factorial function can be written down immediately. computer programs are not alone in this.

Up:Recursion
Simple Example With a Data Structure
A data structure earns the name recursive if its structure looks identical at every point within it. It must therefore make a note of the pointer to the next structure in the list. one which branches left and one which branches to the right. The solution is to delete the list backwards from last to first using the following recursive routine. Node:Simple Example With a Data Structure. The simplest recursive structure is the linked list. or it will never be able to get beyond the first structure in the list. Previous:Simple Example without a Data Structure. /**************************************************************/ struct list *deletetoend (ptr) struct list *ptr. it will lose information about where the rest of the list is. In order to work this out. which checks that n is not 0 (it is not) and so tries to return 1 * factorial(0).C Programming Tutorial
3 * factorial(3-1)
In order to calculate that. before it deletes that structure. but starts unloading the stack and returning the values. the function has to call factorial recursively. It has two pointers. Finally. Next:Advantages and Disadvantages of Recursion. passing the value (3-1) or 2 to the new call. Neither of these structures goes on for ever. }. The problem it faces is this: if it deletes the first structure in the list. checks whether it is zero (it is not) and tries to return the value 2 * factorial(1). The new call takes this value. deletetoend() is a function which releases the dynamic memory allocated to a linked list in one go. { if (ptr != NULL)
Simple Example without a Data Structure
258
. because the pointer to the successor of a structure is held in its predecessor. The expression goes through the following steps before finally being evaluated:
factorial (3) == == == == 3 3 3 3 * * * * factorial(2) (2 * factorial(1)) (2 * (1 * factorial(0))) (2 * (1 * 1)))
== 3 * 2 * 1 * 1
Try to write this function without using recursion and compare the two. it needs to call factorial again. The next simplest structure is the binary tree: this structure splits into two at every point.
/* structure definition */ struct list { /* some other data members */ struct list *succ. so it seems reasonable to suppose that they might be handled easily using controlled recursive functions. it calls factorial(0) which does not call factorial any more. At every point in a linked list. there are some data of identical type and one pointer to the next structure.

There is also the slight danger Simple Example With a Data Structure 259
. Unfortunately there are few situations in which recursion can be employed in a practical way. in turn.
newlast then becomes the new end of the list. Node:Advantages and Disadvantages of Recursion. newlast->succ = deletetoend (newlast->succ). releasing the second last structure (which is now on the end of the list). Previous:Simple Example With a Data Structure. passing its successor in the list as a parameter. Notice that deletetoend() calls itself immediately. (ptr->succ) The function keeps doing this until it finds the end on the list. ptr = deletetoend (ptr). The function returns the value NULL at each stage. a program would almost certainly run out of memory. Next:Recursion and Global Variables. so that when called. { if (free((char *) ptr) != 0) { printf ("DEBUG [Z0/TktDtStrct] memory release failure\n"). releasestruct (ptr). It does assume that the programmer has been careful to ensure that the end of the list is marked by a NULL pointer. If the pointer supplied is already NULL then this function does nothing. } /**************************************************************/ releasestruct (ptr) struct list *ptr. If a recursive function buried itself a thousand levels deep. returns and the process continues until the entire list has been deleted. Up:Recursion
Advantages and Disadvantages of Recursion
Why should programmers want to clutter up programs with techniques as mind boggling as recursion at all? The great advantage of recursion is that it makes functions very simple and allows them to behave just like the thing they are attempting to model. } } /* release memory back to pool */
We supply a pointer to the place we would like the list to end.C Programming Tutorial
{ deletetoend (ptr->succ). and its successor is NULLified in a single statement. This. This need not be the very beginning: it could be any place in the list. The major disadvantage of recursion is the amount of memory required to make it work: do not forget that the program stack grows each time a function call is made. deletetoend() offers a very elegant way of deleting part or all of a linked list:
struct list *newlast. The very last-called deletetoend() then reaches the statement releasestruct() which frees the memory taken up by the last structure and hands it back to the free memory pool. } return (NULL). This is the conventional way of denoting a pointer which does not point anywhere. The function then eliminates all structures after that point. up to the end of the list. That function consequently returns and allows the second-last deletetoend() to reach the releasestruct() statement. If it is not NULL then it executes the statements enclosed by the if braces.

in which a second function alterGLOBAL() accidentally alters the value of GLOBAL in the middle of the function:
int GLOBAL = -2. What is a recursive function? 2. Previous:Recursion. Node:Recursion and Global Variables. recursion(). Previous:Advantages and Disadvantages of Recursion. Up:Recursion
Recursion and Global Variables
Global variables and recursion do not mix well.C Programming Tutorial that a recursive function will go out of control if a program contains bugs. If global variables and parameters are used instead. Node:Questions 28. Up:Recursion
Questions
1. consider a recursive function. What is a program "stack" and what is it for. as fast as ++ can make it more positive then GLOBAL will never be able to satisfy the condition of being zero and it will go on making recursive calls. State the major disadvantage of recursion. alterGLOBAL() cannot alter a variable in recursion() by accident. The only time that recursive functions should attempt to alter global storage is when the function concerned operates on a global data structure. Most recursive routines only work because they are sealed capsules and what goes on inside them can never affect the outside world. To appreciate the danger. because it only works with its own local copies of parameters and variables which are locked away in a sealed capsule. Next:Questions 28. never returning. The stack would fill up the memory and the program would plunge down an unending recursive well. Next:Errors and debugging. If alterGLOBAL() makes the mistake of setting GLOBAL to a positive value. this difficulty can be controlled much more easily. recursion () { if (++GLOBAL == 0) { return (0). 3. then the ++ operator in recursion() can only make GLOBAL larger and it will never be able to satisfy the condition that GLOBAL == 0 and so again the function would never be able to return. Node:Example Programs chapter. Previous:Recursion and Global Variables. as in the example above. } alterGLOBAL(). } /* another function which alters GLOBAL */
This function is treading a fine line between safety and digging its own recursive grave. out of harm's way. if only local variables are used. If alterGLOBAL() makes GLOBAL more negative. Up:Top
Advantages and Disadvantages of Recursion
260
.

Statistical Data Handler
The first program is a utility which allows the user to type sets of floating point data into an editor and to calculate the mean, standard deviation...and so on, of those data. The program is capable of loading and saving the data to disk, as well as being able to handle several sets of data at once. The editor works in insert or overwrite modes. The program is menu driven and its operation should be reasonably self explanatory, so it is presented with rather sparse documentation. • The editor: • Insert Overwrite: • Quitting section: • Program listing stat: Node:The editor, Next:Insert Overwrite, Previous:Statistical Data Handler, Up:Statistical Data Handler

The Editor
A simple machine independent editor is provided for entering data. The editor first asks the user whether the current number of sets of data is to be altered. The default value is zero so, when data are typed in for the first time, this should be set up, by responding Y for yes. Up to twenty independent sets of data can be used. This number is set at the start and it is held in the memory and saved to disk with data files. If the number of sets is reduced at any time, the top sets are cut off from the calculations, but they are not lost forever, provided the number is changed back to include them before they are saved to disk, since the number of sets is used as an upper bound in a for loop: it does not actually alter the memory. More sets can be added at any time by making this value larger. Node:Insert Overwrite, Next:Quitting section, Previous:The editor, Up:Statistical Data Handler

Insert/Overwrite
A project file can be edited in either insert mode or overwrite mode. Files which contain no data may only be edited insert mode. The editor senses this and selects the mode automatically. In insert mode the user is prompted for values. Type 0.0 in place of an entry to get out of this mode. In overwrite mode the user is offered each entry in turn. If a non digit character is typed in (such as a . (dot) or a - (dash) etc..) the value of an entry is not altered. However, if a new value is entered, the new value will replace the old one. By default, the values are offered in turn from 1 to the final value. However, on selecting overwrite mode, the user is Example Programs 261

C Programming Tutorial prompted for a starting value, and the values are offered from the starting number to the end. This is to avoid the rather tedious process of working through all the entries which are not required in a system independent way. Node:Quitting section, Next:Program listing stat, Previous:Insert Overwrite, Up:Statistical Data Handler

Quitting Sections
When quitting sections in which the user is supposed to enter data, the convention is that typing a zero value (0.0 for a time, 0 in any other instance) is a signal to break out of a section. Typing 0.0 while editing in insert mode causes the editor to quit. Node:Program listing stat, Previous:Quitting section, Up:Statistical Data Handler

The Program Listing
The program includes three library files, which are used for the following purposes. #include <stdio.h> Standard IO eader file #include <ctype.h> Contains character ID macros #include <math.h> Includes math function declarations The flow of program logic is most easily described by means of a program structure diagram. The diagram shows the structure of function calls within the program and this can be related to the listing. The general scheme of the program is this: 1. Various flags concerning the data structure are cleared. 2. A menu is printed and the program cycles through the menu options. 3. The editor determines the data group to be edited, updates the screen with the data in the current group and loops through insert or overtype editing until the user quits. 4. The analysis calls custom functions which scan through the data structure calculating the relevant quantities. 5. Various toolkits perform run of the mill activities. The data structure of this program is an array of linked lists. The array provides the roots of several independent linked lists: one for each group of data. These linked lists are attended to by toolkit routines and by special functions such as over(). Node:Listing stat, Next:Variable Cross Referencer, Previous:Statistical Data Handler, Up:Example Programs chapter

Previous:Example Programs chapter.g. if so. Up:Variable Cross Referencer
Comments
This simplified program could be improved in a number of ways. Node:Errors and debugging. so that #undef-ined symbols and variables were clearly distinguished. Previous:Output of Cross Referencer. label the line number with a *. Next:Summary.c. Up:Top
Comments
288
. *123 342 *1234 • At present the program only marks macros with a # symbol on the line at which they are defined.C Programming Tutorial Node:Commonts on cref. Here are some suggestions for improvement: • The program could determine whether an identifier was of type pointer or not and. e. It could be made to mark them at every line.

Next:Run time errors. Up:Errors and debugging
Compiler Trappable Errors
• Missing semicolon. Previous:Missing semicolon. • Compiler Trappable Errors: • Run time errors: • Tracing errors: • Pathological Problems: • Porting Programs between computers: • Qu: Node:Compiler Trappable Errors..
Node:Missing brace. In many cases compiler errors are not generated because the actual error which was present but because the compiler got out of step. Up:Compiler Trappable Errors Errors and debugging 289
.
statement.. but: { }. It is useful therefore to build a list of errors and probable causes personally. Up:Compiler Trappable Errors
Missing semicolon. <-. Often the error messages give a completely misleading impression of what has gone wrong. Every statement must end with a semi colon. These few examples here should help beginners get started and perhaps give some insight into the way C works. A compound statement which is held in curly braces seldom needs a semi colon to follow. Next:Missing brace. Next:Mistyping Upper Lower Case.: • Missing brace: • Mistyping Upper Lower Case: • Missing inv comma: • Variable not declared or scope wrong: • Using a function or assignment inside a macro: • Forgetting to declare a function which is not type int: • Type mismatch in expressions: Node:Missing semicolon.This semi colon is only needed if the curly braces enclose a type declaration or an initializer for static array/structure etc. Previous:Errors and debugging.
A missing semicolon is easily trapped by the compiler. Previous:Compiler Trappable Errors.Errors and debugging
Mistakes! Debugging can be a difficult process.

So write
{ }
and fill in the statements afterwards. Up:Compiler Trappable Errors
Missing quote "
If a quote is missed out of a statement containing a string then the compiler will usually signal this with a message like:
String too long or unterminated. Up:Compiler Trappable Errors
Mistyping Upper/Lower Case
C distinguishes between small and capital letters. Node:Missing inv comma. Node:Using a function or assignment inside a macro. Next:Using a function or assignment inside a macro.
Node:Variable not declared or scope wrong. Next:Variable not declared or scope wrong. Count braces carefully. If a program fails at the linking stage because it has found a reference to a function which had not been defined. abs (x = function()). Often this error will generate a message like `unexpected end of file' because it is particularly difficult for a compiler to diagnose.
Missing closing brace }
290
. One way to avoid this is to always fill braces in before the statements are written inside them. Up:Compiler Trappable Errors
Using a function or assignment inside a macro
If abs (x) is a macro and not a function then the following are incorrect:
abs (function()). this is often the cause. Next:Forgetting to declare a function which is not type int. Previous:Variable not declared or scope wrong. Previous:Missing brace. Previous:Mistyping Upper Lower Case. Up:Compiler Trappable Errors
Variable not declared or scope wrong
This means that a variable is used which has not first been declared. Previous:Missing inv comma. Next:Missing inv comma. or that a variable is used outside of its sealed capsule.C Programming Tutorial
Missing closing brace }
This error is harder to spot and may cause a whole host of irrelevant and incorrect errors after the missing brace. Node:Mistyping Upper Lower Case.

}
The function1() is type char. *function2(). Previous:Forgetting to declare a function which is not type int. If it is required that they return another type of variable. function1(). along with the other declarations:
CallFunction () { char ch. this must by declared in two places: a) in the function which calls the new function. This error might generate something like "lvalue required". Up:Compiler Trappable Errors
Forgetting to declare a function which is not type int
All functions return values of int by default. Up:Compiler Trappable Errors
Type mismatch in expressions
There is a rule in C that all maths operations have to be performed with long variables. The result cannot therefore be assigned to a short type afterwards or the compiler will complain that Using a function or assignment inside a macro 291
. Next:Type mismatch in expressions. Node:Forgetting to declare a function which is not type int. function2() is type pointer to char. This must also be declared where the function is defined:
char function1 () { }
and
char *function2() { }
This error might result in the message "type mismatch" or "external variable/function type/attribute mismatch" Node:Type mismatch in expressions. If the user forgets this and tries to use short C automatically converts it into long form. Previous:Using a function or assignment inside a macro.C Programming Tutorial Only a single variable can be substituted into a macro. These are
int long int double long float
The result is also a long type.

Up:Run time Type mismatch in expressions 292
. Up:Run time errors
Confusion of = and ==
A statement such as:
if (a = 0) { }
is valid C. i = (short) j * 2. Previous:Compiler Trappable Errors.j = 2. Up:Errors and debugging
Errors not trappable by a compiler (run time errors)
• Confusion of = and ==: • Missing & in scanf: • Confusing C++ and ++C: • Unwarranted assumptions about storage of arrays/structures: • Number of actual and formal parameters does not match: • Conversion string in scanf or printf wrong: • Accidental confusion of int short and char: • Arrays out of bounds: • Mathematical Error: • Uncoordinated Output using put/get I/O: • Global Variables and Recursion: Node:Confusion of = and ==. i = j * 2. Next:Confusing C++ and ++C.j = 2. the cast operator has to be used to cast the long result to be a short one. but notice that = is the assignment operator and not the equality operator ==. To compare a to zero the correct syntax is:
if (a == 0) { }
Node:Missing & in scanf. Previous:Run time errors.
Node:Run time errors. Next:Missing & in scanf. So the following is wrong:
short i.C Programming Tutorial there is a type mismatch. Next:Tracing errors.
short i. Previous:Confusion of = and ==.
If a short result is required. It is legal to put an assignment inside the if statement (or any other function) and the value of the assignment is the value being assigned! So writing the above would always give the result zero (which is `FALSE' in C) so the contents of the braces {} would never be executed.

then there is a subtle difference. Next:Unwarranted assumptions about storage of arrays/structures. char. Node:Confusing C++ and ++C.
array [C++] = 0. The arguments of the scanf statement must be pointers or addresses of variables.g.
Notice however that the & is not always needed if the identifier in the expression is already a pointer. Previous:Confusing C++ and ++C. &ch.
Including the & now would be wrong. not the contents of the variables themselves.
and should read:
int i. If this error is trappable then it will be something like "Variable is not a pointer". if they are hidden inside another statement e. but not in all cases. However. So if you find that a program is out of step by 1. ch. Thus the following is wrong:
int i. The following is correct:
int *i. &i). Up:Run time errors
Confusion of = and ==
293
. this could be the cause. Up:Run time errors
Confusing C++ and ++C
In many cases these two forms are identical. scanf ("%c %d". scanf ("%c %d". Next:Number of actual and formal parameters does not match.C Programming Tutorial errors
Missing & in scanf
This error can often be trapped by a compiler. Node:Unwarranted assumptions about storage of arrays/structures. ++C causes C to be incremented by 1 before the assignment takes place whereas C++ causes C to be incremented by 1 after the assignment has taken place. i). char *ch.ch. scanf ("%c %d". char ch. Previous:Missing & in scanf.i).

Previous:Unwarranted assumptions about storage of arrays/structures.C Programming Tutorial
Unwarranted assumptions about storage
C stores arrays in rows. Previous:Number of actual and formal parameters does not match. . and as far as the language is concerned the storage locations are next to one another in one place up to the end of the array. say. like aligning variables with particular addresses.. *array = 0. When finding a pointer to. it is not necessarily true that the array will necessarily be stored in this way.. Use:
&(array[3])
Do not assume that the size of a structure is the sum of the sizes of its parts! There may be extra data inside for operating system use or for implementation reasons. The following sort of construction only works for simple data types:
char array[10].
is safe. the third element. Node:Number of actual and formal parameters does not match. A program will be loaded into one or more areas (where ever the operating system can find space) and new variable space will be found wherever it is available. but this will not generally be in whole blocks `side by side' in the memory.
While it is true that the variable "array" used without its square brackets is a pointer to the first element of the array. Up:Run time errors
The number of actual and formal parameters does not match
This problem can be avoided in ANSI C and C++ but not in K&R C. When passing values to a function the compiler will not spot whether you have the wrong number of parameters in a statement. provided they are all of the correct type. array[1] = 0.. Up:Run time errors Unwarranted assumptions about storage 294
. you should not assume that
array + 3 * sizeof (datatype)
will be the location. Node:Conversion string in scanf or printf wrong. Using:
char array[10]. in general. Next:Conversion string in scanf or printf wrong. . The values which are assumed for missing parameters cannot be guaranteed.. *(array + 1) = 0. array[10] = 0. array[0] = 0. This might not be exactly true. Next:Accidental confusion of int short and char. *(array + 10) = 0. They are probably garbage and will most likely spoil a program.

should be scanf ("%d".&x).
or even:
double x.
Another effect which can occur if the conversion specifier is selected as being long when it the variable is really short is that neighbouring variables can receive the scanf values instead! For instance if two variables of the same type happen to be stored next to each other in the memory:
short i. scanf("%ld". These are wrong:
float x.&x).&x).
which might look like:
-------------------------------------| | | -------------------------------------i j
and the user tries to read into one with a long int value. scanf ("%f". scanf will store a long int value. Suppose the left hand box were i and the right hand box were j and you wanted to input the value of i: instead of getting:
-------------------| 002345 | | -------------------i j
scanf might store
0000000000000002345
as
-----------------------| 000000000 | 0000002345 | -----------------------i j
The conversion string in scanf/printf is wrong
295
. float x. should perhaps be scanf ("%f".j. float x.&x). which is the size of two of these short variables.C Programming Tutorial
The conversion string in scanf/printf is wrong
Incorrect I/O is can be the result of poorly matched conversion strings in I/O statements.

Next:Arrays out of bounds.
and the you allow the program to write to array[6] or more. Previous:Mathematical Error.h at the start of the program. A program might continue regardless of the fact that a mathematical function failed. If characters/integers are passed as parameters it is easy to mistype char for int etc. Next:Mathematical Error.C Programming Tutorial because the value was long. Previous:Conversion string in scanf or printf wrong. Up:Run time errors
Accidental confusion of int. Previous:Accidental confusion of int short and char. a crashworthy routine might be the result. Characters are stored by their ASCII values. Up:Run time errors
Accidental confusion of int. If an array is sized:
type array[5]. short and char
296
. Next:Global Variables and Recursion. Up:Run time errors
Arrays out of bounds
C does not check the limits of arrays. However the computer might! In the worst case this could cause the program to crash. but this would mean that the number would over flow out of i into j and in fact j might get the correct value and i would be set to zero!! Check the conversion specifiers!! Node:Accidental confusion of int short and char. Node:Arrays out of bounds. Node:Mathematical Error. Previous:Arrays out of bounds. short and char
Often when working with characters one also wants to know their ASCII values. C will not complain. Some mathematical errors (often subtle ones) can be caused by forgetting to include to file math. On the other hand if the declaration is wrong:
function (ch) int (ch). { }
but the character is continually assumed to be a character by the program.. Next:Uncoordinated Output using put/get I/O. Node:Uncoordinated Output using put/get I/O. Up:Run time errors
Mathematical Error
C does not necessarily signal mathematical errors. The compiler probably won't notice this because no conversion is needed between int and char.

would be the careless use of GLOBAL in the function alterGLOBAL() and the function would never be able to return. Node:Tracing errors. This means that it is not written to the screen until the buffer is either full or is specifically emptied. Again the cure is to write:
printf ("\n"). This results in strange effects such as programs which produce no output until all the input is complete (short programs) or spontaneous bursts of output at uncoordinated intervals. recursion(). Up:Run time errors
Global Variables and Recursion
Global variables and recursion should not be mixed. Consider a recursive function:
int GLOBAL. Up:Errors and debugging
Tracing Errors
• Locating a problem: Node:Locating a problem. The stack would fill up the memory and the program would plunge down an unending recursive well. All it would take to crash the program. Next:Pathological Problems. Up:Tracing errors
Uncoordinated Output using buffered I/O
297
. recursion () { if (++GLOBAL == 0) { return (0). ch = getch(). } /* another function which alters GLOBAL */
This function is treading a fine line between safety and digging its own recursive grave. } alterGLOBAL().
Node:Global Variables and Recursion. One cure is to terminate with a newline \n character which flushes the buffers on each write operation. Previous:Uncoordinated Output using put/get I/O. Most recursive routines work only because they are sealed capsules and what goes on inside them can never affect the outside world. puts() is buffered. Special functions on some systems such as getch() may also suffer from this problem. The only time that recursive functions should alter global storage is when the function concerned operates on a global data structure. Previous:Run time errors.C Programming Tutorial
Uncoordinated Output using buffered I/O
Output which is generated by functions like putchar(). Previous:Tracing errors.

subsection `Low Level File Handling': in that program a seemingly innocent macro defined by
#define CLRSCRN() putchar('\f')." Locating a problem 298
. It is almost impossible to advise about these errors. Try to use local variables. so they may not be used. Sometimes these will be the result of misconceptions about C functions.C Programming Tutorial
Locating a problem
Complex bugs can be difficult to locate.
caused the C library functions creat() and remove() to fail is remarkable ways on an early Amiga C compiler! The problem was that a single call to CLRSCRN() at the start of the function DelFile() caused both of the library functions (in very different parts of the program) above to make recursing function calls the function DelFile(). The deletion of CLRSCRN() cured the problem entirely! In general it is worth checking carefully the names of all functions within a program to be sure that they do not infringe upon library functions. Try "commenting out" lines of suspect code. Check that a program has not run out of private memory. Use statements like ch = getchar() to halt a program in certain places and to find out the exact location at which things go wrong. read() and write() are names which everyone wishes to use at some point. Try retyping the program. Next:Porting Programs between computers. Here are some tips for fault finding: 1. 7. or using a filter which strips out any illegal characters which might have found their way into a program. has to be the truth. Consider the following example which was encountered while writing the simple example in the chapter on Files and Devices. in preference to global ones for local duties. In other words: put comment markers around lines that you would like to eliminate temporarily and then recompile to pinpoint errors. Use statements like printf("program is now here") to map out the progress of a program and to check that all function calls are made correctly. but they are the names of standard library functions. (If it repeatedly crashes for no apparent reason. or operating system design peculiarities. Even capitalizing (Read() / Write()) might not work: beware that special operating system libraries have not already reserved these words as library commands. when all else fails and the manuals are in the waste paper basket. Previous:Tracing errors. 5. A programmer can only hope to try to eliminate all possibilities in homing in on the problem. but occasionally they may be the result of compiler bugs. 9. 4. Check variable declarations and missing parameters. try to find someone who programs in C regularly on the computer system concerned. this could be a cause. Node:Pathological Problems. Get some sleep! Hope the problem has gone away in the morning. To misquote Sherlock Holmes: "At the end of the day. For example. Up:Errors and debugging
Pathological Problems
Problems which defy reasonable explanations are called pathological or `sick'. Check that the compiler disk has not been corrupted (make a new copy) . Never rely on global variables for passing messages between functions.getting desperate now! 8. 6. 2.) Make the program stack size bigger if that is possible. however improbable. Failing these measures. 3. the last possibility.

e. • Check conversion characters in printf() and scanf() as some compilers choose slightly different conventions for these.g.i)
{ char *string. limited/unlimited size. Previous:Pathological Problems. This can cause errors at run time if a program runs out of space. function (string. }
3. struct Name
{ int member1. while (a < b)
{ while (b == 0) { printf ("a is negative"). Up:Top Pathological Problems 299
. pause() or wait loops. Nevertheless. even though there is nothing wrong with the code. Next:Qu. there are almost inevitably problems in porting programs from one computer to another. • The stack size for (memory available to) a program is likely to vary between systems. int member2. especially scanf(). Up:Errors and debugging
Questions
Spot the errors in the following: 1. Next:reserved words list. Programmers attempting to transfer programs between machines are recommended to look at all the scanf() statements first and to check all the conversion specifiers with a local compiler manual.. Here are some more potential problems to look out for: • Assumptions about the size of data objects such as int and float can be risky. Previous:Errors and debugging.C Programming Tutorial Node:Porting Programs between computers. Previous:Porting Programs between computers. Node:Qu. whereas others may labour at it for some time! • Check for assumptions made about filenames. For example. The most likely area of incompatibility betwee compilers regards filing operations. valid characters etc. }
Node:Summary. Up:Errors and debugging
Porting Programs between computers
Programs written according to the style guidelines described in this book should be highly portable. }
2. Some computers may scarcely notice counting to 50000. • Check for functions which rely on the speed of a particular computer. scanf() is capable of producing a full spectrum of weird effects which have nothing to do with I/O. int i.

Next:Identifiers. Previous:Primitive Data Types.. "This is a string" and have type pointer to character.)
Constants
303
. a. Identifiers may not begin with a number. Up:Summary
Storage Classes
auto Local variable (redundant keyword) const No variable allocated..9.. uses no storage (except as a pointer) Node:Storage Classes. Up:Summary
Identifiers
Idenitifiers may contain the characters: 0.z and _ (the underscore character).Z. value doesn't change extern Variable is defined in another file static Value is preserved between function calls register Stored in a register. Next:Storage Classes. Previous:Storage Classes.g. A. Up:Summary
Primitive Data Types
char Holds any character int Integer type short int Integer no larger than int long int Integer no smaller than int float Floating point (real number) long float Double precision float double (ditto) void Holds no value. Node:Primitive Data Types. Next:Statements. if possible volatile Value can be changed by agents outside the program. (The compiler assumes that an object beginning with a number is a number.C Programming Tutorial Strings String constants are written in double quotes e. Previous:Constants. Node:Identifiers.

e."). } { int a.g. printf ("I love C because..C Programming Tutorial Node:Statements. }
Summary of Operators and Precedence The highest priority operators are listed first. Previous:Identifiers. a = 6. Any pair of curly braces may contain local declarations after the opening brace..
{ a = 6. Next:Character Utilities.g. printf ("I love C because.
a = 6. The curly braces do not end with a semi colon and stand in place of a single statement.
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 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
Identifiers
304
. Up:Summary
Statements
A single statement is any valid string in C which ends with a semi colon..").
A compound statement is any number of single statements groued together in curly braces. e..

Next:Special Control Characters..9 isxdigit(ch) Is 0.9 or a.C Programming Tutorial
== != & ^ | && || = += -= *= /= %= >>= <<= &= ^= |= is equal to is not equal to bitwise bitwise bitwsie logical logical AND exclusive OR includive OR AND OR left to right left to right left left left left left right right right right right right right right right right right to to to to to to to to to to to to to to to to right right right right right left left left left left left left left left left left
assign add assign subtract assign multiply assign divide assign remainder assign right shift assign left shift assign AND assign exclusive OR assign inclusive OR assign
Node:Character Utilities.z A.127 Statements 305
.F isspace(ch) Is white space character (space/newline/tab) ispunct(ch) Is punctuation or symbolic isalnum(ch) Is alphanumeric (alphavetic or number) isprint(ch) Is printable on the screen (and space) isgraph(ch) If the character is printable (not space) iscntrl(ch) Is a control character (not printable) isascii(ch) Is in the range 0. Up:Summary
Character Utilities
char ch.. Previous:Statements...
isalpha(ch) Is alphabetic a...Z isupper(ch) Is upper case islower(ch) Is lower case isdigit(ch) Is in the range 0..f or A.

Up:Summary
Special Control Characters
Control characters are invisible on the screen. Previous:Character Utilities. Next:Input/Output Functions. These characters are listed below.C Programming Tutorial iscsym(ch) Is a valid character for a C identifier toupper(ch) Converts character to upper case tolower(ch) Converts character to lower case toascii(ch) Converts character to ascii (masks off top bit) Node:Special Control Characters. (See Appendix C) Node:Input/Output Functions. Up:Summary
Input/Output Functions
printf () Formatted printing scanf () Formatted input analysis getchar() Get one character from stdin file buffer putchar() Character Utilities 306
. \b backspace BS \f form feed FF (also clear screen) \n new line NL (like pressing return) \r carriage return CR (cursor to start of line) \t horizontal tab HT \v vertical tab (not all versions) \" double quotes (not all versions) \' single quote character ' \\ backslash character \ \ddd character ddd where ddd is an ASCII code given in octal or base 8. Next:print conversions. Previous:Special Control Characters. They have special purposes usually to do with cursor movement and are written into an ordinary string or character by typing a backslash character \ followed by some other character.

C Programming Tutorial Put one charcter in stdout file buffer gets () Get a string from stdin puts () Put a string in stdout fprintf() Formatted printing to general files fscanf() Formatted input from general files fgets() Get a string from a file fputs() Put a string in a file fopen() Open/create a file for high level access fclose() Close a file opened by fopen() getc() Get one character from a file (macro?) ungetc(). returns true or false fread() Read a block of characters fwrite() Write a block of characters ftell() Returns file position fseek() Finds a file position rewind() Moves file position to the start of file fflush() Empties file buffers open() Open a file for low level use close() Close a file opened with open() creat() Create a new file read() Read a block of untranslated bytes write() Write a block of untranslated bytes Input/Output Functions 307
. Undo last get operation putc() Put a character to a file (macro?) fgetc() Get a character from a file (function) fputc() Put a character from a file (function) feof() End of file .

Previous:Maths Library Summary.
label is an identifier which occurs somewhere else in the given function and is defined as a label by using the colon:
label : printf ("Ugh! You used a goto!"). Next:Comparisons. For this reason it has been ignored in this book.
Node:reserved words list. Up:Top
Maths Library
310
. For completeness. and for those who insist on using it (may their programs recover gracefully) the form of the goto statement is as follows:
goto label.C Programming Tutorial atan(x) Inverse tangent of x in radians atan2(x. Previous:Summary. Up:Summary
goto
This word is redundant in C and encourages poor programming style.y) Inverse tangent of x/y in radians sinh(x) Hyperbolic sine cosh(x) Hyperbolic cosine tanh(x) Hyperbolic tangent Node:goto.

Next:Character Conversion Table. (This does mean that. Once a library has been included in a program. typed in upper case. The set of reserved words above is used to build up the basic instructions of C. but this is not recommended. Up:Top
All the Reserved Words
311
. its functions are defined and you cannot use their names yourself. you can not use them in programs your write Please note that this list is somewhat misleading. This is because most of the facilities which C offers are in libraries that are included in programs. the reserved words could be used as variable names. C requires all of these reserved words to be in lower case.All the Reserved Words
Here is a list of all the reserved words in C.) (A "d" by the word implies that it is used as part of a declaration.)
auto d break case char d continue default do double d else entry extern d float d for goto if int d long d register d return short d sizeof static d struct switch typedef d union d unsigned d while
also in modern implementations: enum d void d const d signed d volatile d
Node:Comparisons. Previous:reserved words list. Many more words are out of bounds.

else ....... */ * struct union
end.)... ? ! $ N/A N/A
The conditional expressions if and switch are essentially identical to Pascal's own words if and case but there is no redundant "then".. Input and Output in C can match all of BASICs string operations and provide more.. The loop constructions of C are far superior to those of either BASIC or Pascal however../ DIV.. though string variables can be more awkward to deal with.) { } do { } while (........ { . switch (. Pascal := = *.) { case : } /* . BASIC has no analogue of the switch construction.. ELSE N/A
if (.% printf ("..'). MOD PRINT "... case . N/A
FOR x = ..
NEXT x N/A
N/A
repeat until (. IF . THEN...
INPUT a
for (x = ..) { } while (. } ^ record N/A REM ..do begin end. of
REPEAT UNTIL .. mod writeln ('..
C = == *.. then ./ /.../ div.a). for x := .."..to begin end.... Three Languages: Words and Symbols Compared 312
.) ... though correspondingly less robust in terms of program crashability.'). else . Input and Output in C is more flexible than Pascal."
scanf (". while .... the following table will give you a rough and ready indication of how the main words and symbols of the three languages relate... readln (a).) if . read (a)... N/A BASIC = = *.. write ('..etc) or BBC BASIC..Three Languages: Words and Symbols Compared
If you are already familiar with Pascal (Algol.")..

2) printf ("22"). 3) printf ("The 3 wise men").x (or perhaps accounts. 5) The semi-colon (.Answers to questions
Chapter 1 1) A tool which translates high level language into machine language.3). 2) By typing the name of an executable file. Only macro names can be used if the header file is not included. printf ("The %d wise men". Chapter 7 1) A group of statements enclosed by curly braces {}. functions. 4) Most facilities are held in libraries Chapter 4 1) To provide a basic set of facilities to the user 2) The filename used by a computer to reference a device 3) accounts. statements. variables. 4) It signifies the end of a block. 3) By typing something like "cc filename" 4) NO! 5) Compiler errors and runtime errors. 2) Comments.) 3) Not necessarily. 4) Header file. (This is a matter of opinion.) Chapter 8
Answers to questions
320
.h 3) No. preprocessor commands. declarations.EXE) 5) By typing the name in 4) Chapter 5 1) #include <filename> or #include "filename" 2) stdio. of course. the return of control to somethng else. It starts wherever main() is.c 4) accounts. Chapter 3 1) printf ("Wow big deal").

j. 5) int can have values + or -. 9) printf ("%d". 6) I = 67.g. 4) The result is garbage.(int)23... after its name: e. function or macro 2) a.) <-. function (. 5) By using "return". Unsigned can only be + and can hold slightly larger + values than int.here { } 3) Yes. Chapter 11 1) With variable parameters or with return() 2) Where a function is definned. 7) int 8) At the function defintion and in the calling function. 5) * means "the contents of" and & means "the address of" comment!
Answers to questions
321
. 4) No and it is illegal. { return (a*b).b.1256).c. 4) double is twice the length of float and can hold significantly larger values. Chapter 10 1) A name for some variable.b) int a.C Programming Tutorial
1) The compiler thinks the rest of the program is all one Chapter 9 1) function (a. } 2) No.f 3) int i. 10) No. 3) The value is discarded.

another(x.h> main ()
Answers to questions
322
.y.y.h> 3) false 4) false Chapter 14 1) A variable which holds the address of another variable 2) With a * character. Chapter 12 1) A global variable can be accessed by any part of a program. 3) Local variables cannot leak out. Value parameters use their own local copies.j. 4) Variable parameters do. This expression initializes the place that number points to. 5) Because number has not been initialized. e.g. 3) Any type at all! 4) doubleptr = (double *)chptr.C Programming Tutorial
6) No. Nothing outside them can reach local variables. { } There are 6 storage spaces altogether. main () { float x. } another(x. not number itself. so they do not. (See main text) Chapter 15 printf 1) #include <stdio. Chapter 13 1) #define birthday 19 2) #include <math. 2) A local variable canonly be accessed by a select part of a program.y). int *i.y) float x. 5) int i.

5) if (1 != 23) { printf ("Thank goodness for mathematics"). putchar (ch). newline or tab 5) true. Low level I/O 1) The statement is possible provided putchar() is not implemented as a macro. 2) printf ("%d". for 2) while : at the start of each loop do : at the end of each loop for : at the start of each loop 3) do.-5.23). } 2) This depends on individual compilers 3) a) b) c) d) scanf 1) space.C Programming Tutorial
{ printf ("%2e".while 4) #include <stdio. Chapter 16 1) The thing(s) an operator acts upon. (Note however that the output is buffered so characters may not be seen on the output for some time!) 2) ch = getchar(). 4) variable = 10 . do. 3) rem = 5 % 2.5 % 2).h> #define TRUE 1 main () { char ch... It copies the input to the output: a simple way of writing on the screen. } Chapter 18 1) Three: while.while. while (true) { No conversion string Conversion string without matching value Probably nothing Conversion string without matching value
Answers to questions
323
.6.

Chapter 23 1) FILE is defined by stdio.C Programming Tutorial
ch = getchar(). 3) False. Valid array bounds from array[0][0] to array[3][4] Chapter 20 1) Arrays of characters. The function would return before the vaue could be incremented. 5) Overflow. 2) FILE *fp. underflow. } Chapter 19 1) The array identifier (without square brackets) is a pointer to the first element in the array. The actual types are double. -. It provides a generic pointer.h It is reserved only when this file is included. putchar (ch). i. 4) Yes. No! Arrays are always variable parameters. 2) static char *strings[]. 5) volatile 6) typedef double real. Chapter 22 1) double 2) Probably true. (See main text) 3) See the Morse code example. 3) double array[4][5]. They are meant for comparitive purposes only. long float and int.
Answers to questions
324
. 3) The length of a string (excluding NULL byte) 4) Joins two strings. Chapter 23 1) ++. It is not a built in part of the language. Could then initialize with braces {} and item list. It does not make sense to do arithmetic with enumerated data. This is implementation dependent. domain error. 2) You pass the array identifier. without square brackets. one which can be assigned to any other pointer type. Pointers to arrays of characters. Loss of accuracy and division by zero.and any assignment or unary operator 2) It could make a program too difficult to read 3) No.e.