gasracrown.blogg.se

Define compiling code
Define compiling code











define compiling code

Several statements calling the ASSERT function can be added to the code to monitor the a condition as the program proceeds. Multiple statements can be included in the macro function definition by adding a backslash at the end of each line, allowing the definition to continue on the next line. The function can then excute appropriate statements according to the result of the evaluation. The condition to be evaluated will be passed from the caller as the ASSERT function argument. SIMPLE(Ten thousand dancing in the breeze.) īy defining an ASSERT macro function, we can evaluate a specified condition for a bollean value. SIMPLE(Along the Lake beneath the trees) SIMPLE(That floats on high oer Vales and Hills) String anotherline = "A host of dancing " When we use the merging operator (#), we can combine two terms into a single term. The stringizing operator is useful to pass string values to a preprocessor #define directive without needing to surround each string with double quote. In other words, it converts a characters passed as a macro argument into a string, and adds double quotes to enclose the string.Īll whitespaces before or after the characters passed as a macro argument to the stringizing operator (#) is ignored and multiple spaces between characters is reduced to just one space. It is used only with macros that take arguments. The number-sign or stringizing operator (#) converts macro parameters to string literals without expanding the parameter definition. Here is another example which may give unexpected results:Ī = SQR(b+2) // a = (b+2*b+2) = 3+2*3+2 = 11 not 25 So, we need to use the following Macro to get intended answer. Surprisingly, it prints out j = 64 instead of j = 4.īecause j = 64/ 4*4 but not j = 64/ (4*4). If the definition already exists, the compiler ignores the duplicate definition, otherwise, a #define directive will permit the compiler to use the definition in that header file. To create a macro to guard against duplication, an #ifndef directive first tests to see if the definition has already been made by another header file included in the same program. For instance, myfunction.h is represented as MYFUNCTION_H. The name is an uppercase version of the file name, with the dot changed to an underscore. This creates a unique macro name for each header file. The solution to this problem of redefinition is to use preprocessor directives to ensure the compiler will only be exposed to a single definition, so called inclusion guards.

define compiling code

This can cause duplication where definitions appear in two files. h header files, and the header files may contain one or more #include directives to make other classes or functions available from other header files. To test multiple definitions, the #ifdef macro can be expressed as #if defined and additional tests made #elif defined macros.Ī C++ code usually has many. The macro can then be redefined by using #define directive again. When that test returns true, it will do insert up the a corresponding #endif directive.Īny previously defined macro can be removed later using the #undef directive so that subsequent #ifdef conditional tests fail. On the other hand, the #ifndef directive tests to see if a specified macro has not been defined. When the macro has been defined, so the test returns true, the preprocessor will do insert on subsequent lines up to a corresponding #endif directive. The #ifdef directive performs the most common preprocessor function by testing to see if a specified macro has been defined. #ifdef, #ifndef, #if defined, #else, #elif defined For numeric substitution in expression the macro name should be enclosed in parentheses to ensure correct precedence. As with constant variable names, the macro name uses uppercase, and defined string values should be encolsed within double quotes. Similar to #include preprocessor directives, #define directives can appear at the start of the source code. The #define directives specifies a macro, comprising an identifier name and a string or numerics, to be substituted by the preprocessor for each occurance of that macro in the source code.Ĭout << STARS << endl << SITE << endl << STARS Ĭout << endl << "Next YEAR is: " << ((YEAR)+1) << endl Other preprocessor directives can be used to substitute test or numerics before assembly and compilation. In the previous section, we saw the preprocessor substitutes library code for #include directives.













Define compiling code