
- •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 15: C You Again 197
Breaking out of a loop
Loops aren’t infinite if you have a way to break out of them. For most loops, that escape clause is provided in the looping statement itself. In the for state ment, the escape condition is the middle item, as in
for(a=32;a<128;a=a+1)
The escape clause is a<128, which is the condition on which the loop ends.
Some loops, however, are designed without an end. That’s because the condi tion that ends the loop happens elsewhere. In that case, the loop is designed to be eternal — which is fine, as long as some condition elsewhere eventually breaks the loop.
As an example, most word processors work with an infinite loop hidden inside their programs. The loop sits and scans the keyboard, over and over, waiting for you to type a command. Only when you type the proper “I want to quit” command does the thing stop and you return to DOS. It’s like a controlled infi nite loop — it’s not really infinite because you have a way out.
The following program is TYPER1.C, a first stab at a word processor, though you can do nothing in the program except type and see what you type on the screen. The program is set up with an on-purpose infinite for loop. The break keyword is then used with the if command to bust out of the loop when you press the ~ (tilde) key:
#include <stdio.h>
int main()
{
char ch;
puts(“Start typing”);
puts(“Press ~ then Enter to stop”);
for(;;)
{
ch=getchar();
if(ch==’~’)
{
break;
}
}
printf(“Thanks!\n”);
return(0);
}

198 Part III: Giving Your Programs the Ability to Run Amok
This program wins the award for having the most indentation and curly braces to this point in this book. Be careful as you type it into your editor. Save the file to disk as TYPER1.C.
Compile TYPER1.C.
Run the resulting program, which works somewhat like a typewriter. You can type away and fill the screen with text.
When you’re done typing, press the tilde (~) key and then Enter. (This is
TYPER’s only “command.”) You’re done.
The for(;;) statement doesn’t error. The reason is that the semicolons are required, but what’s between them isn’t (a peculiarity of the for keyword).
I read the for(;;) command aloud as “for ever.”
The for loop in TYPER1.C is infinite because its “while true” condition is missing. Gone! The compiler therefore assumes that the condition is true all the time and that the program loops infinitely.
The first and last parts of the for statement’s items aren’t included either, though the semicolons inside the parentheses are still required in order to meet the demands of C etiquette.
Because several statements belong to the for loop, they’re enclosed in curly braces.
The getchar function waits for a key to be pressed on the keyboard and displays that character. The character is then saved in the ch variable.
The if statement tests the ch variable to see whether it’s equal to the
~ (tilde) character. Note that two equal signs are used. If the comparison is true, indicating that the user pressed the ~ key, the break command is executed. Otherwise, break is skipped and the loop is repeated, reading another character from the keyboard.
The break keyword
The break keyword gets you out of a loop — any loop in C, not just a for loop. No matter what the loop’s ending condition is, break immediately issues a “parachute out of this plane” command. The program continues with the next statement after the loop:
break;

Chapter 15: C You Again 199
The break keyword is a C language statement unto itself and must end prop erly with a semicolon.
The loop that break breaks you out of doesn’t have to be an endless loop. break can stop any loop, at any time. It’s often used with an if statement to test some condition. Based on the results of the test, the loop is stopped by using break (just as was done with TYPER1.C in the preceding section).
break stops only the loop it’s in. It doesn’t break out of a nested loop, or a loop within a loop. See Chapter 18 for more information on nested loops.
If you use break outside of a loop — when there’s nothing to break out of — the compiler goes berserk and generates an error.
It’s funny how the word is break and not brake. The same twisted logic applies to the Break key on your keyboard.

200 Part III: Giving Your Programs the Ability to Run Amok