Contents
How are the arguments to a macro expanded?
All arguments to a macro are completely macro-expanded before they are substituted into the macro body. After substitution, the complete text is scanned again for macros to expand, including the arguments. This rule may seem strange, but it is carefully designed so you need not worry about whether any function call is actually a macro invocation.
Can a function like a macro take arguments?
Function-like macros can take arguments, just like true functions. To define a macro that uses arguments, you insert parameters between the pair of parentheses in the macro definition that make the macro function-like. The parameters must be valid C identifiers, separated by commas and optionally whitespace.
Is there way to see expanded C macros after preprocessor?
Depending on which compiler you use, there should be a way to see the code after the preprocessor (which does the macro expansion, macros are not known by the compiler at all) is done. With gcc, the option is -E. Here’s a simplified example, using toy code and not the actual GTK+ macro:
When do you change the number of arguments in a macro?
The number of arguments you give must match the number of parameters in the macro definition. When the macro is expanded, each use of a parameter in its body is replaced by the tokens of the corresponding argument. (You need not use all of the parameters in the macro body.)
Can a macro be left out of the preprocessor?
You can leave macro arguments empty; this is not an error to the preprocessor (but many macros will then expand to invalid code). You cannot leave out arguments entirely; if a macro takes two arguments, there must be exactly one comma at the top level of its argument list.
How to create a macro with an argument?
1 Macros with arguments To create a macro with arguments, put them in parentheses separated by commas after the macro name, e.g. 2 Multiple macros One macro can expand to another; for example, after defining Toggle line numbers 1 #define FOO BAR 2 #define BAR (12) 3 it will be the case 3 Macro tricks 3.1.