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

258 Part IV: C Level
void main()
{
printf("He calls me on the phone with nothing say\n");
•printf("Not once, or twice, but three times a day!\n");
{jerk();printf("Bill is a jerk!\n");
•printf("He insulted my wife, my cat, my mother\n");
•printf("He irritates and grates, like no other!\n");
{jerk();printf("Bill is a jerk!\n");
•printf("He chuckles it off, his big belly a-heavin'\n");
•printf("But he won't be laughing when I get even!\n");
Figure 20-1: |
|
{ |
jerk(); |
|
How a |
|
|
||
|
|
|
|
printf("Bill is a jerk!\n"); |
function |
} |
|
|
|
|
|
|||
works in a |
|
|
||
• |
|
|||
program. |
|
|||
• |
|
|||
|
|
|
||
|
|
|
|
|
|
|
|
|
|
The computer still reads instructions in the source code from the top down in the main function. However, when the computer sees another function, such as jerk(), it temporarily sidesteps to run the instructions in that function. Then, it returns back to where it was.
Keep in mind that not all functions are as simplistic as jerk(). Most of them contain many lines of code — stuff that would be too complex and redundant to use all over the place in a program.
Prototyping Your Functions
Prototyping refers to the art of telling the compiler what’s demanded of a func tion. It may seem like a silly thing to do, but it is in fact a good way to ensure that functions are used properly — plus it helps you keep track of your code. I know, it sounds a little like the compiler doesn’t trust you. But you probably don’t trust it much either, so the respect is mutual.
Proto comes from the Greek word for first.
Typing comes from the Latin word for “what you do on a keyboard.”

Chapter 20: Writing That First Function 259
Prototypical prototyping problems
You have to do two things to appease the prototyping gods. First, you have to properly configure the jerk function itself. Change Line 19 in the BIGJERK2.C source code to read:
void jerk()
This line tells the compiler that the jerk() function returns no values. That takes care of any function should return a value type of errors. (I cover functions that return values in Chapter 22; functions that don’t return a value are void. So there.)
Second, you have to tell the compiler about the jerk() function way early in the program. Essentially, that’s what prototyping is all about: You tell the com piler, “Hello? There’s a jerk() function later in this program, and here’s what you should expect to see.” You do this by sticking a line up top that looks like the start of the jerk function — but ends with a semicolon:
void jerk(void);
Stick this line between the #include <stdio.h> and the int main() that starts the main() function. The first part of your program looks like this:
#include <stdio.h>
void jerk(void);
int main()
The void jerk(void); line is the prototype. It tells the compiler to expect a jerk() function later on in the source code. It also says that the function will be of the void type, and won’t return any values. Furthermore, the jerk() function doesn’t require any values, which is why a void is in its parentheses. It’s heavy-duty void material. Just follow along if you don’t understand it.
Make the editing changes per the preceding instructions. A final rendition of the BIGJERK2.C program is shown here:
#include <stdio.h>
void jerk(void);
int main()
{
printf(“He calls me on the phone with nothing say\n”);

260 Part IV: C Level
printf(“Not once, or twice, but three times a day!\n”); jerk();
printf(“He insulted my wife, my cat, my mother\n”); printf(“He irritates and grates, like no other!\n”); jerk();
printf(“He chuckles it off, his big belly a-heavin’\n”); printf(“But he won’t be laughing when I get even!\n”); jerk();
return(0);
}
/* This is the jerk() function */
void jerk()
{
printf(“Bill is a jerk\n”);
}
When you’re done, resave BIGJERK2.C to disk. Recompile, and you shan’t be bothered by the various warning errors again.
The prototype is basically a rehash of a function that appears later in the program.
The prototype must shout out what type of function the program is and
describe what kind of stuff should be between the parentheses.
The prototype must also end with a semicolon. This is muy importanto.
I usually copy the first line of the function to the top of the program, paste it in there, and then add a semicolon. For example, in BIGJERK2.C, I copied Line 21 (the start of the jerk function) to the top of the source code and pasted it in, adding the necessary voids and semicolon.
No, the main() function doesn’t have to be prototyped. The compiler is expecting it and knows all about it. (Well, almost. . . .)
Required prototyping is something they added to the C language after it
was first introduced. You may encounter older C source code files that seem to lack any prototyping. Back in the days when such programs were written (before about 1990), this was a common way of doing things.
A sneaky way to avoid prototyping problems
Only the coolest of the C language gurus do this trick — so don’t tell anyone that you found out about it in a For Dummies book! Shhhh!

Chapter 20: Writing That First Function 261
Face it: Prototyping is a mess. Why repeat a function’s definition at the top of the program when it’s so obviously presented when the function is written? Seems redundant, eh? Many others think so, which is why they code their pro grams upside down. To wit, here’s another rendition of the BIGJERK program:
#include <stdio.h>
/* the jerk() function */
void jerk(void)
{
printf(“Bill is a jerk\n”);
}
/* Program starts here */
int main()
{
printf(“He calls me on the phone with nothing say\n”); printf(“Not once, or twice, but three times a day!\n”); jerk();
printf(“He insulted my wife, my cat, my mother\n”); printf(“He irritates and grates, like no other!\n”); jerk();
printf(“He chuckles it off, his big belly a-heavin’\n”); printf(“But he won’t be laughing when I get even!\n”); jerk();
return(0);
}
Edit the source code for BIGJERK2.C and make the changes in the preceding program. Basically, you’re removing the prototype for jerk() and replacing it with the jerk() function itself. Note that the jerk() function is defined as void jerk(void), just like a prototype, but it’s the function itself.
Save the changed source code to disk as BIGJERK3.C. Compile and run. The output is the same, but by turning the function upside down, you have utterly removed the possibility of prototyping errors.
The program still starts at the main() function despite stacking any other functions before it. Yes, the compiler is that smart.
You don’t have to code your programs this way. Rarely do programmers know in advance which functions they need, so most programmers start out coding functions the way it was done earlier in this chapter. Only after the function is written do they cut and paste it to the top of the file.
Don’t add the semicolon to the function’s declaration when you list your functions first! If you do, you get one of those nasty parse errors.