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

302 Part IV: C Level

What the #defines Are Up To

The #define directive is another one of those things the compiler eats before it works on your source code. Using #define, you can set up some handy, clever, and memorable word equivalents to big numbers, constant values, or just about anything that appears in your C source code.

The full scope of the #define “construction” is offered in Chapter 8. Because I’m on an anti-redundancy campaign, I don’t repeat it here. Let me add, how­ ever, that #define is the most popular item placed into a header file. Edit your HEAD.H file, in fact, and add these two lines:

#define TRUE 1

#define FALSE (!TRUE)

Save HEAD.H back to disk with these modifications.

What you have done is to create a shortcut word, TRUE, that you can use as a “true” condition.

The FALSE shortcut word is defined as being “not” true. The exclamation point in C means not. The value of FALSE is obviously !TRUE. (It works out to be 0; no big deal.)

To make immediate use of the two new shortcut words, modify the source code for HTEST.C:

#include <stdio.h> #include “head.h”

int main() SPIT

if(TRUE)

BLECH(“Must be true!\n”); if(FALSE)

BLECH(“Never see this message.\n”); return(0);

SPOT

Carefully type this source code into your editor. Save it to disk. Compile. Run.

The output looks something like this:

Must be true!

Chapter 23: The Stuff That Comes First 303

The best time for using the TRUE or FALSE shortcut words is when you create a while loop. Refer to Chapters 17 and 18.

In the C language, the value 1 is taken to be TRUE, and 0 is FALSE.

Some C programmers prefer to define TRUE and FALSE this way:

#define FALSE 0

#define TRUE(!FALSE)

Whatever. This book defines TRUE as 1 and FALSE as “not true,” which works out to 0. If you see the preceding #defines in a program, every­ thing still works as advertised. Just don’t let it throw you.

Some compilers may issue a warning message when no comparison is in the if statement’s parentheses. That’s just the compiler being smart again; it recognizes the certainty of the if keyword’s “comparison” and tells you whether it is always TRUE or is always FALSE.

Some compilers may also point out that the second if statements are never executed because the if condition is always false. Big deal. Run the program anyway.

Avoiding the Topic of Macros

Another # thing used in the C language is the macro. Like #include and #define, macros are instructions to the compiler that tell it how to handle conditions and whether specific parts of the source code are to be used or skipped. Weird, but handy.

Though there’s no reason to describe in detail what macros are and because I’m not crazy enough to waste time with an example, you may see even more pound-sign goobers playing the macro game in someone else’s C source code. Pick any from this list:

#if

#else

#endif

#ifdef

#ifndef

These are also instructions to the C compiler, designed primarily to indicate whether a group of statements should be compiled or included with the final program.

304 Part IV: C Level

For example:

#if GRAPHICS

//do graphical wonders here

#else

//do boring text stuff here #endif

Suppose that GRAPHICS is something defined earlier in the program or in a header file — #define GRAPHICS 1, for example. If so, the compiler com­ piles the statements only between the #if GRAPHICS and #else lines.

Otherwise, the statements between #else and #endif are compiled. The result? Potentially two different programs using one source code.

Crazy! Why bother?

I have used this trick only once: I wrote a program that had two versions — one that ran on old color PC systems and a second that ran on monochrome systems. On the color PCs, I had statements that displayed text in color, which didn’t work on the old monochrome monitors. Therefore, I used the #if goober so that I needed to write only one source code file to create both versions of the program.

There’s really no need to bother with this stuff. I’m mentioning it here because it fits in the section heading and because some C poohbah somewhere would chastise me for ignoring it.

The #if thing is followed by a shortcut word defined elsewhere in the

program with the #define construction. If it’s value isn’t 0, the #if con­ dition is true and the statements between #if and #endif are compiled. Oh, and there’s an #else in there too, which serves the same purpose as the C language else word.

You may see some of the #ifdef or #ifndef things in various header files. I have no idea what they’re doing in there, but they look darn impressive.

Another #-goober is #line. It’s rarely used. Supposedly, what it does is force the compiler to report a unique line number just in case an error occurs. (Don’t worry — I don’t get it either.)

Chapter 24

The printf() Chapter

In This Chapter

Using printf() to display text

Displaying forbidden characters with escape sequences

Displaying the values of variables

Understanding conversion characters

Perhaps one of the most familiar fellows you have met on your C pro­ gramming journey is the printf() function. Though you may have been

using it, and despite my various and sundry hints about some of its power, I don’t feel that you have really been formally introduced. Now, after all that time, this chapter is your formal introduction to the full power of printf().

A Quick Review of printf()

You should know three printf() tricks:

printf() displays the text you put inside the double quotes.

printf() requires the backslash character — an escape sequence — to display some special characters.

printf() can display variables by using the % conversion character.

The % conversion characters can also be used to format the output of the information that’s printed.