Articles Comments

Complete, Concrete, Concise » Entries tagged with "macro"

How to Add Comments to Macros

Comments are an important part of documenting your code. Adding comments to macros is quite easy, but it has to be done the right way. This works with both C and C++ compilers. The easiest way to document macros is to just add comments before or after the macro definition: // returns the larger of the two arguments #define MAX(x, y) (x)>(y)?(x):(y) This works well for small macros, but if you have a larger macro that is spread over several lines, it might be nice to put comments nearer some tricky or crucial bit of code. You can do that by using C-style comments (/**/). The preprocessor treats comments as white space, so comments of the form /* some sort of comment */ are treated as white space. #define TRANS_TEST(a, b, c) \ { /* variables … Read entire article »

Filed under: C, C++

Preprocessor – the #undef Directive

Behaviour of the #undef directive is the same in both C and C++. Purpose It is used to undefine a macro. A macro is an identifier (or label) followed by replacement text. There is only a single namespace for macros. A program which redefines an existing macro is considered to be malformed – even though most compilers only generate a warning instead of an error. Format #undef MACRO_NAME All preprocessor directives begin with the # symbol. It must be the first character on the line or the first character on the line following optional white space. Some early compilers flagged an error if # was not the first character on the line. Spaces or tabs are permitted between the # and undef, but not escape characters or other symbols or macros. The preprocessor removes whitespace and … Read entire article »

Filed under: C, C++

Preprocessor – The #define Directive

This is the most complicated of the preprocessor directives (a little over 7 pages is devoted to it in the C99 spec and the C++98 spec devotes about 4 pages to it). Behaviour of the #define directive is the same in both C and C++. C99 added variable argument macros; the upcoming C++0x standard adds variadic macros, as well. Purpose It is used to define a macro. A macro is an identifier (or label) followed by text. When the preprocessor encounters the macro in the program it replaces the macro with the text that follows the macro name. There is no restriction on what may be in the replacement text. The only restriction on the replacement text is that it must be all on the same line as the #define directive (you can use the … Read entire article »

Filed under: C, C++

Compilation Error – Improperly Defined Macro

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 … Read entire article »

Filed under: C, C++