#define Preprocessor Directive

A preprocessor define directive directs the preprocessor to replace all subsequent occurrences of a macro with specified replacement tokens.

The #define directive can contain an object-like definition or a function-like definition

Object-Like Macros An object-like macro definition replaces a single identifier with the specified replacement tokens. The following object-like definition causes the preprocessor to replace all subsequent instances of the identifier COUNT with the constant 1000:
#define COUNT 1000

If the statement

int arry[COUNT];

appears after this definition and in the same file as the definition, the preprocessor would change the statement to

int arry[1000];

in the output of the preprocessor.

Other definitions can make reference to the identifier COUNT:

#define MAX_COUNT COUNT + 100

The preprocessor replaces each subsequent occurrence of MAX_COUNT with COUNT + 100, which the preprocessor then replaces with 1000 + 100.

If a number that is partially built by a macro expansion is produced, the preprocessor does not consider the result to be a single value. For example, the following will not result in the value 10.2 but in a syntax error.

#define a 10
a.2

Using the following also results in a syntax error:

#define a 10
#define b a.11

Identifiers that are partially built from a macro expansion may not be produced. Therefore, the following example contains two identifiers and results in a syntax error:

#define d efg
abcd
Function-Like Macros To define a function-like macro, specify an identifier name followed by a parenthesized parameter list in parenthesis and the replacement tokens. The parameters are imbedded in the replacement code. White space cannot separate the identifier (which is the name of the macro) and the left parenthesis of the parameter list. A comma must separate each parameter. For portability, you should not have more than 31 parameters for a macro.

Use function-like macros in your program as follows. In the body of your program source, insert a defined function-like macro name followed by a list of arguments in parentheses. A comma must separate each argument. Once the preprocessor identifies a function-like macro invocation, argument substitution takes place. Parameters in the replacement code are replaced by the corresponding arguments. Any macro invocations contained in an argument itself are completely replaced before the argument replaces its corresponding parameter in the replacement code.

Examples of Usage
The following line defines the macro SUM as having two parameters a and b and the replacement tokens (a + b):

#define SUM(a,b) (a + b)

This definition causes the preprocessor to change the following statements (if the statements appear after the previous definition):

c = SUM(x,y);
c = d * SUM(x,y);

In the output of the preprocessor, these statements would appear as:

c = (x + y);
c = d * (x + y);

Use parentheses to ensure correct evaluation of replacement text. For example, the definition:

#define SQR(c)  ((c) * (c))

requires parentheses around each parameter c in the definition in order to correctly evaluate an expression like:

y = SQR(a + b);

The preprocessor expands this statement to:

y = ((a + b) * (a + b));

Without parentheses in the definition, the correct order of evaluation is not preserved, and the preprocessor output is:

y = (a + b * a + b);
Notes:
  1. Arguments of the # and ## operators are converted before replacement of parameters in a function-like macro.
  2. The number of arguments in a mcro invocation must be the same as the number of parameters in the corresponding macro definition.
  3. Commas in the macro invocation argument list do not act as argument separators when they are:
    • in character constants
    • in string literals
    • surrounded by parenthesis
  4. The scope of a macro definition begins at the definition and does not end until a corresponding #undef directive is encountered. If there is no corresponding #undef directive, the scope of the macro lasts until the end of the compilation is reached.
  5. A recursive macro is not fully expanded. For example, the definition
        #define x(a,b) x(a+1,b+1) + 4

    would expand

        x(20,10)

    to

        x(20+1,10+1) + 4

    rather than trying to expand the macro x over and over within itself.

  6. A definition is not required to specify replacement tokens. The following definition removes all instances of the token debug from subsequent lines in the current file:
        #define debug
  7. You can change the definition of a defined identifer or macro with a second preprocessor #define directive only if the second preprocessor #define statement is preceded by a preprocessor #undef directive. The #undef directive nullifies the first definition so that the same identifier can be used in a redefinition.
  8. Within the text of the program, the preprocessor does not scan character constants or string constants for macro invocations.

 



Preprocessor Macros
Preprocessor Directives


#undef Preprocessor Directive
Preprocessor Macro Operators
List of Preprocessor Directives