资源预览内容
第1页 / 共53页
第2页 / 共53页
第3页 / 共53页
第4页 / 共53页
第5页 / 共53页
第6页 / 共53页
第7页 / 共53页
第8页 / 共53页
第9页 / 共53页
第10页 / 共53页
亲,该文档总共53页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
Chapter 9 The Preprocessor,9_1.1 Introduction9_1.2 The #include Preprocessor Directive9_1.3 The #define Preprocessor Directive: Symbolic Constants9_1.4 The #define Preprocessor Directive: Macros9_1.5 Conditional Compilation,Outline,The #include Preprocessor DirectiveThe #define Preprocessor Directive: Symbolic Constants,Key Points,9_1.1 Introduction,The role played by each processor program during the build process:,9_1.1 Introduction,PreprocessingOccurs before a program is compiledInclusion of other files Definition of symbolic constants and macrosConditional compilation of program codeConditional execution of preprocessor directivesFormat of preprocessor directivesLines begin with # Only white space characters before directives on a line,9_1.2 The #include Preprocessor Directive,#includeCopy of a specified file included in place of the directive #include Searches standard library for file Use for standard library files#include filename Searches current directory, then standard library Use for user-defined filesUsed for:Programs with multiple source files to be compiled togetherHeader file has common declarations and definitions (classes, structures, function prototypes)#include statement in each file,9_1.2 The #include Preprocessor Directive,#include,9_1.3 The #define Preprocessor Directive: Symbolic Constants,#definePreprocessor directive used to create symbolic constants and macrosSymbolic constantsWhen program compiled, all occurrences of symbolic constant replaced with replacement textFormat#define identifier replacement-text Example:#define PI 3.14159 Everything to right of identifier replaces text#define PI = 3.14159Replaces “PI” with = 3.14159Cannot redefine symbolic constants once they have been created,9_1.4 The #define Preprocessor Directive: Macros,MacroOperation defined in #defineA macro without arguments is treated like a symbolic constantA macro with arguments has its arguments substituted for replacement text, when the macro is expandedPerforms a text substitution no data type checkingThe macro#define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) would causearea = CIRCLE_AREA( 4 ); to becomearea = ( 3.14159 * ( 4 ) * ( 4 ) );,9_1.4 The #define Preprocessor Directive: Macros,Use parenthesisWithout them the macro#define CIRCLE_AREA( x ) PI * ( x ) * ( x ) would causearea = CIRCLE_AREA( c + 2 );to becomearea = 3.14159 * c + 2 * c + 2;Multiple arguments#define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) )would causerectArea = RECTANGLE_AREA( a + 4, b + 7 ); to becomerectArea = ( ( a + 4 ) * ( b + 7 ) );,9_1.4 The #define Preprocessor Directive: Macros,9_1.4 The #define Preprocessor Directive: Macros,带参的宏与函数区别,9_1.4 The #define Preprocessor Directive: Macros,#undefUndefines a symbolic constant or macroIf a symbolic constant or macro has been undefined it can later be redefined,9_1.5 Conditional Compilation,Conditional compilation Control preprocessor directives and compilationCast expressions, sizeof, enumeration constants cannot be evaluated in preprocessor directivesStructure similar to if#if !defined( NULL )#define NULL 0#endif Determines if symbolic constant NULL has been definedIf NULL is defined, defined( NULL ) evaluates to 1If NULL is not defined, this function defines NULL to be 0Every #if must end with #endif#ifdef short for #if defined( name )#ifndef short for #if !defined( name ),9_1.5 Conditional Compilation,Other statements#elif equivalent of else if in an if structure#else equivalent of else in an if structureComment out codeCannot use /* . */Use#if 0code commented out#endifTo enable code, change 0 to 1,9_1.5 Conditional Compilation,Debugging #define DEBUG 1#ifdef DEBUG cerr Variable x = x endl;#endif Defining DEBUG to 1 enables codeAfter code corrected, remove #define statement Debugging statements are now ignored,9_1.6 The #error and #pragma Preprocessor Directives,#error tokensTokens are sequences of characters separated by spacesI like C+ has 3 tokensDisplays a message including the specified tokens as an error messageStops preprocessing and prevents program compilation#pragma tokensImplementation defined action (consult compiler documentation)Pragmas not recognized by compiler are ignored,9_1.7 The # and # Operators,#Concatenates two tokensThe statement#define TOKENCONCAT( x, y ) x # y would causeTOKENCONCAT(O,K) to becomeOK,9_1.8 Line Numbers,#lineRenumbers subsequent code lines, starting with integer valueFile name can be included#line 100 myFile.cLines are numbered from 100 beginning with next source code fileCompiler messages will think that the error occurred in myfile.CMakes errors more meaningfulLine numbers do not appear in source file,9_1.9 Predefined Symbolic Constants,Five predefined symbolic constantsCannot be used in #define or #undef,9_1.10 Assertions,assert macro Header Tests value of an expressionIf 0 (false) prints error message and calls abortExample:assert( x = 10 );If NDEBUG is definedAll subsequent assert statements ignored#define NDEBUG,
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号