C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

programs they write. If changes need to be made to the program later, the original programmer can be
found to help. It’s also a good idea to include the filename that you use to save the program on disk at
the beginning of a program so that you can find a program on disk when you run across a printed
listing.


Note

This book might comment too much in some places, especially in the beginning
chapters. You are so unfamiliar with C that every little bit of explanation helps.

Tip

For testing purposes, you might find it useful to comment out a section of code by
putting /* and */ around it. By doing this, you cause C to ignore that section of code,
and you can concentrate on the piece of code you’re working on. Do not, however,
comment out a section of code that already contains comments because you cannot
embed one comment within another. The first */ that C runs across triggers the end of
the comment you started. When C finds the next */ without a beginning /*, you get an
error.

Whitespace


Whitespace is the collection of spaces and blank lines you find in many programs. In a way,
whitespace is more important than comments in making your programs readable. People need
whitespace when looking through a C program because those programs are more readable than
programs that run together too much. Consider the following program:


Click here to view code image


#include <stdio.h>
main(){float a, b;printf("How much of a bonus did you get?
");scanf(" %f",
&a);b = .85 * a;printf("If you give 15 percent to charity, you will
still have %.2f.", b);return 0;}

To a C compiler, this is a perfectly good C program. You might get a headache looking at it, however.
Although the code is simple and it doesn’t take a lot of effort to figure out what is going on, the
following program is much easier to decipher, even though it has no comments:


Click here to view code image


#include <stdio.h>
main()
{
float a, b;
Free download pdf