![]() 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. 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. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |