C语言学习第九章(英文版)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Chapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C1Chapter9ThePreprocessorChapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C29_1.1Introduction9_1.2The#includePreprocessorDirective9_1.3The#definePreprocessorDirective:SymbolicConstants9_1.4The#definePreprocessorDirective:Macros9_1.5ConditionalCompilationOutlineChapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C3The#includePreprocessorDirectiveThe#definePreprocessorDirective:SymbolicConstantsKeyPointsChapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C49_1.1IntroductionTheroleplayedbyeachprocessorprogramduringthebuildprocess:Chapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C59_1.1IntroductionPreprocessingOccursbeforeaprogramiscompiledInclusionofotherfilesDefinitionofsymbolicconstantsandmacrosConditionalcompilationofprogramcodeConditionalexecutionofpreprocessordirectivesFormatofpreprocessordirectivesLinesbeginwith#OnlywhitespacecharactersbeforedirectivesonalineChapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C69_1.2The#includePreprocessorDirective#includeCopyofaspecifiedfileincludedinplaceofthedirective#includefilenameSearchesstandardlibraryforfileUseforstandardlibraryfiles#includefilenameSearchescurrentdirectory,thenstandardlibraryUseforuser-definedfilesUsedfor:ProgramswithmultiplesourcefilestobecompiledtogetherHeaderfile–hascommondeclarationsanddefinitions(classes,structures,functionprototypes)#includestatementineachfileChapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C79_1.2The#includePreprocessorDirective#include#include“file2.c”file1.cAfile3.cC#include“file3.c”file2.cBfile1.cAfile3.cfile2.cChapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C89_1.3The#definePreprocessorDirective:SymbolicConstants#definePreprocessordirectiveusedtocreatesymbolicconstantsandmacrosSymbolicconstantsWhenprogramcompiled,alloccurrencesofsymbolicconstantreplacedwithreplacementtextFormat#defineidentifierreplacement-textExample:#definePI3.14159Everythingtorightofidentifierreplacestext#definePI=3.14159Replaces“PI”with=3.14159CannotredefinesymbolicconstantsoncetheyhavebeencreatedChapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C99_1.4The#definePreprocessorDirective:MacrosMacroOperationdefinedin#defineAmacrowithoutargumentsistreatedlikeasymbolicconstantAmacrowithargumentshasitsargumentssubstitutedforreplacementtext,whenthemacroisexpandedPerformsatextsubstitution–nodatatypecheckingThemacro#defineCIRCLE_AREA(x)(PI*(x)*(x))wouldcausearea=CIRCLE_AREA(4);tobecomearea=(3.14159*(4)*(4));Chapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C109_1.4The#definePreprocessorDirective:MacrosUseparenthesisWithoutthemthemacro#defineCIRCLE_AREA(x)PI*(x)*(x)wouldcausearea=CIRCLE_AREA(c+2);tobecomearea=3.14159*c+2*c+2;Multiplearguments#defineRECTANGLE_AREA(x,y)((x)*(y))wouldcauserectArea=RECTANGLE_AREA(a+4,b+7);tobecomerectArea=((a+4)*(b+7));Chapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C119_1.4The#definePreprocessorDirective:Macros#defineMAX(x,y)(x)(y)?(x):(y)…….main(){inta,b,c,d,t;…….t=MAX(a+b,c+d);……}宏展开:t=(a+b)(c+d)?(a+b):(c+d);intmax(intx,inty){return(xy?x:y);}main(){inta,b,c,d,t;…….t=max(a+b,c+d);………}Chapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C129_1.4The#definePreprocessorDirective:Macros带参的宏与函数区别带参宏函数处理过程不分配内存简单的字符置换分配内存先求实参值,再代入形参处理时间编译时程序运行时参数类型无类型问题定义实参,形参类型程序长度变长不变运行速度不占运行时间调用和返回占时间Chapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C139_1.4The#definePreprocessorDirective:Macros#undefUndefinesasymbolicconstantormacroIfasymbolicconstantormacrohasbeenundefineditcanlaterberedefinedChapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C149_1.5ConditionalCompilationConditionalcompilationControlpreprocessordirectivesandcompilationCastexpressions,sizeof,enumerationconstantscannotbeevaluatedinpreprocessordirectivesStructuresimilartoif#if!defined(NULL)#defineNULL0#endifDeterminesifsymbolicconstantNULLhasbeendefinedIfNULLisdefined,defined(NULL)evaluatesto1IfNULLisnotdefined,thisfunctiondefinesNULLtobe0Every#ifmustendwith#endif#ifdefshortfor#ifdefined(name)#ifndefshortfor#if!defined(name)Chapter9ThePreprocessorFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C159_1.5ConditionalCompilationOtherstatements#elif–equivalentofelseifinanifstructure#else–equivalentofelseinanifstructureCommentoutcodeCann

1 / 53
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功