Name
#define directive — Defines a macro
Synopsis
#defineidentifier
definition
#defineidentifier
( )definition
#defineidentifier
( )definition
#defineidentifier
(identifier-list
)definition
The #define
directive defines
a macro named identifier
. The macro’s
replacement text is the list of tokens shown as
definition
. The macro can be simple, with
no arguments, or can have an argument list. The argument list is
introduced by a left parenthesis that immediately follows the macro
name. If there is a space between
identifier
and (
, the (
is interpreted as the start of the definition of a simple macro. The
identifier-list
can be empty, or it can be
a list of identifiers separated by commas. Whitespace is permitted in
the identifier-list
and before the closing
parenthesis.
C programmers are accustomed to using macros to declare
constants and simple inline functions, but C++ offers const
declarations, true inline functions,
and templates. Macros are therefore used much less often in C++ than
in C. The main drawback to macros is that they do not obey scope or namespace
rules. When you must use macros, a common convention is to use all
uppercase letters for the macro names, and never use all uppercase
letters for non-macro names.
A macro’s scope is from the point of definition to the end
of the source file, or until you undefine the macro with the #undef
directive. If you try to repeat a macro definition, the new definition must be identical to the original definition. The only way to give ...
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.