#define (C++)

From RAD Studio
Jump to: navigation, search

Go Up to Defining And Undefining Macros Index


#define macro_identifier <token_sequence>
#define macro_identifier(<arg_list>) <token_sequence>


The #define directive defines a macro. Macros provide a mechanism for token replacement with or without a set of formal, function-like parameters.

Each occurrence of macro_identifier in your source code following this control line will be replaced in the original position with the possibly empty token_sequence (there are some exceptions, which are noted later). Such replacements are known as macro expansions. The token_sequence is sometimes called the body of the macro.

An empty token_sequence results in the removal of each affected macro_identifier from the source code.

After each individual macro expansion, a further scan is made of the newly expanded text. This allows for the possibility of nested macros: The expanded text can contain macro identifiers that are subject to replacement. However, if the macro expands into what looks like a preprocessing directive, the directive will not be recognized by the preprocessor. There are these restrictions to macro expansion:

  • Any occurrences of the macro_identifier found within literal strings, character constants, or comments in the source code are not expanded.
  • A macro will not be expanded during its own expansion (so #define A A will not expand indefinitely).


#define HI "Have a nice day!"
#define empty
#define NIL ""
#define GETSTD #include <stdio.h>

The second syntax form allows the creation of macros with arguments or function-like macros.

See Also