C

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 […]

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 […]

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 […]

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 […]