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

110 Part II: Run and Scream from Variables and Math
Why use integers? Why not just make every number floating-point?
Obviously, if you have a double-precision floating-point number that can handle, essentially, numbers up to one gazillion, why bother declaring any variables as integers? Heck, make everything a double-whammy floating point and be done with it! Sounds good. Is bad.
Integers are truly the handiest and most common types of numeric variables. Oftentimes, you need only small, whole-number values when you’re program ming. Floating-point numbers are okay, but they require more overhead from the computer and take longer to work with. By comparison, integers are far quicker. For this reason, God saw fit to create integers (which He did on the third day, by the way).
Integer types (short, long, wide, fat, and so on)
You have to concern yourself with only two types of integers: the normal integer — the int — and the long integer — the long. (The signed and unsigned aspects are chewed over slowly later in this chapter.)
The int (rhymes with “bent”) is a whole-number value, normally ranging from –32,768 to 32,767. It’s ideally put to use for small numbers without a fractional part. In some versions of C, you may see this value referred to as a short or short int.
The long is a whole-number value, ranging from –2,147,483,648 to 2,147,483,647 — a big range, but not big enough to encompass the national debt or Madonna’s ego. This type of numeric variable is referred to as a long, or long int in some versions of C.
You use the int and long keywords to declare integer variables. int is for smaller values; long is for larger values.
The %d placeholder is used in the printf() function to display int vari ables. (You can also use the %i placeholder; refer to Table 24-2 in Chap ter 24.)
int = short = short int
long = long int
Integer variables (int) are shorter, faster, and easier for the computer to deal with. If Soup for One were a variable, it would be an int. Use an int whenever you need a small, whole numeric value.

Chapter 9: How to C Numbers 111
In some C compilers, the ranges for int and long int are the same. That’s because the compiler (usually a 32-bit model) can more efficiently handle long values than it can handle smaller int values. It’s merely technical junk; don’t memorize it or let it otherwise ruin your day.
Negative numbers — why bother? Sometimes, you need them, but most of the time you don’t. See the next section.
The char variable type can also be used as a type of integer, though it has an extremely small range. These variables are used mostly to store single characters (or strings), which is discussed somewhere else. (Give me a second to look.) Oh, it’s in Chapter 10.
Signed or unsigned, or “Would you like a minus sign with that, Sir?”
I have this thing against negative numbers. They’re good only when you play Hearts. Even so, that’s justification because you may someday write a program that plays Hearts on the computer, in which case you will be in dire need of negative numbers (because you can write the program so that you always win).
When you declare a numeric variable in C, you have two choices: signed and unsigned. Everything is signed unless you specifically type unsigned before the variable type:
unsigned int shoot_the_moon = 26;
A signed type of number means that a variable can hold a negative value. The standard int variable can hold values from –32,768 up to 32,767. That’s half negative numbers, from –32,786 to –1, and then half positive numbers, from 0 up to 32,767. (Zero is considered positive in some cults.)
An unsigned number means that the variable holds only positive values. This unsigned number moves the number range all up to the positive side — no negatives (the C language equivalent of Prozac). Your typical unsigned int has a range from 0 to 65,535. Negative numbers aren’t allowed.
The int variable elephants holds the value 40,000. Try that with a signed int! Ha!
unsigned int elephants 40000;
Table 9-2 illustrates the differences between the variable types as far as the values they can hold are concerned.

112 Part II: Run and Scream from Variables and Math
The whole painful spiel on why we have signed integers
The signed-unsigned business all has to do with how numbers are stored inside a computer. The secret is that everything, no matter how it looks on the screen or in your program, is stored in the binary tongue inside the computer. That’s counting in base 2 (ones and zeroes).
Binary numbers are composed of bits, or binary digits. Suppose that your C language compiler uses two bytes to store an integer. Those two bytes contain 16 binary digits, or bits. (Eight bits are in a byte.) For example:
0111 0010 1100 0100
This value is written as 29,380 in decimal (the human counting system). In binary, the ones and zeroes represent various multiples of two, which can get quite complex before your eyes, but is like eating ice cream to the computer.
Look at this number:
0111 1111 1111 1111
It’s the value 32,767 — almost a solid bank of ones. If you add 1 to this value, you get the fol lowing amazing figure:
1000 0000 0000 0000
How the computer interprets this binary value depends on how you define your variable. For a signed value, a 1 in the far left position of the number isn’t a 1 at all. It’s a minus sign. The pre ceding number becomes –32,768 in binary math. If the variable is an unsigned value, it’s inter preted as positive 32,768.
The deal with signed and unsigned numbers all depends on that pesky first bit in the computer’s binary counting tongue. If you’re working with a signed number, the first bit is the minus sign. Otherwise, the first bit is just another droll bit in the computer, happy to be a value and not a minus sign.
Table 9-2 What Signed and Unsigned Variables Can Hold
Signed |
Range |
Unsigned |
Range |
char |
–128 to 127 |
unsigned char |
0 to 255 |
|
|
|
|
int |
–32768 to 32,767 |
unsigned int |
0 to 65,535 |
|
|
|
|
long |
–2,147,483,648 |
unsigned long |
0 to 4,294,967,295 to 2,147,483,647 |
|
|
|
|
Floating-point numbers (numbers with a decimal part or fractions) can be positive or negative without regard to any signed or unsigned nonsense.
Floating-point numbers are covered in the following section.
Normally, the differences between signed and unsigned values shouldn’t bother you.