
- •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 18: Do C While You Sleep 235
This program creates a 10-by-9 square (okay — grid) of numbers and letters by using a nested-loop arrangement.
Enter the source code into your editor. Save your efforts to disk as GRID.C.
Compile the program. Notice that putting two for statements together doesn’t cause the compiler to spew errors at you (unless you made a typo somewhere).
Run. Here’s what the output should look like:
Here is thy grid...
1-A 1-B 1-C 1-D 1-E 1-F 1-G 1-H 1-I 1-J 2-A 2-B 2-C 2-D 2-E 2-F 2-G 2-H 2-I 2-J 3-A 3-B 3-C 3-D 3-E 3-F 3-G 3-H 3-I 3-J 4-A 4-B 4-C 4-D 4-E 4-F 4-G 4-H 4-I 4-J
5-A 5-B 5-C 5-D 5-E 5-F 5-G 5-H 5-I 5-J 6-A 6-B 6-C 6-D 6-E 6-F 6-G 6-H 6-I 6-J 7-A 7-B 7-C 7-D 7-E 7-F 7-G 7-H 7-I 7-J 8-A 8-B 8-C 8-D 8-E 8-F 8-G 8-H 8-I 8-J 9-A 9-B 9-C 9-D 9-E 9-F 9-G 9-H 9-I 9-J
Wow. Such efficiency should please any government bureaucracy.
The first, outer for loop counts from 1 to 10.
The inner for loop may seem strange, but it’s not. It’s only taking advan tage of the dual-number/-character nature of letters in a computer. The character variable b starts out equal to the letter A and is incremented one letter at a time, up to the letter K. What happens is that b is set equal to the letter A’s ASCII value, which is 65. The variable b then increments up to the letter K’s ASCII value, which is 75. It’s sneaky, but doable.
The printf() function displays the numbers and letters as the inner loop spins. You can see this process on your screen: The outer loop stays at one number while the letters A through K are printed. Then, the outer loop is incremented, and the next row of letters is printed.
Note that the printf() function has a space after the %c character. That’s what keeps the columns in the grid from running into each other.
The putchar() function displays a single character on the screen. In GRID.C, it’s used to display a \n newline character at the end of each row.
Break the Brave and Continue the Fool
Two C language keywords can be used to directly control loops in your pro grams. The keywords are break and continue. The break keyword should be familiar to you, having been introduced in Chapter 15 and tossed at you every now and again since then. The continue keyword is a new beast, but it plays an important role — one that you may even find handy.

236 Part III: Giving Your Programs the Ability to Run Amok
What continue does is to instantly repeat a loop. It works like break in that the rest of the statements in the loop are skipped; unlike break, the continue command sends the computer back to the beginning of the loop. The fool!
Both break and continue are C language keywords and statements unto themselves. They each must end with a semicolon.
Both break and continue work inside any C loop.
Both break and continue cause horrid errors outside a loop.
Please continue. . . .
The following program is BORC.C, which isn’t named after the Muppet’s Swedish Chef. Instead, it’s an acronym for break or continue, the two key words this program fiddles with:
#include <stdio.h>
int main()
{
int x=0;
for(;;)
{
x++;
if(x<=5)
{
printf(“%d, “,x); continue;
}
printf(“%d is greater than 5!\n”,x); break;
}
return(0);
}
Type the source code for BORC.C into your editor. Save it to disk as BORC.C, which stands for break or continue. (If you keep thinking of Bork-Bork-Bork, you save it to disk as BORK.C, which isn’t what I’m after.)
Compile and run the program.
Here’s how it goes:
1, 2, 3, 4, 5, 6 is greater than 5!

Chapter 18: Do C While You Sleep 237
The BORC.C program contains an endless for loop. Furthermore, the loop is cut short by the continue statement. After the value of x grows to be more than 5, the continue is skipped and, finally, a break statement stops the end less loop.
The for(;;) part indicates an endless loop. I read it as “for ever.”
Notice the space after the %d and comma in the printf() statement.
The continue statement causes the rest of the loop — the printf() and break — to be skipped, and then the loop is repeated. The increasing value of x proves that the loop continues to spin.
An else condition doesn’t have to be present to complement the if in Line 10. That’s because continue halts the program right then and there! If no continue were present, the break in Line 16 would ensure that the loop ran through only once.
The continue keyword
Like the break keyword, the continue keyword is used to affect how a loop loops. This time, the job is to immediately repeat the loop, skipping over the remaining statements and starting the loop over with the first line (the for, while, or do or whatever started the loop in the first place):
continue;
The continue keyword is a C language statement unto itself and must end properly with a semicolon.
The continue command comes in handy when you have statements in a loop that you don’t want to repeat every time; continue just skips over them and starts the loop over at the beginning.
The continue command works in any loop, not just in while loops.
Like break, continue affects only the loop it’s in.
The continue keyword forces the computer to instantly repeat a loop. The rest of the statements in the loop are just skipped, and the loop starts all over.
Note that continue repeats only the loop it’s in. If you have a nested loop, keep in mind that continue affects only one loop at a time. Also, continue cannot repeat a while loop when the loop’s condition becomes false.

238 Part III: Giving Your Programs the Ability to Run Amok
Keep in mind that although continue forces another spin of the loop’s wheel, it doesn’t reinitialize the loop. It tells the compiler to “go again,” not to “start over.”
You should keep in mind only two real warnings about the continue command: Don’t use it outside a loop or expect it to work on nested loops; and be careful where you put it in a while loop, lest you skip over the loop’s counter and accidentally create an endless loop.
As a final, consoling point, this command is rarely used. In fact, many C programmers may be a little fuzzy on what it does or may not know pre cisely how to use it.