Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C preprocessor
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Macro string replacement === ;Object-like A macro specifies how to replace text in the source code with other text. An ''object-like'' macro defines a token that the preprocessor replaces with other text. It does not include parameter syntax and therefore cannot support parameterization. The following macro definition associates the text "1 / 12" with the token "VALUE": <syntaxhighlight lang="cpp"> #define VALUE 1 / 12 </syntaxhighlight> ;Function-like A ''function-like'' macro supports parameters; although the parameter list can be empty. The following macro definition associates the expression "(A + B)" with the token "ADD" that has parameters "A" and "B". <syntaxhighlight lang="cpp"> #define ADD(A, B) (A + B) </syntaxhighlight> A function-like macro declaration cannot have whitespace between the token and the first, opening parenthesis. If whitespace is present, the macro is interpreted as object-like with everything starting at the first parenthesis included in the replacement text. ;Expansion The preprocessor replaces each token of the code that matches a macro token with the associated replacement text in what is known as ''macro expansion''. Note that text of string literals and comments is not parsed as tokens and is therefore ignored for macro expansion. For a function-like macro, the macro parameters are also replaced with the values specified in the macro reference. For example, <code>ADD(VALUE, 2)</code> expands to <code>1 / 12 + 2</code>. ;Variadic A [[variadic macro]] (introduced with [[C99]]) accepts a varying number of arguments which is particularly useful when wrapping functions that accept a variable number of parameters, such as <code>[[printf]]</code>. ;Order of expansion ''Function-like'' macro expansion occurs in the following stages: # Stringification operations are replaced with the textual representation of their argument's replacement list (without performing expansion). # Parameters are replaced with their replacement list (without performing expansion). # Concatenation operations are replaced with the concatenated result of the two operands (without expanding the resulting token). # Tokens originating from parameters are expanded. # The resulting tokens are expanded as normal. This may produce surprising results: <syntaxhighlight lang="cpp"> #define HE HI #define LLO _THERE #define HELLO "HI THERE" #define CAT(a,b) a##b #define XCAT(a,b) CAT(a,b) #define CALL(fn) fn(HE,LLO) CAT(HE, LLO) // "HI THERE", because concatenation occurs before normal expansion XCAT(HE, LLO) // HI_THERE, because the tokens originating from parameters ("HE" and "LLO") are expanded first CALL(CAT) // "HI THERE", because this evaluates to CAT(a,b) </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)