Saturday, March 17, 2012

#define macro


#define:

The #define directive defines an identifier or an identifier and a character sequence (i.e., a set of characters) that will be substituted for the identifier each time it is encountered in the source file. The identifier is referred to as a macro name and the replacement process as macro replacement. The general form of the directive is

#define macro-name
#define macro-name char-sequence

Either of the two form of declaration is applicable in C++.  In either case, the label can now be tested by the preprocessor to see if it has been defined:

#ifdef macro-name

This will yield a true result, and the code following the #ifdef will be included in the package sent to the compiler. This inclusion stops when the preprocessor encounters the statement

#endif

or

#endif // macro-name

Notice that there is no semicolon in this statement. There may be any number of spaces between the identifier and the character sequence, but once the character sequence begins, it is terminated only by a newline.

For example, if you wish to use the word UP for the value 1 and the word DOWN for the value 0, you could declare these two #define directives:

#define UP     1
#define DOWN   0

This causes the compiler to substitute a 1 or a 0 each time LEFT or RIGHT is encountered in your source file. For example, the following prints 0 1 2 on the screen:

printf("%d %d %d", DOWN, UP, UP+1);

Once a macro name has been defined, it may be used as part of the definition of other
macro names. For example, this code defines the values of ONE, TWO, and THREE:

#define ONE   1
#define TWO   ONE+ONE
#define THREE ONE+TWO

Macro substitution is simply the replacement of an identifier by the character sequence associated with it. Therefore, if you wish to define a standard error message, you might write something like this:

#define CCPLUSPLUS "www.ccplusplus.com\n"
/* 
 *  something
 */
printf(CCPLUSPLUS);

The compiler will actually substitute the string " www.ccplusplus.com\n" when the identifier CCPLUSPLUS is encountered. To the compiler, the printf() statement will actually appear to be

printf("www.ccplusplus.com\n");

No text substitutions occur if the identifier is within a quoted string. For example,

#define XYZ this is a test
printf("XYZ");

does not print this is a test, but rather XYZ.

If the character sequence is longer than one line, you may continue it on the next by placing a backslash at the end of the line, as shown here:

#define CCPLUSPLUS_LONG "ccplusplus.com is very long and \
Growing company"

C/C++ programmers commonly use uppercase letters for defined identifiers. This convention helps anyone reading the program know at a glance that a macro replacement will take place. Also, it is usually best to put all #defines at the start of the file or in a separate header file rather than sprinkling them throughout the program.

Macros are most frequently used to define names for "magic numbers" that occur in a program. For example, you may have a program that defines an array and has several routines that access that array. Instead of "hard-coding" the array's size with a constant, you can define the size using a #define statement and then use that macro name whenever the array size is needed. In this way, if you need to change the size of the array, you will only need to change the #define statement and then recompile your program. For example,

#define MAX_SIZE 100
/*
 * something
 */
float balance[MAX_SIZE];
/*
 * something
 */
for(i=0; i<MAX_SIZE; i++){
 printf("%f", balance[i]);
}
/*
 * something
 */
for(i=0; i<MAX_SIZE; i++) {
x =+ balance[i];
}

Since MAX_SIZE defines the size of the array balance, if the size of balance needs to be changed in the future, you need only change the definition of MAX_SIZE. All subsequent references to it will be automatically updated when you recompile your program.

No comments:

Post a Comment