语言程序设计函数课件.ppt

语言程序设计函数课件.ppt

ID:57028971

大小:211.00 KB

页数:39页

时间:2020-07-26

语言程序设计函数课件.ppt_第1页
语言程序设计函数课件.ppt_第2页
语言程序设计函数课件.ppt_第3页
语言程序设计函数课件.ppt_第4页
语言程序设计函数课件.ppt_第5页
资源描述:

《语言程序设计函数课件.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、第九章编译预处理编译预处理:在源程序文件中,加入“编译预处理命令”,使编译程序在对源程序进行通常的编译之前,先对这些命令进行预处理,然后将预处理的结果和源程序一起再进行通常的编译处理,以得到目标代码(OBJ文件)。ThepreprocessorThepreprocessor,asitsnameimplies,isaprogramthatprocessesthesourcecodebeforeitpassesthroughthecompiler.Itoperatesunderthecontrolofwhatisknownaspreprocessorcommandl

2、inesordirectives.Preprocessordirectivesareplacedinthesourceprogrambeforethemainline.Beforethesourcecodepassesthroughthecompiler,itisexaminedbythepreprocessorforanypreprocessordirectives.Theyallbeginwiththesymbol#incolumnoneanddonotrequireasemicolonattheend.Thedirectivescanbedividedin

3、tothreecategories:Macrosubstitutiondirectives.Fileinclusiondirectives.Compilercontroldirectives.C提供的编译预处理命令宏命令(Macro)文件包含命令(include)条件编译命令(ifdef)这些命令均以#开头,以区别于语句。MacrosubstitutionMacrosubstitutionisaprocesswhereanidentifierinaprogramisreplacedbyapredefinedstringcomposedofoneormoretok

4、ens.theprocessoraccomplishesthistaskunderthedirectionof#definestatement.Usuallyknownasamacrodefinition(orsimplyamacro)DifferentformsofthemacrosubstitutionSimplemacrosubstitutionArgumentedmacrosubstitution.Nestedmacrosubstitution§9.1宏(Macrosubstitution)一、不带参数的宏simplestringreplacementi

5、scommonlyusedtodefineconstants.一般形式:#define标识符字符串如:#definePI3.1415926#define:宏定义命令#undef:终止宏定义命令[例9.1]#definePI3.1415926main(){floatl,s,r,v;printf(“inputradius:”);scanf(“%f”,&r);l=2.0*PI*r;s=PI*r*r;v=4.0/3.0*PI*r*r*r;printf("l=%10.4fs=%10.4fv=%10.4f",l,s,v);}关于宏定义的说明:1、一般宏名用大写字

6、母表示。2、使用宏可以提高程序的可读性和可移植性。如上述程序中,多处需要使用π值,用宏名既便于修改又意义明确。3、宏定义是用宏名代替字符串,宏扩展时仅作简单替换,不检查语法。语法检查在编译时进行。4、宏定义后面不能有分号。如果加入分号,则连分号一起替换。例如:#definePI3.1415926;area=PI*r*r;在宏扩展后成为:area=3.1315926;*r*r;结果在编译时出现语法错误。5、通常把#define命令放在一个文件的开头,使其在本文件全部有效。(#define定义的宏仅在本文件有效,在其它文件中无效,这与全局变量不同)。6、宏定义终止命

7、令#undef结束先前定义的宏名。#defineG9.8main(){}#undefG/*取消G的意义*/f1()┆7、宏定义中可以引用已定义的宏名。[例9.2]#defineR3.0#definePI3.1415926#deinfeL2*PI*R#defineSPI*R*Rmain(){printf("L=%fS=%f",L,S);}8、对程序中用双引号括起来的字符串,即使与宏名相同,也不替换。例如上例的printf语句中,双引号括起来L和S不被替换。二、带参数的宏whenamacroiscalled,thepreprocessorsubstitute

8、sthestring,r

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。