C

Programming Error – Unintended String Concatenation

In an effort to be helpful, C and C++ compilers concatenate adjacent strings together. In the following example, the compiler will concatenate the two strings into a single string: printf("This is a string " "and this is another string."); Which is probably what you intended anyway. Because C and C++ are free form languages, adjacent […]

C

Keyword – switch, case, default

The switch keyword is probably the least well understood of the C/C++ language keywords (although, const probably comes a close second). The keywords switch, case, and default always go together and cannot be used independently in any other context. There is a small difference in behaviour between C and C++. Most programmers will never notice […]

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

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

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

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

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

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

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

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

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