Documente Academic
Documente Profesional
Documente Cultură
If the name of the macro being defined occurs in token-string (even as a result of another macro
expansion), it is not expanded.
A second #define for a macro with the same name generates a warning unless the second token
sequence is identical to the first.
Microsoft Specific
Microsoft C/C++ lets you redefine a macro if the new definition is syntactically identical to the
original definition. In other words, the two definitions can have different parameter names. This
behavior differs from ANSI C, which requires that the two definitions be lexically identical.
For example, the following two macros are identical except for the parameter names. ANSI C
does not allow such a redefinition, but Microsoft C/C++ compiles it without error.
#define multiply( f1, f2 ) ( f1 * f2 )
#define multiply( a1, a2 ) ( a1 * a2 )
On the other hand, the following two macros are not identical and will generate a warning in
Microsoft C/C++.
#define multiply( f1, f2 ) ( f1 * f2 )
#define multiply( a1, a2 ) ( b1 * b2 )
END Microsoft Specific
This example illustrates the #define directive:
#define WIDTH
80
#define LENGTH
( WIDTH + 10 )
The first statement defines the identifier WIDTH as the integer constant 80 and defines LENGTH
in terms of WIDTH and the integer constant 10. Each occurrence of LENGTH is replaced by
(WIDTH + 10). In turn, each occurrence of WIDTH + 10 is replaced by the expression (80 + 10).
The parentheses around WIDTH + 10 are important because they control the interpretation in
statements such as the following:
var = LENGTH * 20;
After the preprocessing stage the statement becomes:
var = ( 80 + 10 ) * 20;
which evaluates to 1800. Without parentheses, the result is:
var = 80 + 10 * 20;
which evaluates to 280.
Microsoft Specific
Defining macros and constants with the /D compiler option has the same effect as using a #define
preprocessing directive at the start of your file. Up to 30 macros can be defined by using the /D
option.
END Microsoft Specific