《C语言学习第九章(英文版)》由会员分享,可在线阅读,更多相关《C语言学习第九章(英文版)(53页珍藏版)》请在金锄头文库上搜索。
1、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 Directiv
2、e: 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 codeCondit
3、ional 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 f
4、ile 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 s
5、tatement 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
6、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,MacroOpe
7、ration 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
8、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 argumen
9、ts#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:
10、 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
11、 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
12、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 Condition
13、al 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 character
14、s 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 # a
15、nd # 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
16、 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,