




已阅读5页,还剩41页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
AFirstBookofANSICFourthEdition Chapter14AdditionalCapabilities AFirstBookofANSIC FourthEdition 2 Objectives AdditionalFeaturesBitOperationsMacrosCommand LineArgumentsCommonProgrammingandCompilerErrors AFirstBookofANSIC FourthEdition 3 AdditionalFeatures ThetypedefdeclarationstatementConditionalpreprocessordirectivesEnumeratedconstantsConditionalexpressionsThegotostatement AFirstBookofANSIC FourthEdition 4 ThetypedefDeclarationStatement typedefpermitsconstructingalternatenamesforanexistingCdatatypenametypedefdoubleREAL makestheREALanaliasfordoubleREALval isequivalenttodoubleval typedefdoesnotcreateanewdatatypeTheequivalenceproducedbytypedefcanfrequentlybeproducedequallywellby definetypedefprocessedbythecompiler defineprocessedbythepreprocessor AFirstBookofANSIC FourthEdition 5 ThetypedefDeclarationStatement continued Anotherexample typedefintARRAY 100 ARRAYfirst second Equivalenttothetwodefinitionsintfirst 100 andintsecond 100 Asanotherexample typedefstruct charname 20 intidNum empRecord empRecordemployee 75 AFirstBookofANSIC FourthEdition 6 ConditionalPreprocessorDirectives ifndefand ifdefpermitconditionalcompilationinthatthestatementsimmediatelyfollowingthesedirectives uptoan elseor endif arecompiledonlyiftheconditionistrue whereasthestatementsfollowingthe elsearecompiledonlyiftheconditionisfalseForexample ifndefconditioncompilethestatementsplacedhere elsecompilethestatementsplacedhere endif AFirstBookofANSIC FourthEdition 7 ConditionalPreprocessorDirectives continued The elsedirectiveisoptional ifndefisthemostfrequentlyusedconditionalpreprocessordirectiveItsmostcommonusageisintheform ifndefheader file include endifForexample ifndefstdio h include endif AFirstBookofANSIC FourthEdition 8 EnumeratedConstants Setofrelatedintegervaluescanbeequatedtoanequivalentsetofconstantsusinganenumeratedlistenum Mon Tue Wed Thr Fri Sat Sun Bydefault thefirstenumeratednameinanenumeratedlisthasavalueof0Theenumerationaboveisequivalentto defineMon0 defineTue1 defineWed2 AFirstBookofANSIC FourthEdition 9 EnumeratedConstants continued Tospecifythevalueofthefirstenumeratedname enum Mon 1 Tue Wed Thr Fri Sat Sun Anyintegerconstantcanbeequatedtoenumeratednames theyneednotbeinsequence andanoptionalenumeratedlistnamecanbeusedenumescsequences BELL a BACKSPACE b NEWLINE n RETURN r TAB t Enumeratedconstantscanbeanyvaliduser createdidentifier buteachnamemustbeuniqueOKfortwoconstantstobeequatedtothesameintegervalue AFirstBookofANSIC FourthEdition 10 ConditionalExpressions Aconditionalexpressionusestheconditionaloperator andprovidesanalternatewayofexpressingasimpleif elsestatementexpression1 expression2 expression3Forexample theif elsestatementif hours 40 rate 0 045 elserate 0 02 Canbereplacedwithrate hours 40 0 045 0 02 isuniqueinCinthatitisaternaryoperator AFirstBookofANSIC FourthEdition 11 ConditionalExpressions continued Conditionalexpressionsareonlyusefulinreplacingif elsestatementswhentheexpressionsintheequivalentif elsestatementarenotlongorcomplicatedForexample thestatementmaxVal a b a b isaone linestatementthatassignsthemaximumvalueofthevariablesaandbtomaxValAlonger equivalentformofthisstatementisif a b maxVal a elsemaxVal b AFirstBookofANSIC FourthEdition 12 ThegotoStatement gotoprovidesanunconditionaltransferofcontroltosomeotherstatementinaprogramgotolabel labelisanyuniquenamechosenaccordingtotherulesforcreatingvariablenamesForexample if denom 0 0 gotoerr elseresult num denom err printf Error AttemptedDivisionbyZero AFirstBookofANSIC FourthEdition 13 ThegotoStatement continued Generally itismucheasiertocallanerrorroutineforunusualconditionsoruseabreakstatementifthisisnecessary ratherthanuseagotoTheoretically agotoisneverrequiredbecauseC snormalstructuresprovidesufficientflexibilitytohandleallpossibleflowcontrolrequirementsgotostendtocomplicateprogramsUsingevenonegotostatementinaprogramisalmostalwaysasignofbadprogrammingstructure AFirstBookofANSIC FourthEdition 14 BitOperations AFirstBookofANSIC FourthEdition 15 TheANDOperator causesabit by bitANDcomparisonbetweenitstwooperandsANDoperationsareextremelyusefulinmasking oreliminating selectedbitsfromanoperandForexample The0sinop2effectivelymasktherespectivebitsinop1 whiletheonesinop2filtertherespectivebitsinop1throughwithnochangeintheirvaluesInthisexample op2iscalledamask AFirstBookofANSIC FourthEdition 16 TheANDOperator continued Program14 1producesthefollowingoutput 325ANDedwith263is221 AFirstBookofANSIC FourthEdition 17 TheInclusiveOROperator TheresultofaninclusiveOR bitcomparisonis1ifeitherbitisa1 otherwisetheresultisa0InclusiveORoperationsareextremelyusefulinforcingselectedbitstotakeona1valueorforpassingthroughotherbitvaluesunchangedForexample ORingwitha0hasthesameeffectasANDingwitha1 AFirstBookofANSIC FourthEdition 18 TheInclusiveOROperator continued Program14 2producesthefollowingoutput 325ORedwith263is367 AFirstBookofANSIC FourthEdition 19 TheExclusiveOROperator TheresultofanexclusiveOR comparisonis1ifoneandonlyoneofthebitsbeingcomparedisa1 otherwisetheresultis0AnexclusiveORoperationcanbeusedtocreatetheoppositevalue orcomplement ofanyindividualbitinavariableForexample AFirstBookofANSIC FourthEdition 20 TheComplementOperator istheunarycomplementoperatorIfop1containsthebinarynumber11001010 op1replacesthisnumberwith00110101 isusedtoforceanybitinanoperandto0 independentofthenumberofbitsusedtostoreitop1 ifithas32bits Thismakestheprogramportablebetweenmachinesusingdifferentintegerstoragesizes AFirstBookofANSIC FourthEdition 21 Different SizedDataItems When and areusedwithoperandsofdifferentsizes theshorteroperandisincreasedinbitsizetomatchthesizeofthelargeroperandWhenextendingsignednumbers theoriginalleftmostbitisreproducedintheadditionalbitsthatareaddedtothenumber AFirstBookofANSIC FourthEdition 22 Different SizedDataItems continued AFirstBookofANSIC FourthEdition 23 Different SizedDataItems continued AFirstBookofANSIC FourthEdition 24 TheShiftOperators Theleftshiftoperator causesthebitsinanoperandtobeshiftedtotheleftbyagivenamountForexample p1 op1 4 Forunsignedintegers eachleftshiftcorrespondstomultiplicationby2Alsotrueforsignednumbersusingtwo scomplementrepresentation aslongastheleftmostbitdoesnotswitchvalues AFirstBookofANSIC FourthEdition 25 TheShiftOperators continued AFirstBookofANSIC FourthEdition 26 TheShiftOperators continued Therightshiftoperator causesthebitsinanoperandtobeshiftedtotherightbyagivenamountForexample op1 op1 3 AFirstBookofANSIC FourthEdition 27 TheShiftOperators continued AFirstBookofANSIC FourthEdition 28 TheShiftOperators continued AFirstBookofANSIC FourthEdition 29 TheShiftOperators continued ThetypeoffillshowninFigures14 7band14 7c wherethesignbitisreproducedinvacatedbitpositions isknownasanarithmeticrightshiftInanarithmeticrightshift eachsingleshifttotherightcorrespondstoadivisionby2Somecomputersautomaticallyfillthevacatedbitswith0s thistypeofshiftisknownasalogicalshiftForpositivesignednumbers wheretheleftmostbitis0 botharithmeticandlogicalrightshiftsproducethesameresult AFirstBookofANSIC FourthEdition 30 Macros Initssimplestform the definepreprocessorisusedtoequateconstantsandoperatorstosymbolicnames defineSALESTAX0 05ThesubstitutionsaremadebytheCpreprocessorjustpriortoprogramcompilationCplacesnorestrictionsontheequivalencesthatcanbeestablishedwiththe definestatementWhentheequivalenceconsistsofmorethanasinglevalue operator orvariable thesymbolicnameisreferredtoasamacro AFirstBookofANSIC FourthEdition 31 Macros continued Forexample theequivalenceestablishedbythestatement defineFORMAT Theansweris f n enablesustowritethestatementprintf FORMAT 15 2 Thecompileralwaysreceivestheexpandedversionafterthetexthasbeeninsertedinplaceofthesymbolicnamebythepreprocessor AFirstBookofANSIC FourthEdition 32 Macros continued Equivalencescanusearguments defineSQUARE x x xy SQUARE num isexpandedtoy num num Advantage sincethedatatypeoftheargumentisnotspecified themacrocanbeusedwithanydatatypeargumentBecareful val SQUARE num1 num2 isexpandedtoval num1 num2 num1 num2 Solution use defineSQUARE x x x AFirstBookofANSIC FourthEdition 33 Macros continued MacrosareextremelyusefulwhenthecalculationsorexpressionstheycontainarerelativelysimpleAmacrodefinitioncanbecontinuedonanewlinebyusinga TheadvantageofusingamacroinsteadofafunctionisanincreaseinexecutionspeedNoexecutiontimelossduetothecallandreturnproceduresrequiredbyafunctionDisadvantage theincreaseinrequiredprogrammemoryspacewhenamacroisusedrepeatedlyEachtimeamacroisused thecompletemacrotextisreproducedandstoredaspartoftheprogramAfunctionisstoredinmemoryonlyonce AFirstBookofANSIC FourthEdition 34 Command LineArguments AFirstBookofANSIC FourthEdition 35 Command LineArguments continued Youcanusecommand lineargumentstopassargumentstoamain functionC pgm14 3threeblindmiceTostandardizeargumentspassingtomain onlytwoitemsareallowed anumberandanarrayThenumberisanintegervariable whichmustbenamedargc shortforargumentcounter Thearrayisaone dimensionallist whichmustbenamedargv shortforargumentvalues AFirstBookofANSIC FourthEdition 36 Command LineArguments continued AFirstBookofANSIC FourthEdition 37 Command LineArguments continued AFirstBookofANSIC FourthEdition 38 Command LineArguments continued AFirstBookofANSIC FourthEdition 39 Command LineArguments continued IftheexecutableversionofProgram14 3isnamedpgm14 3 exe asampleoutputforthecommandlinepgm14 3threeblindmiceis Thenumberofitemsonthecommandlineis4Theaddressstoredinargv 0 is3280036ThecharacterpointedtoispTheaddressstoredinargv 1 is3280044ThecharacterpointedtoistTheaddressstoredinargv 2 is3280050ThecharacterpointedtoisbTheaddressstoredinargv 3 is3280056Thecharacterpointedtoism AFirstBookofANSIC FourthEdition 40 Command LineArguments continued AFirstBookofANSIC FourthEdition 41 Command LineArguments continued AFirstB
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- DeepSeek大模型赋能数字医疗规划方案
- 人的能力与个性分析
- 宪法考研试题及答案
- 物理振动试题及答案
- 湖北省云学联盟2024-2025学年高一下学期5月月考英语试题(含答案)
- 密封胶施工饱满度与连续性技术专题
- 2025短期用工劳动合同模板
- 提高工程设计企业的成本控制与预算管理
- 2025标准版担保借款合同样式
- P-gp-inhibitor-28-生命科学试剂-MCE
- 早期预警评分量表(MEWS评分表)
- 2024年上海市七年级语文下学期期末考试复习(基础知识+课内古诗文+课外文言文)
- 交通出行车费报销单模板
- 中国民族钢琴艺术鉴赏智慧树知到期末考试答案章节答案2024年西安交通大学
- 安徽省合肥市包河区2024届八年级数学第二学期期末学业质量监测试题含解析
- 健身房安全知识培训
- 《诫子书》同步训练 课堂达标 考点过关(四套)
- 策划视频大赛策划方案
- 深度学习技术在医学图像识别中的应用
- 《卡诺循环演示》课件
- 《如何阅读文献》课件
评论
0/150
提交评论