- •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
Appendix A
The Stuff You Need to Know before You Read All the Other Stuff in This Book
In This Appendix
Configuring your computer as a programming workstation
Selecting a compiler
Choosing an editor
Creating programs
You need a few things before C programming is possible on your computer. The purpose of this appendix is to outline what you need and how to use
it in order to work with the sample programs in this book. It’s not that hard, but it may be something you’re not used to, so pay attention!
Setting Things Up
You need two things to program in C on your computer:
A compiler
A place to put your programs
For Linux, Unix, and the Mac OS X operating system, your C language compiler is already included; it comes with the operating system. For Windows and older Mac systems, you must obtain a compiler. That’s not as difficult as it sounds.
360 C For Dummies, 2nd Edition
The C language compiler
Thanks to the C language’s popularity, many compilers are available for you to use with this book. I do, however, recommend the following:
Windows: If you’re using Windows, I recommend that you get a GCC-compatible C compiler. A list of compilers is provided on this book’s Web page, at www. c-for-dummies.com.
For this book, I used the MinGW compiler, which comes with the Dev-C++
IDE (Integrated Development Environment). It’s free and available from
www.bloodshed.net.
Whichever compiler you use, note its location on your PC’s hard drive. You have to use this location to create a batch file or modify your system’s path so that you can access the compiler from any folder in your disk system.
More on that later.
Other compilers are out there, including the best-selling Microsoft Visual C++ (MSVC). If you have MSVC, fine; you should be okay with running the programs in this book. Note, however, that I’m not familiar with the current version of MSVC and don’t refer to it in this book, nor can I answer questions about it via e-mail. If you don’t have MSVC, you have no reason to buy it.
Plenty of free, shareware, and open-source C compilers are available on the Internet.
If you have other books on the C language, check in the back of the book
for a free compiler.
Any GCCor GNU-compatible C compiler works best with this book.
Linux, FreeBSD, or Mac OS X: If you’re using any of these variations of Unix, you should already have the GCC compiler installed and ready to use. To confirm, open a terminal window and type the following line at the command prompt:
gcc -v
The version number of GCC and other information is displayed on the screen. If you get a Command not found error, GCC isn’t installed; you have to update your operating system to include GCC as well as all the C programming librar ies and other materials. (You can generally do that through your operating system’s setup or configuration program; it doesn’t typically require that the entire operating system be reinstalled.)
Appendix A: The Stuff You Need to Know before You Read All the Other Stuff 361
Unix: If you have a “real” version of Unix, the command is cc and not gcc. In fact, you may notice that cc even works on other Unix-like operating systems, where the cc command is often linked to the GCC compiler, for compatibility’s sake.
Mac (before OS X): Older versions of the Mac lack a built-in C language com piler. I recommend the Code Warrior compiler, though you should also check the Apple Web site to see whether any other (free) compilers are available: http://developer.apple.com/.
The place to put your stuff
When you learn to program, you create scads of files. These files include the original-text source code files, the final program files, and perhaps even object code files, depending on the compiler. Obviously, you want to keep those files organized and separate from your regular junk.
For this book, I recommend creating a prog folder or directory. Create this folder off your main folder — the $HOME folder in Unix or the My Documents folder in Windows. The prog folder is designed to hold all your programming projects.
Beneath prog, you should put the c folder, for all your C language program ming projects.
Finally, create a learn folder, in which you put all the projects for this book. The rest of this appendix provides specific examples.
Windows. To create a folder for your C language projects, follow these steps:
1.Open the My Documents icon on the desktop.
2.Choose File New Folder to create a new folder and then name the folder prog.
3.Open the prog folder.
4.Choose File New Folder to create a new folder, and then name it c.
5.Open the c folder.
6.Create a folder inside the c folder, and name that folder learn.
7.Close the c folder window.
362 C For Dummies, 2nd Edition
The learn folder is where you place all the files created in this book.
Linux, FreeBSD, Mac OS X, or Unix: To create a folder for your C program ming projects, obey these steps:
1.If you’re using a graphical shell, open a terminal window. You need to get at the command prompt.
The terminal window should open into your account’s home directory. If you aren’t in your home directory, type the cd command to return there.
Ensure that you’re not logged in as the root account; creating programs as the root user is a security risk.
2. Create the prog/c/learn directory branch:
mkdir -p prog/c/learn
The -p switch directs mkdir to create all the subdirectories that are speci fied; it’s the same as issuing three separate mkdir commands at once. With one command, you have created the prog directory, the c subdirec tory, and, finally, the learn subdirectory. Ta-da.
You use the learn folder for storing all the source code and program files created in this book.
Mac (before OS X). Alas, the old Mac operating system lacked a “home folder” for all your stuff. If you have such a folder, use it as a base to create the subfolders in the following steps. Otherwise, you can create these folders right on the desktop for handy access:
1.Press Ú+N to create a new folder.
2.Name that folder prog, for “programming.”
3.Open the prog folder.
4.Press Ú+N to create a subfolder inside the prog folder.
5.Name that folder c.
6.Open the c folder.
7.Press Ú+N to create a subfolder inside the c folder.
8.Name that subfolder learn.
9. Close all the open windows you just created.
When using your compiler, remember to save all your files in the learn folder.
