
- •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

106 Part II: Run and Scream from Variables and Math
Real, live constant variables
Using #define to create a constant for use throughout your program is a handy thing to do. In fact, I recommend using it whenever you have some thing in your program that you figure may change. For example:
#define NUMBER_OF_USERS 3 #define COLUMN_WIDTH 80 #define US_STATES 50
Each of these examples makes it possible to change aspects of your entire program merely by editing a single #define declaration. But are they really constants — the opposite of variables?
No! That’s because they have the const keyword, which converts a mildmannered variable into an unyielding constant. To wit:
const int senses = 6;
The preceding statement creates a variable named senses, but fixes that variable’s value at 6. The value cannot be changed or used for something else later in the program; if you try, a “read-only variable” error pops up.
Most often you don’t see const used to create constant values. Instead, this statement is quite common:
const char prompt[] = “Your command:”;
This statement creates the string variable prompt and sets its contents equal to Your command:. The const keyword ensures that this variable cannot be reused or its contents ever changed — a good idea for this type of variable.

Chapter 9
How to C Numbers
In This Chapter
Using different variables for different numbers
Understanding the long and short of int
Knowing your signed and unsigned variables
Floating a number
Double floating a number
Formatting a huge value
Put on your safety goggles, my greenhorn companion! I have danced around the flaming inferno of numbers far too long. It’s time to dive headlong into that hellfire of values and digits. Far down from the comfy safety of the int lie
numbers large and loathsome. Terrifying values you can gingerly place into your puny programs. Numbers lethal and toxic, but which you can also tame, as long as you obey my gentle advice in this chapter.
Fear not! Instead, don your asbestos suit and follow me. Watch your step.
There Are Numbers, and Then
There Are Numbers
Welcome to what will soon be one of many new, frustrating aspects of the C programming language. It’s known as the C Numeric Data Type Puzzle. Unlike in real life, where we can just pull any number out of the ethers and be joyously happy with it, in C you must pull numbers from specific parts of the ethers based on which type of number it is. This makes the frustration factor begin rising, with the logical question “What’s a number type?”

108 Part II: Run and Scream from Variables and Math
Okay. It isn’t a “number type.” It’s a numeric data type, which is how you say “number type” if you work at the Pentagon. You have to tell the C compiler which type of number you’re using because it thinks about numbers differently from the way humans do. For example, you have to know the following things about the number:
Is it a whole number — without a fraction or decimal part?
How big is the number (as in value-large, not big-on-the-page-large)?
If the number does have a fractional part, how precise must the number be? (Like to the thousandths, millionths, or gazillionths decimal place. Scientists have to know the precision when they send missiles to coun tries with opposing ideologies.)
I know that this stuff is all alien to you. What most programmers want to do is say “I need a number variable — just give me one, quick — before this value slips out the back of the computer and becomes a government statistic!” But you have to think a little more before you do that.
The most common numeric data type is the integer.
If you’re going to work with decimal numbers, such as a dollar amount, you need the floating-point number.
Keep reading.
Numbers in C
A number of different types of numbers are used in C — different numeric data types, so to speak. Table 9-1 lists them all, along with other statistical informa tion. Flag the table with a sticky note. This table is something you refer to now and again because only the truly insane would memorize it.
Table 9-1 |
C Numeric Data Types |
|
Keyword |
Variable Type |
Range |
char |
Character (or string) |
–128 to 127 |
|
|
|
int |
Integer |
–32,768 to 32,767 |
|
|
|
short |
Short integer |
–32,768 to 32,767 |
|
|
|
short int |
Short integer |
–32,768 to 32,767 |
|
|
|

|
|
|
Chapter 9: How to C Numbers |
109 |
|
|
|
|
|||
|
|
|
|
|
|
|
Keyword |
Variable Type |
Range |
|
|
|
long |
Long integer |
–2,147,483,648 to 2,147,483,647 |
|
|
|
|
|
|
|
|
|
unsigned char |
Unsigned character |
0 to 255 |
|
|
|
|
|
|
|
|
|
unsigned int |
Unsigned integer |
0 to 65,535 |
|
|
|
|
|
|
|
|
|
unsigned short |
Unsigned short integer |
0 to 65,535 |
|
|
|
|
|
|
|
|
|
unsigned long |
Unsigned long integer |
0 to 4,294,967,295 |
|
|
|
|
|
|
|
|
|
float |
Single-precision |
±3.4 ∞ 1038 to |
|
|
|
|
floating-point |
±3.4 ∞ 10–38 |
|
|
|
|
(accurate to 7 digits) |
|
|
|
|
double |
Double-precision |
±1.7 ∞ 10–308 to |
|
|
|
|
floating-point |
±1.7 ∞ 10308 |
|
(accurate to 15 digits)
The keyword is the C language keyword used to declare the variable type. If you have been reading the chapters in this book in order, you have used int, char, and float already.
The variable type tells you which type of variable the keyword defines. For example, char defines a character (or string) variable, and int defines integers. C has many variable types, each of which depends on the type of number or value being described.
The range tells you how big of a number fits into the variable type. For example, integers range from –32,768 up to 0 and up again to 32,767. Other types of variables handle larger values. This value may be differ ent on your compiler; use the values in Table 9-1 for reference only.
Table 9-1 isn’t that complex. In all, C has really only four types of variables:
char
int
float
double
The int can be modified with either short or long, and both char and int are modified with unsigned. The float and double variables are both floatingpoint, though the values held by double are larger.