Using C: Seven Steps

I l @ ve RuBoard

Using C: Seven Steps

C is a compiled language. If you are accustomed to using a compiled language, such as Pascal or FORTRAN, you will be familiar with the basic steps in putting together a C program. If your background is in an interpreted language, however ”such as BASIC or Logo ”or if you have no background at all, you need to learn how to compile. We'll soon guide you through that process, and you'll see that it is straightforward and sensible . First, to give you an overview of programming, we'll break down the act of writing a C program into seven steps (see Figure 1.3).

Figure 1.3. The seven steps of programming.
graphics/01fig03.jpg

Step 1: Define the Program Objectives

Naturally enough, you should start with a clear idea of what you want the program to do. Think in terms of the information your program needs, the feats of calculation and manipulation the program needs to do, and the information the program should report back to you. At this level of planning, you should be thinking in general terms, not in terms of some specific computer language.

Step 2: Design the Program

After you have a conceptual picture of what your program ought to do, you should decide how the program will go about it. What should the user interface be like? How should the program be organized? Who will the target user be? How much time do you have to complete the program?

You also need to decide how to represent the data in the program and, possibly, in auxiliary files, and which methods to use to process the data. As you first learn programming in C, the choices will be simple, but as you deal with more complex situations, you'll find that these decisions require more thought. Choosing a good way to represent the information can often make designing the program and processing the data much easier.

Again, you should be thinking in general terms, not about specific code, but some of your decisions may be based on general characteristics of the language. For example, a C programmer has more options in data representation than, say, a BASIC programmer.

Step 3: Write the Code

Now that you have a clear design for your program, you can begin to implement it by writing the code. That is, you translate your program design into the C language. Here is where you really have to put your knowledge of C to work. You can sketch your ideas on paper, but eventually you have to get your code into the computer. The mechanics of this process depend on your programming environment. We'll present the details for some common environments soon. In general, you use a text editor to create what is called a source code file. This file contains the C rendition of your program design. Listing 1.1 shows an example of C source code.

Listing 1.1 An example of C source code.
 #include <stdio.h> int main(void) {   int dogs;   printf ("How many dogs do you have?\n");   scanf ("%d", &dogs);   printf ("So you have %d dog(s)!\n", dogs);   return 0; } 

As part of this step, you should document your work. The simplest way is to use C's comment facility to incorporate explanations into your source code. We'll explain more about using comments in Chapter 2, "Introducing C."

Step 4: Compile

The next step is to compile the source code. Again, the details depend on your programming environment, and we'll look at some common environments shortly. Here, we'll take a more conceptual view of what happens.

The compiler is a program whose job is to convert source code into executable code. Executable code is code in the native, or machine, language of your computer. Different computers have different machine languages, and a C compiler translates C to a particular machine language. C compilers also incorporate code from C libraries into the final program; the libraries contain a fund of standard routines, such as printf() and scanf() , for your use. (More accurately, a program called a linker brings in the library routines, but on most systems the compiler runs the linker for you.) The end result is an executable file containing code that the computer understands and that you can run.

The compiler also checks that your program is valid C. If the compiler finds errors, it reports them to you and doesn't produce an executable file. Understanding a particular compiler's complaints is another skill you will pick up.

Step 5: Run the Program

Traditionally, the executable file is a runnable program. To run the program in many common environments, including UNIX and MS-DOS, just type the name of the executable file. Other environments, such as VMS on a VAX, might require a run command or some other mechanism. Integrated development environments ( IDEs ), such as those provided for Windows and Macintosh environments, let you edit and execute your C program from within the IDE by selecting choices from a menu or by pressing special keys.

Step 6: Test and Debug the Program

The fact that your program runs is a good sign, but it's possible that it could run incorrectly. Therefore, you should check to see that your program does what it is supposed to do. You'll find that some of your programs have mistakes ” bugs , in computer jargon. Debugging is finding and fixing program errors. Making mistakes is a natural part of learning. It seems inherent to programming, too, so when you combine learning and programming, you had best prepare yourself to be reminded often of your fallibility. As you become a more powerful and subtle programmer, your errors, too, will become more powerful and subtle.

You have many opportunities to err. You can make a basic design error. You can implement good ideas incorrectly. You can overlook unexpected input that messes up your program. You can use C incorrectly. You can make typing errors. You can put parentheses in the wrong place, and so on. You'll find your own items to add to this list.

Fortunately, the situation isn't hopeless, although there might be times when you think it is. The compiler catches many kinds of errors, and there are things you can do to help yourself track down the ones that the compiler doesn't catch. Throughout this book, we'll give you debugging advice.

Step 7: Maintain and Modify the Program

When you create a program for yourself or for someone else, that program could see extensive use. If it does, you'll probably find reasons to make changes in it. Perhaps there is a minor bug that shows up only when someone enters a name beginning with Zz , or you might think of a better way to do something in the program. You could add a clever new feature. You might adapt the program so that it runs on a different computer system. All these tasks are greatly simplified if you document the program clearly and if you follow sound design practices.

Commentary

Programming is not always as linear a process as we've just described. Sometimes you have to go back and forth between steps. For instance, when you are writing code, you might find that your plan was impractical , you may see a better way of doing things, or after you see how a program runs, you might feel motivated to change the design. Documenting your work helps you move back and forth between levels.

Most learners tend to neglect steps 1 and 2 (defining program objectives and designing the program) and go directly to step 3 (writing the program). The first programs you write are simple enough that you can visualize the whole process in your head. If you make a mistake, it's easy to find. As your programs grow longer and more complex, mental visualizations begin to fail, and errors get harder to find. Eventually, those who neglect the planning steps are condemned to hours of lost time, confusion, and frustration as they produce ugly, dysfunctional , and abstruse programs.

The moral here is that you should develop the habit of planning before coding. Use the ancient but honorable pen-and-pencil technology to jot down the objectives of your program and to outline the design. If you do so, you eventually reap substantial dividends in time saved and satisfaction gained .

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net