




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025福建宁德市建周建材有限公司招聘4人笔试历年参考题库附带答案详解
- 2025福建南平闽延电力建设公司招聘3人笔试历年参考题库附带答案详解
- 2025甘肃定西市陇西县人力资源有限公司招聘工作人员4人笔试历年参考题库附带答案详解
- 2025湖南冷链物流集团招聘法务主管1名笔试历年参考题库附带答案详解
- 2025年西安市工业合作联社下属企业招聘笔试笔试历年参考题库附带答案详解
- 2025年湖南邵阳市新宁县城乡建设发展集团有限公司招聘拟聘人员笔试历年参考题库附带答案详解
- 2025北京国际大数据交易所有限责任公司招聘笔试历年参考题库附带答案详解
- 2025中国电信股份有限公司淮南分公司实习生招募80人笔试历年参考题库附带答案详解
- 2025哈尔滨“丁香人才周”(春季)引才现场招聘活动模拟试卷及答案详解(典优)
- 2025年合肥滨投文化创意发展有限公司招聘3人考前自测高频考点模拟试题及参考答案详解1套
- 2025年一卷政治高考真题及答案
- 安静与智慧主题班会课件
- 云南民族大学附属高级中学2026届高三上学期联考(一)生物试卷(含答案)
- 2025至2030年中国包月视频点播行业市场竞争格局分析及投资方向研究报告
- 皮带机安全知识培训
- 零星维修工程施工组织设计方案方案
- 2025年汽车驾驶员(技师)考试试题及答案(含答案)
- 2025大连国际机场招聘25人笔试历年参考题库附带答案详解
- 2025年浙江铁塔招聘笔试备考题库(带答案详解)
- 2025年上海市(秋季)高考语文真题详解
- 《秘书文档管理第三版》课件第七章
评论
0/150
提交评论