Complete, Concrete, Concise » 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 strings can appear in many different formats – as this string table shows: char *array[] = { "str_1", "str_2", "str_3" "str_4", "str_5", NULL}; Did you notice the missing comma (,) after "str_3"? It can be a hard one to spot. This is the type of bug that can result in something like a simple parser failing: if (strcmp(array[i], test_string) == 0) because str_3 and str_4 will … Read entire article »
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 the difference. The most common use of switch is as a replacement for multiple if-else statements: switch (value) … Read entire article »
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 »
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 »
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 »
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 »
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 »
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 »
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 »
How to Add Comments to Macros
July 20th, 2011 | 4 Comments
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++