Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C For Dummies 2nd Ed 2004.pdf
Скачиваний:
43
Добавлен:
17.08.2013
Размер:
8.34 Mб
Скачать

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.