Programming in C - Common Mistakes
(Page 8 of 8 )
Mistakes are a fact of life. When you write a computer program in C, the compiler must convert your source code to machine code, so there must be some very strict rules governing how you use the language. Leave out a comma where one is expected, or add a semicolon where you shouldn’t, and the compiler won’t be able to translate your program into machine code.
You’ll be surprised just how easy it is to introduce typographical errors into a program, even after years of practice. If you’re lucky, these errors will be picked up when you compile or link your program. If you’re really unlucky, they can result in your program apparently working fine but producing some intermittent erratic behavior. You can end up spending a lot of time tracking these errors down.
Of course, it’s not only typographical errors that cause problems. You’ll often find that your detailed implementation is just not right. Where you’re dealing with complicated decisions in your program, it’s easy to get the logic wrong. Your program may be quite accurate from a language point of view, and it may compile and run without a problem, but it won’t produce the right answers. These kinds of errors can be the most difficult to find.
Points to Remember It would be a good idea to review what you’ve gleaned from your first program. You can do this by looking at the overview of the important points in Figure 1-4.
Summary You’ve reached the end of the first chapter, and you’ve already written a few programs in C. You’ve covered quite a lot of ground, but at a fairly gentle pace. The aim of this chapter was to introduce a few basic ideas rather than teach you a lot about the C programming language. You should be confident about editing, compiling, and running your programs. You probably have only a vague idea about how to construct a C program at this point. It will become much clearer when you’ve learned a bit more about C and written some programs with more meat to them.
In the next chapter, you’ll move on to more complicated things than just producing text output using theprintf()function. You’ll manipulate information and get some rather more interesting results. And by the way, theprintf()function does a whole lot more than just display text strings—as you’ll see soon.
Exercises The following exercises enable you to try out what you’ve learned in this chapter. If you get stuck, look back over the chapter for help. If you’re still stuck, you can download the solutions from the Downloads area of the Apress website (http://www.apress.com), but that really should be a last resort.

Figure 1-4. Elements of a simple program
Exercise 1-1. Write a program that will output your name and address using a separate printf()statement for each line of output.
Exercise 1-2. Modify your solution for the previous exercise so that it produces all the output using only one printf() statement.
Exercise 1-3. Write a program to output the following text, exactly as it appears here:
--------------------------------------------------
"It's freezing in here," he said coldly.
--------------------------------------------------