C语言基础教程课件(英文版)ch.ppt_第1页
C语言基础教程课件(英文版)ch.ppt_第2页
C语言基础教程课件(英文版)ch.ppt_第3页
C语言基础教程课件(英文版)ch.ppt_第4页
C语言基础教程课件(英文版)ch.ppt_第5页
已阅读5页,还剩41页未读 继续免费阅读

VIP免费下载

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论