
- •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 |
61 |
Using Comments to Disable
Comments are ignored by the compiler. No matter what lies between the /* and the */, it’s skipped over. Even vital, lifesaving information, mass sums of cash, or the key to eternal youth — all these are ignored if they’re nestled in a C language comment.
Modify the MADLIB1.C source code, changing the last part of the program to read:
/* Display the output */
/*
printf(“\n\nDon’t touch that %s %s!\n”,adjective,food); printf(“I just %s the %s!\n”,chore,furniture);
*/
To make the modification, follow these cinchy steps:
1.Insert a line with /* on it before the first printf() function in this example.
2.Insert a line with */ on it after the second printf() function.
With the last two printf() statements disabled, save the file to disk and recompile it. It runs as before, but the resulting “mad lib” isn’t displayed. The reason is that the final two printf() functions are “commented out.”
You can use comments to disable certain parts of your program. If some thing isn’t working correctly, for example, you can “comment it out.” You may also want to include a note to yourself, explaining why that section is commented out.
Sometimes you may notice that something which should be working isn’t working. The reason is that you may have accidentally commented it out. Always check your /* and */ comment bookends to make sure that they match up the way you want them to.
By using an editor with color-coded text, you can easily spot missing */
characters to end a comment. If you notice that a greater chunk of your source code is colored as a comment, a misplaced */ is probably to blame.

62 |
Part I: Introduction to C Programming |
The Perils of “Nested” Comments
The most major of the faux pas you can commit with comments is to “nest” them, or to include one set of comments inside another. To wit, I present the following C program fragment:
if(all_else_fails) |
|
{ |
|
display_error(erno); |
/* erno is already set */ |
walk_away(); |
|
} |
|
else |
|
get_mad(); |
|
Don’t worry about understanding this example; it all comes clear to you later in this book. However, notice that the display_error function has a comment after it: erno is already set. But suppose that, in your advanced under standing of C that is yet to come, you want to change the gist of this part of the program so that only the get_mad() function is executed. You comment out everything except that line to get it to work:
/* if(all_else_fails)
{
display_error(erno); /* erno is already set */ walk_away();
}
else */
get_mad();
Here, the C compiler sees only the get_mad function, right?
Wrong! The comment begins on the first line with the /*. But it ends on the line with the display_error() function. Because that line ends with */ — the comment bookend — that’s the end of the “comment.” The C compiler then starts again with the walk_away function and generates a parse error on the rogue curly brace floating in space. The second comment bookend (just above the get_mad() function) also produces an error. Two errors! How heinous.
This example shows a nested comment, or a comment within a comment. It just doesn’t work. Figure 5-1 illustrates how the C compiler interprets the nested comment.
To avoid the nested-comment trap, you have to be careful when you’re dis abling portions of your C program. The solution in this case is to uncomment the erno is already set comment. Or, you can comment out each line individually, in which case that line would look like this:

Chapter 5: To C or Not to C |
63 |
/* display_error(erno); |
/* erno is already set */ |
This method works because the comment still ends with */. The extra /* inside the comment is safely ignored.
Yeah, nested comments are nasty, but nothing you need to worry about at this point in the game.
Note that the C++ style of comments, //, doesn’t have a nesting problem.
Figure 5-1:
The perils of a nested comment.
What you wrote:
/* if(all_else_fails)
{
display_error(erno); /* erno is already set */ walk_away();
}
else */
get_mad();
What the compiler sees:
/* if(all_else_fails)
{
display_error(erno); /* erno is already set */ walk_away();
}
else
*/ |
|
! |
|
OPS |
|
|
|
|
|
get_mad(); |
O |
|
|
|
|
More errors! |
|
ACK! |
|
|

64 |
Part I: Introduction to C Programming |