Macros are tricky things to use in C/C++ programming. When they work, they work great. When they have bugs, they are a pain to troubleshoot.
Macros are a simple text substitution done by the preprocessor. Whenever the macro name is encountered in your code, the preprocessor replaces it with the text to the right of the macro name (plugging in any optional parameters you may have passed).
A macro is composed of three parts:
- #define – this introduces the macro
- macro name and optional parameter list. The macro name cannot have any spaces and the parameter list cannot be separated from the macro name with any spaces.
- text that will replace the macro – everything following the macro name (and optional parameter list), until the end of the line, is what the preprocessor will use to replace the macro name. For long macros, or for better formatting, the macro can be defined over several lines by ending each line with a backslash character (\) and a new line (enter) (extra spaces between the backslash character and new line means the macro ends there – so be careful of extra spaces following the backslash).
Working Examples:
#define my_macro // preprocessor will replace my_macro with this comment.
#define my_macro // when the preprocessor encounters my_macro, \ it will replace it with everything on this \ line that is to the right of the definition \ (which is this comment). Fortunately, \ it is possible to spread the line over several \ lines by using the backslash character
Not Working Examples:
The following macro won’t compile following expansion (it is supposed to replace the macro with the contents of the parameter list):
#define my_macro (x) x
my_macro(int a);
The (unhelpful but correct) error message from the compiler (GCC v.4.4.1) is:
error: expected '=', ',', ';', 'asm' or '__attribute__' before 'x'
The problem is the space between the macro name and the parameter list – there should be none.
Here’s another macro that won’t compile (like the one above, it is supposed to replace the macro with the parameters passed) :
#define my_macro (x, y) x y
my_macro(int, abc);
The (unhelpful but correct) from the compiler (GCC v.4.4.1) is:
error: expected ')' before ',' token
Again, the problem is a space between the macro name and the parameter list. Removing the space fixes the problem.
It is an error to separate the macro name and the macro parameter list with a space.