![]() Printf("Blank at index %d\n", (int)( (p - 1) - av)) The macro will be considered as a single (meta-)instruction: the "do-while(0)".ĭo ) Writing the macro without terminating " ", the user is obliged to put a " " after calling the macro otherwise the compiler ends in error. Do not merely put braces without the beginning "do" and terminating "while(0)" as we may face the problem presented in § 2 if this macro is used in a "if-else" with a terminating " ". The compiler will not generate a loop as the iteration condition is always false: while(0). This makes the multi-instructions macro appear as one (meta-)instruction: To prevent this kind of tricky error, it is advised to group the instructions of the macro in a "do-while" not followed by a " " (according to the rule of § 2). printf()) will be executed upon each loop iteration: We expect that ALL the instructions composing ADD() be executed upon each loop iteration instead of:Īctually, the preprocessor will generate a set of instructions without surrounding braces and consequently only the first instruction (i.e. For example, if we use it without braces in a while statement: This is dangerous to write macros in this way because it introduces tricky bugs. Or perhaps the macro originally did not contain multiple instructions and a software modification required to add more instructions into it. We may face the problem presented in the preceding paragraph if it is used inĪ if statement as the user of the macro may not put braces around the macro call. When a macro embeds several instructions as shown in this example: Thus, it is advised to systematically put blank chars around "+" and "-" operators and when we define macros with the "e" hexadecimal number. This makes the compilation and execution work: To fix this issue, we need to rewrite CONSTANT_2 with white spaces between "e" and "+": This is not a legal notation in C language as exponents are not allowed for hexadecimal contants like "0x3". In other words it sees 0x3 multiplied by 10 power 20. ![]() This is a weakness of the compiler which believes to see an integer (0x3) followed by an exponent (e+20). The compiler detected an error at line 4, column 21 that is to say in the CONSTANT_2 macro. Try_1.c:10:19: note: in expansion of macro 'CONSTANT_2' Try_1.c:4:21: error: invalid suffix "+20" on integer constant When compiling this program, we get an unexpected error: ![]() We can define and use constants as follow:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |