Introduction to C Programming by Rob Miles, Electronic Engineering


C

A look at C
Making C Run
Creating C Programs
What Comprises a C Program?
The Advantages of C
The Disadvantages of C

A look at C

There are literally hundreds of programming languages around, you will need to know at least 3!
We are going to learn a language called C. C is a very flexible and powerful programming language originally designed in the early 1970s. It is famous as the language the UNIX operating system was written in, and was specially designed for this. However its use has now spread way beyond that field and it is currently very popular.

C is a professional language. So what do I mean by that? Consider the chain saw. If I, Rob Miles, want to use a chain saw I will hire one from a shop. As I am not an experienced chain saw user I would expect it to come with lots of built in safety features such as guards and automatic cut outs. These will make me much safer with the thing but will probably limit the usefulness of the tool, i.e. because of all the safety stuff I might not be able to cut down certain kinds of tree. If I was a real lumberjack I would go out and buy a professional chain saw which has no safety features whatsoever but can be used to cut down most anything. If I make a mistake with the professional tool I could quite easily lose my leg, something the amateur machine would not let happen.

In programming terms this means is that C lacks some safety features provided by other programming languages. This makes the language much more flexible.

However, if I do something stupid C will not stop me, so I have a much greater chance of crashing the computer with a C program than I do with a safer language.

This is not something to worry about, you should always work on the basis that any computer will tolerate no errors on my part and anything that I do which is stupid will always cause a disaster!

Making C Run

You actually write the program using some form of text editor - which may be part of the compiling and linking system.
C is usually a compiled programming language. The computer cannot understand the language directly, so a program called a compiler converts the C into the machine code instructions which do the job. Actually getting a program to run is however a two stage process. First you show your program, often called the source, to the compiler. If the compiler gives it the thumbs up you then perform a process called linking.

Linking is when all the various portions of your program are brought together to form something which can be run. You might ask "Why to we link things? the compiler has created a machine code version of my program, can't I just run that?". The reason that we have the additional linking process is that it allows us to reuse standard pieces of code from a library. Many things your program will do are common to lots of other programs, for example you will want to read information from the keyboard and you will want to send information to the display. Rather than compile the program code which does this every time you compile your program, a much more efficient way is to put a compiled version of this code into a library. Your program just contains a reference to the particular function you want to use, the linker then loads the relevant part from the library when it creates your program.

It is possible to get the compiler to give you warnings in this case.
Note that a side effect of this is that if you refer to a function which does not exist, the compiler will not mind particularly - but the linker will not find the item in its library, and thus give you an error.

Once the linker has finished you are left with a free standing file which is your program. If you run this your program gets control!

Creating C Programs

The actual business of constructing and compiling the depends on the computer you are using and the particular version of C. We will look at the business of producing your program in the laboratory section of this course. Initially it is best if we just work through your programs on paper. I reckon that you write programs best when you are not sitting at the computer, i.e. the best approach is to write (or at least map out) your solution on paper a long way away from the machine. Once you are sitting in front of the keyboard there is a great temptation to start pressing keys and typing something in which might work. This is not good technique. You will almost certainly end up with something which almost works, which you will then spend hours fiddling with to get it going.

If you had sat down with a pencil and worked out the solution first you would probably get to a working system in around half the time. I am not impressed by hacking programmers who spend whole days at terminals fighting with enormous programs and debugging them into shape. I am impressed by someone who turns up, types in the program and makes it work first time!

What Comprises a C Program?

A program is the thing that you write to perform a particular task.

It will actually be a file of text, often called a source file. This is what the compiler acts on. A source file contains three things :

To take these in turn

Controlling the Compiler

The compiler directives are the same for all versions of C.
One of the very powerful features of C is the way in which you can change the way the compiler processes your program by including directives. There are many directives available, in a C program a directive is always preceded by the # character and must appear right at the beginning of a line. Directives can be used to "build in" particular values, for example constants like PI, and also allow you to change which parts of the program which the compiler works on, making it possible to use the same piece of program on several different types of computer.

Storing the Data

Programs work by processing data. The data has to be stored within the computer whilst the program processes it. All computer languages support variables of one form or another. A variable is simply a named location in which a value is held whilst the program runs. C also lets you build up structures which can hold more than one item, for example a single structure could hold all the information about a particular bank customer.

Describing the Solution

The actual instructions which describe your solution to the problem must also be part of your program. In the case of C a lump of program which does one particular thing is called a function.

Seasoned programmers break down a problem into a number of smaller ones and make a function for each.
A function can be very small, or very large. It can return a value which may or may not be of interest. It can have any name you like, and your program can contain as many functions as you see fit. One function may refer to others. The C language also has a large number of function libraries available which you can use. These save you from "re-inventing the wheel" each time you write a program.

Within the function there will be a number of statements. A statement is an instruction to perform one particular operation, for example add two numbers together and store the result. The really gripping thing about programs is that a statement can decide which statement is performed next, so that your program can look at things and decide what to do.

You give a name to each function that you create, and you try to make the name of the function fit what it does, for example menu or save_file. The C language actually runs your program by looking for a function with a special name, main. This function is called when your program starts running, and when main finishes, your program ends.

The Advantages of C

The good news about C is that you can write code which runs quickly, and your program is very "close to the hardware". By that I mean that you can access low level facilities in your computer quite easily, without the compiler or run time system stopping you from doing something potentially dangerous.

The use of compiler directives to the pre-processor make it possible to produce a single version of a program which can be compiled on several different types of computer. In this sense C is said to be very portable. The function libraries are standard for all versions of C so they can be used on all systems.

The Disadvantages of C

The disadvantages of C fall neatly from the advantages. The biggest one is that you can write C programs which can fail in very catastrophic ways. These programs will appear totally valid as far as the compiler is concerned but will not work and may even cause your computer to stop. A more picky language would probably notice that you were doing something stupid in your program and allow you to find the error before it crashed your computer! However a more picky language would probably not allow you to write the program in the first place!

Another disadvantage of C is that it allows you to write very terse code. You can express exactly what you want to do in very few statements. You might think that this is nice, because it makes your programs even more efficient, but it has the side effect of making them much harder to understand. At the time you write the code you know exactly what each part is supposed to do. If you come back to the program in several months you will need time to "get back inside it". If the code is written very tightly you will take much longer to do this, and other people may not be able to understand it at all! I write code which is not the most efficient possible, but is easy to understand. I am sacrificing program performance for ease of maintenance.


Rob Miles, R.S.Miles@e-eng.hull.ac.uk, Electronic Engineering
HTML by Bronwen Reid, July 1995