- •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 28
Ten Tips for the Budding
Programmer
In This Chapter
Using the command history
Keeping your editor open in another window
Enjoying a color-coded editor
Knowing your editor’s line number commands
Keeping a command-prompt window open
Understanding a few commands
Naming your variables
Solving incrementing and decrementing riddles
Breaking out of a loop
Here are some of my top-notch suggestions for programmers just start ing out. Man, I wish I had had this list back in the steam-powered com
puter days, when I first started learning how to program.
Use the Command-Line History
Going back and forth between your editor and compiler at the command prompt gets tedious. Fortunately, most of the command-line shells out there (in both Unix and Windows) have a command-repeat button. Use it!
For example, if you press the up-arrow key, you can recall the preceding com mand line. Pressing the up-arrow key again recalls the command before that. If you find that you’re reediting a lot, use the up-arrow key to recall the com mand to start your editor, and ditto for the commands to recompile.
348 Part V: The Part of Tens
Keep Your Editor Open
in Another Window
The preceding tip can be null and void if you use your operating system’s graphical environment. Keep a command prompt open in one window, and keep your text editor open in another window (see Figure 28-1). As long as you can remember to save your source code, it’s perfectly fine to open a separate window for your editor and use it to edit your source code file. Then, in the other window, run a command prompt so that you can compile the source code as well as run the result.
Figure 28-1:
Switching between editor and commandprompt windows.
Switch between windows by clicking the mouse in one or the other. Or, use the Alt+Tab key to switch windows, which seems to work in most graphical environments.
The key is to remember to save the document in one window before compil ing in another window.
Use a Context-Colored Text Editor
This is why I recommend the Vim editor: As long as you put that .c on the end of the source code filename, Vim recognizes your C language code and
Chapter 28: Ten Tips for the Budding Programmer 349
uses different colors to present it to you on the screen. This feature is so useful that if you ever go back to a monochrome editor, you notice that it slows you down!
To activate the colors in Vim, type a colon, :, and then syntax enable, and press Enter.
When you’re running Vim in a Windows window, choose Syntax Automatic so that C language keywords are highlighted.
In Unix, to keep syntax enable activated, edit or create a file named
.vimrc in your home directory. Into that file, add or include the follow ing command:
:syntax enable
Then save the .vimrc file back to disk.
Another bonus to highlighted text is that you can easily spot missing quotes; text between quotes is color-coded, so if a quote is missing, the source code looks like blech.
Turn on auto-indenting if your editor has such a feature. Vim turns on auto-indenting when you use the syntax-enable command, or choose Syntax Automatic from the menu.
Know the Line-Number Commands
in Your Editor
The C language compiler reports errors in your source code and lists the lines on which the errors occur. If your text editor displays line numbers, you can easily locate the specific line containing the error and then fix the error.
In Windows Notepad, you can display the line and column number on the status bar. To do so, first ensure that Word Wrap is off (choose Format Word Wrap if necessary), and then choose View Status Bar. (Note that the Status Bar command may not be available in earlier ver sions of Notepad.)
Vim displays the cursor’s position on the bottom of the window, toward the right side. (The line number is followed by a comma and the column number, shown as 1,1 in Figure A-1 in Appendix A.)
In Vim, the command to go to a specific line is G. For example, if the compiler reports an error in Line 64, type 64G and VIM instantly jumps to Line 64. Think “Line number, Goto” to remember this trick.
350 Part V: The Part of Tens
Keep a Command Prompt Window Open If You’re Using the IDE
In Windows, most compilers come with an Integrated Development Environment (IDE). It’s a window that contains the editor, plus other gizmos for making Windows programs. It can be quite complex, but also very handy; like Vim, for example, the Dev-C++ IDE color-codes your C commands.
One thing most IDEs don’t do is show you the output of the console pro grams created in this book. Because of that, if you use an IDE, be sure to keep a console window handy, open, and logged to the prog\c\learn folder in Windows. That way, you can type the names of the programs you create and see their output in that window. Otherwise, the output isn’t visible from the IDE.
Know a Few Handy Command-Prompt
Commands
I strongly advise that you become familiar with using a command prompt. While there are hundreds of command prompt commands to type, with a bazillion options and more ways to make typos than typing with welder’s gloves on, pay heed to the few commands listed in Table 28-1.
Table 28-1 |
|
Command-Prompt Commands |
Windows/DOS |
Unix |
What It Does |
dir |
ls |
Lists all files in the current directory or folder. In |
|
|
Unix, the -l switch after ls is used to display more |
|
|
details about the files in the directory. |
|
|
|
dir *.c |
ls *.c |
Lists only the C language source-code files in a |
|
|
folder or directory. |
cls |
clear |
Clears the screen. |
|
|
|
ren |
mv |
Renames a file; ren or mv is followed by the file’s |
|
|
original name and then the new name, such as ren |
|
|
goodbye.c bye.c or mv goodbye.c bye.c, |
|
|
which renames the file goodbye.c to bye.c. |