Articles Comments

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

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 #error Directive

Preprocessor – the #error Directive This is a very useful and often underused preprocessor directive. Behaviour of this preprocessor directive is the same for both C and C++ compilers. Purpose The #error directive terminates compilation and outputs the text following the directive. Format #error text 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 error, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the # and error together. If anything follows the #error directive (other than white space) then the program is malformed. The following are valid … Read entire article »

Filed under: C, C++

Preprocessor – the #line Directive

I don’t think I have ever seen this directive used. Behaviour of this preprocessor directive is the same for both C and C++ compilers. Purpose The #line directive allows setting the current line number and name of the file being compiled. Format #line integer or #line integer "file_name" or #line preprocessing_expression 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 line, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the # and line together. If anything follows the #line directive (other than white space) then the program is … Read entire article »

Filed under: C, C++

Preprocessor – the #pragma Directive

Behaviour of this preprocessor directive is the same for both C and C++ compilers. Behaviour of this directive is very likely to vary from one compiler vendor to another. Purpose The #pragma directive allows implementation specific control of the compiler. Format #pragma command 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 pragma, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the # and pragma together. If anything follows the #pragma directive (other than white space) then the program is malformed. The following are valid uses: #pragma … Read entire article »

Filed under: C, C++

Preprocessor – Understanding the stringizing (#) Operator

This is one of three preprocessor operators. The other two operators are the token pasting operator (##) and the define operator. The behaviour is the same for both C and C++ compilers. Purpose The stringizing operator is used to convert parameters passed to a macro into strings. Format # token_to_turn_into_a_string Use The stringizing (#) operator may only be used with #define preprocessor directives taking a parameter. The parameter token immediately to the right of the stringizing operator (ignoring any white space) is transformed by the preprocessor into a string. For example: #define make_string(x) #x will convert whatever is passed as parameter x into a string. Leading and trailing white space are deleted. Any white space between the parameter's tokens are collapsed into a single white space character between tokens. Any conversions necessary to make character literals in the parameter's tokens valid in a string … Read entire article »

Filed under: C, C++

Preprocessor – the #endif Directive

Behaviour of this preprocessor directive is the same for both C and C++ compilers. Purpose The #endif directive is used end / close / terminate a selection block (#if, #ifdef, or #ifndef. Format #if or #ifdef or #ifndef preprocessor or code statements #elif controlling_expression (optional) preprocessor or code statements #else (optional) preprocessor or code statements #endif 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 endif, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the # and endif together. If anything follows the #endif directive (other than white space) … Read entire article »

Filed under: C, C++

Preprocessor – the #else Directive

#else is one of five preprocessor selection statements allowing selection of alternative sections of code for compilation. The other four selection statements are: #ifdef, #ifndef, #if, and #elif. Behaviour of this preprocessor directive is the same for both C and C++ compilers. Purpose The #else directive provides a final alternative for a preprocessor selection block. If all previous selection options fail, then the code found between the #else and #endif is compiled. Format #if or #ifdef or #ifndef preprocessor or code statements #elif controlling_expression (optional) preprocessor or code statements #else preprocessor or code statements #endif 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 … Read entire article »

Filed under: C, C++

Preprocessor – the #ifndef Directive

Behaviour of the #ifndef directive is the same in both C and C++. Purpose The #ifndef directive is one of five preprocessor selection statements allowing selection of alternative sections of code for compilation. The other four selection statements are: #ifdef, #if, #elif, and #else. Format #ifndef macro name valid preprocessor or code statements #endif or #elif or #else 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 ifndef, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the # and ifndef together. The following are valid uses: #ifndef … Read entire article »

Filed under: C, C++

Preprocessor – the #elif Directive

This (along with the #if directive) is probably the second most complicated preprocessor directive because the controlling expression can be complex and include tricky macro replacements. #elif is one of five preprocessor selection statements allowing selection of alternative sections of code for compilation. The other four selection statements are: #ifdef, #ifndef, #if, and #else. Behaviour of this preprocessor directive is the same for both C and C++ compilers. An identifier is a sequence of letters, numbers and underscore characters. Identifiers must begin with a letter or underscore character. Purpose The #elif directive controls whether the statements found between it and a terminating #endif, #else, or #elif directive are compiled or skipped. The decision is based on the value of the controlling expression. It is used to allow creating more complex code selection blocks, featuring more … Read entire article »

Filed under: C, C++

Preprocessor – the #if Directive

This (along with the #elif directive) is probably the second most complicated preprocessor directive because the controlling expression can be complex and include tricky macro replacements. #if is one of five preprocessor selection statements allowing selection of alternative sections of code for compilation. The other four selection statements are: #ifdef, #ifndef, #elif, and #else. Behaviour of this preprocessor directive is the same for both C and C++ compilers. An identifier is a sequence of letters, numbers and underscore characters. Identifiers must begin with a letter or underscore character. Purpose The #if directive controls whether the statements found between it and a terminating #endif, #else, or #elif directive are compiled or skipped. The decision is based on the value of the controlling expression. Format #if controlling_expression preprocessor or code statements #elif or #else or #endif All preprocessor directives … Read entire article »

Filed under: C, C++