
- •Table of Contents
- •Introduction
- •About This Here Dummies Approach
- •How to Work the Examples in This Book
- •Foolish Assumptions
- •Icons Used in This Book
- •Final Thots
- •The C Development Cycle
- •From Text File to Program
- •The source code (text file)
- •The compiler and the linker
- •Running the final result
- •Save It! Compile and Link It! Run It!
- •Reediting your source code file
- •Dealing with the Heartbreak of Errors
- •The autopsy
- •Repairing the malodorous program
- •Now try this error!
- •The Big Picture
- •Other C Language Components
- •Pop Quiz!
- •The Helpful RULES Program
- •The importance of being \n
- •Breaking up lines\ is easy to do
- •The reward
- •More on printf()
- •Printing funky text
- •Escape from printf()!
- •A bit of justification
- •Putting scanf together
- •The miracle of scanf()
- •Experimentation time!
- •Adding Comments
- •A big, hairy program with comments
- •Why are comments necessary?
- •Bizarr-o comments
- •C++ comments
- •Using Comments to Disable
- •The More I Want, the More I gets()
- •Another completely rude program example
- •And now, the bad news about gets()
- •The Virtues of puts()
- •Another silly command-prompt program
- •puts() and gets() in action
- •More insults
- •puts() can print variables
- •The Ever-Changing Variable
- •Strings change
- •Running the KITTY
- •Hello, integer
- •Using an integer variable in the Methuselah program
- •Assigning values to numeric variables
- •Entering numeric values from the keyboard
- •The atoi() function
- •So how old is this Methuselah guy, anyway?
- •Basic mathematical symbols
- •How much longer do you have to live to break the Methuselah record?
- •The direct result
- •Variable names verboten and not
- •Presetting variable values
- •The old random-sampler variable program
- •Maybe you want to chance two pints?
- •Multiple declarations
- •Constants and Variables
- •Dreaming up and defining constants
- •The handy shortcut
- •The #define directive
- •Real, live constant variables
- •Numbers in C
- •Why use integers? Why not just make every number floating-point?
- •Integer types (short, long, wide, fat, and so on)
- •How to Make a Number Float
- •The E notation stuff
- •Single-character variables
- •Char in action
- •Stuffing characters into character variables
- •Reading and Writing Single Characters
- •The getchar() function
- •The putchar() function
- •Character Variables As Values
- •Unhappily incrementing your weight
- •Bonus program! (One that may even have a purpose in life)
- •The Sacred Order of Precedence
- •A problem from the pages of the dentistry final exam
- •The confounding magic-pellets problem
- •Using parentheses to mess up the order of precedence
- •The computer-genie program example
- •The if keyword, up close and impersonal
- •A question of formatting the if statement
- •The final solution to the income-tax problem
- •Covering all the possibilities with else
- •The if format with else
- •The strange case of else-if and even more decisions
- •Bonus program! The really, really smart genie
- •The World of if without Values
- •The problem with getchar()
- •Meanwhile, back to the GREATER problem
- •Another, bolder example
- •Exposing Flaws in logic
- •A solution (but not the best one)
- •A better solution, using logic
- •A logical AND program for you
- •For Going Loopy
- •For doing things over and over, use the for keyword
- •Having fun whilst counting to 100
- •Beware of infinite loops!
- •Breaking out of a loop
- •The break keyword
- •The Art of Incrementation
- •O, to count backward
- •How counting backward fits into the for loop
- •More Incrementation Madness
- •Leaping loops!
- •Counting to 1,000 by fives
- •Cryptic C operator symbols, Volume III: The madness continues
- •The answers
- •The Lowdown on while Loops
- •Whiling away the hours
- •Deciding between a while loop and a for loop
- •Replacing those unsightly for(;;) loops with elegant while loops
- •C from the inside out
- •The Down-Low on Upside-Down do-while Loops
- •The devil made me do-while it!
- •do-while details
- •The always kosher number-checking do-while loop
- •Break the Brave and Continue the Fool
- •The continue keyword
- •The Sneaky switch-case Loops
- •The switch-case Solution to the LOBBY Program
- •The Old switch-case Trick
- •The Special Relationship between while and switch-case
- •A potentially redundant program in need of a function
- •The noble jerk() function
- •Prototyping Your Functions
- •Prototypical prototyping problems
- •A sneaky way to avoid prototyping problems
- •The Tao of Functions
- •The function format
- •How to name your functions
- •Adding some important tension
- •Making a global variable
- •An example of a global variable in a real, live program
- •Marching a Value Off to a Function
- •How to send a value to a function
- •Avoiding variable confusion (must reading)
- •Functions That Return Stuff
- •Something for your troubles
- •Finally, the computer tells you how smart it thinks you are
- •Return to sender with the return keyword
- •Now you can understand the main() function
- •Give that human a bonus!
- •Writing your own dot-H file
- •A final warning about header files
- •What the #defines Are Up To
- •Avoiding the Topic of Macros
- •A Quick Review of printf()
- •The printf() Escape Sequences
- •The printf() escape-sequence testing program deluxe
- •Putting PRINTFUN to the test
- •The Complex printf() Format
- •The printf() Conversion Characters
- •More on Math
- •Taking your math problems to a higher power
- •Putting pow() into use
- •Rooting out the root
- •Strange Math? You Got It!
- •Something Really Odd to End Your Day
- •The perils of using a++
- •Oh, and the same thing applies to a --
- •Reflections on the strange ++a phenomenon
- •On Being Random
- •Using the rand() function
- •Planting a random-number seed
- •Randoming up the RANDOM program
- •Streamlining the randomizer
- •Arrays
- •Strings
- •Structures
- •Pointers
- •Linked Lists
- •Binary Operators
- •Interacting with the Command Line
- •Disk Access
- •Interacting with the Operating System
- •Building Big Programs
- •Use the Command-Line History
- •Use a Context-Colored Text Editor
- •Carefully Name Your Variables
- •Breaking Out of a Loop
- •Work on One Thing at a Time
- •Break Up Your Code
- •Simplify
- •Talk through the Program
- •Set Breakpoints
- •Monitor Your Variables
- •Document Your Work
- •Use Debugging Tools
- •Use a C Optimizer
- •Read More Books!
- •Setting Things Up
- •The C language compiler
- •The place to put your stuff
- •Making Programs
- •Finding your learn directory or folder
- •Running an editor
- •Compiling and linking
- •Index

Chapter 5
To C or Not to C
In This Chapter
Inserting notes for yourself
Using fancy commenting techniques
Borrowing C++ comments
Disabling code with comments
Avoiding nested comments
An important part of programming is remembering what the heck it is you’re doing. I’m not talking about the programming itself — that’s easy
to remember, and you can buy books and references galore in case you don’t. Instead, the thing you have to remember is what you are attempting to make a program do at a specific spot. You do that by inserting a comment in your source code.
Comments aren’t really necessary for the small programs you’re doing in this book. Comments don’t begin to become necessary until you write larger programs — on the scope of Excel or Photoshop — where you can easily lose your train of thought. To remind yourself of what you’re doing, you should stick a comment in the source code, explaining your approach. That way, when you look at the source code again, your eyes don’t glaze over and the drool doesn’t pour, because the comments remind you of what’s going on.
Adding Comments
Comments in a C program have a starting point and an ending point. Every thing between those two points is ignored by the compiler, meaning that you can stick any text in there — anything — and it doesn’t affect how the pro gram runs.
/* This is how a comment looks in the C language */

56 |
Part I: Introduction to C Programming |
This line is a fine example of a comment. What follows is another example of a comment, but the type that gives this book its reputation:
/*
Hello compiler! Hey, error on this: pirntf! Ha! Ha! You can’t see me! Pbbtbtbt!
Nya! Nya! Nya! */
The beginning of the comment is marked by the slash and the asterisk: /*.
The end of the comment is marked by the asterisk and the slash: */.
Yup, they’re different.
The comment is not a C language statement. You do not need a semi colon after the */.
A big, hairy program with comments
The following source code is MADLIB1.C. It uses the printf() and scanf() functions described in Chapter 4 to create a short yet interesting story:
/*
MADLIB1.C Source Code Written by (your name here) */
#include <stdio.h>
int main()
{
char adjective[20]; char food[20]; char chore[20]; char furniture[20];
/* Get the words to use in the madlib */ |
|
printf(“Enter an adjective:”); |
/* prompt */ |
scanf(“%s”,&adjective); |
/* input */ |
printf(“Enter a food:”); scanf(“%s”,&food);
printf(“Enter a household chore (past tense):”); scanf(“%s”,&chore);
printf(“Enter an item of furniture:”); scanf(“%s”,&furniture);
/* Display the output */

Chapter 5: To C or Not to C |
57 |
printf(“\n\nDon’t touch that %s %s!\n”,adjective,food); printf(“I just %s the %s!\n”,chore,furniture);
return(0);
}
Type the source code exactly as written. The only thing new should be the comments. Each one begins with /* and ends with */. Make sure that you get those right: A slash-asterisk begins the comment, and an asterisk-slash ends it. (If you’re using a color-coded editor, you see the comments all coded in the same color.)
Save the file to disk and name it MADLIB1.C.
Compile. Run.
Here is a sample of the program’s output:
Enter an adjective:hairy
Enter a food:waffle
Enter a household chore (past tense):vacuumed
Enter an item of furniture:couch
Don’t touch that hairy waffle!
I just vacuumed the couch!
Oh, ha-ha! Ouch! My sides!
This program is long and looks complex, but it doesn’t use any new tricks. Everything here, you have seen already: char to create string variables, printf() to display text and string variables, and scanf() to read the keyboard. Yawn.
MADLIB1.C uses these four string variables: adjective, food, chore, and furniture. All four are created by the char keyword, and 20 characters of storage are set aside for each one. Each of the string variables is filled by scanf() with your keyboard input.
Each of the final printf() functions contains two %s placeholders. Two string variables in each function supply the text for the %s placeholders.
The second-to-last printf() function begins with two newline characters, \n \n. These characters separate the input section, where you enter the bits of text, from the program’s output. Yes, newlines can appear any where in a string, not just at the end.
MADLIB1.C has five comments. Make sure that you can find each one. Notice that they’re not all the same, yet each one begins with /* and ends with */.

58 |
Part I: Introduction to C Programming |
Why are comments necessary?
Comments aren’t necessary for the C compiler. It ignores them. Instead, com ments are for you, the programmer. They offer bits of advice, suggestions for what you’re trying to do, or hints on how the program works. You can put any thing in the comments, though the more useful the information, the better it helps you later on.
Most C programs begin with a few lines of comments. All my C programs start with information such as the following:
/* COOKIES.C
Dan Gookin, 1/20/05 @ 2:45 a.m.
Scan Internet cookie files for expired dates and delete.
*/
These lines tell me what the program is about and when I started working on it.
In the source code itself, you can use comments as notes to yourself, such as
/* Find out why this doesn’t work */
or this:
save=itemv; |
/* Save old value here */ |
or even reminders to yourself in the future:
/*
Someday you will write the code here that makes the computer remember what it did last time this program ran.
*/
The point is that comments are notes for yourself. If you were studying C pro gramming in school, you would write the comments to satiate the fixations of your professor. If you work on a large programming project, the comments placate your team leader. For programs you write, the comments are for you.
Comment Styles of the Nerdy
and Not-Quite-Yet-Nerdy
The MADLIB1.C program contains five comments and uses three different com menting styles. Though you can comment your programs in many more ways, these are the most common: