Introduction to C Programming by Rob Miles, Electronic Engineering
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!
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.
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!
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!
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
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.
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.
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 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 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.