Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C For Dummies 2nd Ed 2004.pdf
Скачиваний:
43
Добавлен:
17.08.2013
Размер:
8.34 Mб
Скачать

20

Part I: Introduction to C Programming

Before you cower in shame, fear not, gentle programmer newbie person. Errors happen. You deal with them. Like this:

1.Reedit your source code, saving the fixed-up file to disk.

2.Recompile the source code.

3.Run the result.

Errors can still happen. Heck, you may never get to Step 3! But these steps show you how to deal with them.

It happens.

I might remind you to look at the C language development cycle from Chapter 1. Note Steps 4 and 6. Nod your head wisely in agreement.

Reediting your source code file

Source code is not carved in stone — or silicon, for that matter. It can be changed. Sometimes, the changes are necessary, in the case of errors and boo-boos. At other times, you may just want to modify your program, adding a feature or changing a message or prompt — what the hard-core C geeks call tweaking or twiddling. To do that, you have to reedit your source code file.

For example, the GOODBYE program from Chapter 1 displays a message on the screen:

Goodbye, cruel world!

This program can easily be modified to show any message you like. To do so, use your editor and change the source code file, replacing the original mes­ sage with your newer, pithier message. Follow these steps:

1.Use your text editor to reedit the GOODBYE.C source code.

2.Edit Line 5, which looks like this:

printf(“Goodbye, cruel world!\n”);

3.Replace the text Goodbye, cruel world! with Farewell, you ugly toad!

printf(“Farewell, you ugly toad!\n”);

Change only the text between the double quotes. That’s the information that is displayed on the screen. Everything else — don’t touch!

Chapter 2: C of Sorrow, C of Woe 21

4.Double-check your work.

5.Save the file to disk.

It’s okay to overwrite the original; your modified file becomes the new GOODBYE.C source code file.

Now you’re ready to recompile your source code, as covered in the next section.

“Reedit your source code file” means to use your text editor to modify the source code, the text file that contains the C language instructions.

You reedit the source code file to repair an error caught by the compiler or linker or to modify the program. This situation happens a lot.

If you’re using the command prompt to run your editor, don’t forget that you can use the up-arrow key to recall previous commands (in certain command-prompt environments). In this case, press the up-arrow key a few times until the original command to edit the GOODBYE.C source code file reappears at the prompt.

Recompiling (or the C equivalent of the “do-over”)

Recompiling means to make the program one more time — to rework the steps you went through to create the program originally. This process usually hap­ pens after you modify or change the source code, such as you do in the pre­ ceding section. Because the source code is different, you have to feed it to the compiler again to generate the new, better (and, hopefully, bug-free) program.

To recompile the new GOODBYE.C source code, use your compiler as outlined in Appendix A. For most everyone, that’s

gcc goodbye.c -o goodbye

Press the Enter key and pray that no error messages appear, and then you’re done. The new program has been created.

Run the program! Type the proper command — either goodbye or ./goodbye — at the prompt to see the new, stunning output.

Who knew that it would be so darn easy to display such crap on the computer’s screen?