If you want to learn the fundamentals of C programming, you've come to the right place. This article is excerpted from the book Beginning C, third edition, written by Ivor Horton (Apress, 2004; ISBN: 1590592530).
Programming in C - Developing Programs in C (Page 6 of 8 )
The process of developing programs in C may not be evident if youíve never written a program before. However, itís very similar to many other situations in life in which, at the beginning, it just isnít clear how youíre going to achieve your objective. Normally, you start with an idea of what you want to achieve, but you need to translate this into a more precise specification of what you want. Once youíve reached this more precise specification, you can work out the series of steps that will lead to your final objective. So having an idea that you want to build a house just isnít enough. You need to know what kind of house you want, how large itís going to be, what kinds of materials you have to build it with, and where you want to build it. This kind of detailed planning is also necessary when you want to write a program.
Letís go through the basic steps that you need to follow when youíre writing a program. The house analogy is a useful one, so weíll work with it for a while.
Understanding the Problem
The first step is to get a clear idea of what you want to do. It would be lunacy to start building your house before you had established what facilities it should provide: how many bedrooms, how many bathrooms, how big itís going to be, and so on. All these things affect the cost of the house in terms of materials and the work involved in building it. Generally, it comes down to a compromise that best meets your needs within the constraints of the money, the workforce, and the time thatís available for you to complete the project.
Itís the same with developing a program of any size. Even for a relatively straightforward problem, you need to know what kind of input to expect, how the input is to be processed, and what kind of output is requiredóand how itís going to look. The input could be entered with the keyboard, but it might also involve data from a disk file or information obtained over a telephone line or a network. The output could simply be displayed on the screen, or it could be printed; perhaps it might involve updating a data file on disk. For more complex programs, youíll need to look at many more aspects of what the program is going to do. A clear definition of the problem that your program is going to solve is an absolutely essential part of understanding the resources and effort needed for the creation of a finished product. Considering these details also forces you to establish whether or not the project is actually feasible.
To get the house built, youíll need detailed plans. These plans enable the construction workers to do their job and the plans describe in detail how the house will go togetheróall the dimensions, the materials to use, and so on. Youíll also need a plan of what is to be done and when. For example, youíll want the foundation dug before the walls are built, so the plan must involve segmenting the work into manageable units to be performed in a logical sequence.
Itís the same with a program. Youíll need to specify what the program does by dividing it into a set of well-defined and manageable chunks that are reasonably self-contained. Youíll also need to detail the way in which these chunks connect, as well as what information each chunk will need when it executes. This will enable you to develop the logic of each chunk, relatively independently from the rest of the program. If you treat a large program as one huge process that you try to code as a single chunk, chances are that youíll never get it to work.
Given the detailed design of a house, the work can start. Each group of construction workers will need to complete its part of the project at the right time. Each stage will need to be inspected to check that itís been done properly before the next stage begins. Omitting these checks could easily result in the whole house collapsing.
Of course, if a program is large, youíll write the source code one unit at a time. As one part is completed, you can write the code for the next. Each part will be based on the detailed design specifications, and youíll verify that each piece works, as far as you can, before proceeding to the next. In this way, youíll gradually progress to a fully working program that does everything you originally intended.
The house is complete, but there are a lot of things that need to be tested: the drainage, the water and electricity supplies, the heating, and so on. Any one of these areas can have problems that the contractors need to go back and fix. This is sometimes an iterative process, in which problems with one aspect of the house can be the cause of things going wrong somewhere else.
The mechanism with a program is similar. Each of your program modulesóthe pieces that make up your programówill need to be tested individually. When they donít work properly, you need to debug them. Debugging is the process of finding and correcting errors in your program. This term is said to have originated in the days when finding the errors in a program involved tracing where the information went and how it was processed by using the circuit diagram for the computer. The story goes that it was discovered how a computer program error was caused by an insect shorting part of the circuit in the computer. The problem was caused by a bug. Subsequently, the term bug was used to refer to any error in a program.
With a simple program, you can often find an error simply by inspecting the code. In general, though, the process of debugging usually involves adding extra program code to produce output that will enable you to check what the sequence of events is and what intermediate values are produced in a program. With a large program, youíll also need to test the program modules in combination because, although the individual modules may work, thereís no guarantee that theyíll work together! The jargon for this phase of program development is integration testing.