Syntax
The #define directive can contain an object-like definition or a function-like definition. You can also use the define(macro_name, ...) compiler option to define macros on the command line. Macros defined on the command line override macros defined in the source code.
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;
This method of utilizing the #define directive is equivalent to defining and initializing COUNT=1000, using the const int type specifier:
const int COUNT=1000;
Therefore, 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
Function-Like Macros
A function-like macro definition consists of an
identifier 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.
A function-like macro invocation consists of an identifier 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. A parameter in the replacement code is replaced by the corresponding argument. Any macro invocations contained in the argument itself are completely replaced before the argument replaces its corresponding parameter in the replacement code.
The following example demonstrates a #define directive containing a function-like macro.
#define SUM(a, b) ( a + b )
This definition would cause the preprocessor to change the following statements:
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);
Always use parentheses to ensure correct evaluation of replacement text.
Once defined, a preprocessor identifier remains defined and in scope independent of the scoping rules of the language. The scope of a macro definition begins at the definition and does not end until a corresponding #undef directive is encountered. If no corresponding #undef directive is defined, the scope of the macro definition lasts until the end of the compilation unit.
Example of Preprocessor Directives
![]()
Preprocessor Directives - An
Overview
![]()
Scope of Macro Names - #undef
![]()
Resource Compiler -
#define
![]()
Resource Compiler - #undef
define Miscellaneous
Option - define(macro_name, ... )
