




已阅读5页,还剩55页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 Chapter2C SyntaxandSemantics andtheProgramDevelopmentProcess 2 KnowledgeGoals 知识要点 TounderstandhowaC programiscomposedofoneormoresubprograms function Toknowwhatametalanguage 元语 is andhowitisused Tounderstandtheconceptofadatatype 3 2 1TheElementsofC Program theremustbeafunctioncalledmain executionalwaysbeginswiththefirststatementinfunctionmain anyotherfunctionsintheprogramaresubprogramsandarenotexecuteduntiltheyarecalled 2 1 1ConstructtheC program 程序构成 4 ProgramWithSeveralFunctions mainfunction squarefunction cubefunction 5 includeusingnamespacestd intSquare int declaresthesetwointCube int value returningfunctionsintmain cout Thesquareof27is Square 27 endl functioncallcout Thecubeof27is Cube 27 endl functioncallreturn0 intSquare intn returnn n intCube intn returnn n n OutputofprogramThesquareof27is729Thecubeof27is19683 include 6 ShortestC Program intmain return0 typeofreturnedvalue nameoffunction saysnoparameters 7 Question TheMain functioncallstheotherfunctions Whocallsthemain functionandtowhomthereturnvalueisdeliveredbythemain function 8 2 1 2BasicConcepts Syntax 语法 Theformalrulesgoverninghowvalidinstructionsarewritteninaprogramminglanguage 指示如何有效的用编程语言撰写指令的正式规则 Semantics 语义 Thesetofrulesthatdeterminesthemeaningofinstructionswritteninaprogramminglanguage 9 Alanguagethatcandefineacomputerprogramminglanguage whichthecomputermayusetoverifythevalidity 合法性 ofasourceprogram Metalanguage 元语 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 10 Anidentifieristhenameassociatedwithadataobject avariableoraconstant orafunction ItisusedtorefertothatdataobjectorfunctioninaC program 标识符是一个和数据对象或函数相关联的名字 C isacase sensitivelanguage 区分大小写 usingmeaningfulidentifiersisagoodprogrammingpractice 用有意义的名字作为标识符是一个好习惯 2 1 3Identifier 标识符 11 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 2 1 3Identifier 标识符 IdentifierSyntaxTemplates 标识符语法模板 Theshadingindicatesapartofthedefinitionthatisoptional 阴影表明该定义的部分是可选的 12 anidentifiermuststartwithaletterorunderscore 字符或下划线 andbefollowedbyzeroormoreletters A Z a z digits 0 9 orunderscoresVALIDage of dogtaxRateY2KPrintHeadingageOfHorseNOTVALID Why age 2000TaxRateAge Of Catint 2 1 3Identifier 标识符 13 AwordthathasspecialmeaninginC itcannotbeusedasprogrammerdefinedidentifier 2 1 4C Reservedword 保留字 14 autobreakcasecharclassconstcontinuedefaultdeleteelseenumexplicitexternfloatforfriendgotoifinlineintlongnewoperatorprivateprotectedpublicregisterreturnshortsignedsizeofstaticstructswitchthistypedefunionunsignedvirtualvoidwhile 2 1 4C Reservedword 保留字 15 Case sensitivelanguage 区分大小写的语言 InC uppercaselettersaredifferentfromlowercaseletters inti intI iandIaredifferentidentifiers 16 2 1 5DataandDatatype 数据和数据类型 Built inType 基本类型或标准类型 Intcharfloatdoublevoid User definedType 用户自定义类型 enumstructunionclass 17 1 charDataType 字符型 Eachcharacterisenclosedinsinglequotes A a 8 Thesequence orpredefinedorderofallthecharacters makesitpossibletocomparetwodataofchartype a b 0 1 18 2 stringDataType 字符串类型 astringisasequenceofcharactersenclosedindoublequotesstringsamplevalues Hello Year2000 1234 theemptystring nullstring containsnocharactersandiswrittenas 19 MoreAboutString stringisnotabuilt in standard typeitisaprogrammer defineddatatypeitisprovidedintheC standardlibrarystringoperationsincludecomparing2stringvalues 串比较 searchingastringforaparticularcharacter 在串中查找特定的字符 joiningonestringtoanother 串的连接 20 Astatementthatassociates 关联 anidentifierwithadataobject afunction oradatatypesothattheprogrammercanrefertothatitembyname intNum floatx1 3 VariableDeclarations 变量声明 21 Avariableisalocationinmemorywhichwecanrefertobyanidentifier andinwhichadatavaluethatcanbechangedisstored 变量是指定类型的存储空间并以指定标识符命名 存储的变量值可以改变 declaringavariablemeansspecifyingbothitsnameanditsdatatype 声明变量意味着指定了它的名字和数据类型 Datatypeconversionmaybeperformed 可以进行类型转换 3 VariableDeclarations 变量声明 22 WhatisaLiteralValue 字面常量 Anyconstantvaluewritteninaprogram A C Programming 168 23 4 ConstantDeclarations 声明符号常量 Anamedconstantisalocationinmemorythatwecanrefertobyanidentifier andinwhichadatavaluethatcannotbechangedisstored constfloatNORMAL TEMP 98 6 constcharBLANK constintVOTING AGE 18 constfloatMAX HOURS 40 0 24 DifferencebetweenLiteralValueandNamedConstant字面常量和符号常量的区别 NamedConstant Alocationinmemory referencedbyanidentifier thatcontainsadatavaluethatcannotbechanged LiteralValue Anyconstantvaluewritteninaprogram 25 ConventionsofNamingIdentifiers 本教材标识符的命名约定 Variables beginwithalowercaseletter andcapitalizeeachsuccessiveword 小写字母开头 lengthInYardsProgrammer definedfunctions beginwithacapitalletter 大写字母开头 butmainfunctionisallinlowercaseletters CalculatePayment Namedconstants capitalizeeveryletteranduseunderscoretoseparatethewords 大写每一个字符 用下划线分割每一个词 BOOK TITLE 26 Astatementthatstoresthevalueofanexpressionintoavariable 5 AssignmentStatement 赋值语句 SyntaxTemplate Variable Expression Anexpressionisavalidarrangementofvariables constants andoperators 27 Youcanassign give avaluetoavariablebyusingtheassignmentoperator VariableDeclarationsstringfirstName charmiddleletter charletter intage ValidAssignmentStatementsfirstName Fido middleletter X letter middleletter age 12 1 Expressiononrightisevaluated 2 TheresultingvalueisstoredinthememorylocationofVariableonleft NOTE Anautomatictypecoercionoccursafterevaluationbutbeforethevalueisstored ifthetypesdifferforExpressionandVariable ExecutionofAssignment 赋值语句的执行 Variable Expression 29 6 StringConcatenation 字符串连接 concatenationisabinaryoperationthatusesthe operatoratleastoneoftheoperandsmustbeastringvariableornamedconstant theotheroperandcanbestringtypeorchartype 30 ConcatenationExample 字符串连接举例 conststringWHEN Tomorrow stringmessage1 stringmessage2 message1 Yesterday message2 and message1 message1 message2 WHEN 31 7 OutputStatements 输出语句 variablecoutispredefinedtodenoteanoutputstreamthatgoestothestandardoutputdevice displayscreen 变量cout已经定义为和标准输出设备 如 显示器 相联系的输出流 theinsertionoperator called putto takes2operands 插入操作符有两个操作数 theleftoperandisastreamexpression suchascout Therightoperandisanexpressionofsimpletypeorastringconstant 左操作数是流表达式 如cout 右操作数是一个简单类型表达式或字符串常量 32 Example cout Theansweris cout 3 4 endl cout Theansweris 3 4 SyntaxTemplates 33 AtthebeginningoftheprogramVariabledeclarationBeforeeachmajorstep functionblocks Wherethestatementsaredifficulttoread 8 AddingCommentstoprogram 为程序添加注释 34 8 AddingCommentstoprogram 为程序添加注释 C commentscomeintwoforms Thefirstisanysequenceofcharactersenclosedbythe pair Thesecondformsbeginswithtwoslashes 1 thisisanexample 2 thisisanexample 35 2 2ProgramConstruction 程序结构 Thesyntaxtemplateforaprogramlookslikethis Program 36 1 FunctionDefinition 函数定义 Afunctiondefinitionconsistsofthefunctionheadinganditsbody 37 1 FunctionDefinition 函数定义 Afunctiondefinitionconsistsofthefunctionheadinganditsbody Example doublemax doublex doubley if x y returnx elsereturny 38 1 FunctionDefinition 函数定义 Afunctiondefinitionconsistsofthefunctionheadinganditsbody Example voidprintmessage cout Howdoyoudo endl 39 2 Block CompoundStatement 块语句 Thisisthesyntaxtemplateforablock Ablockisjustasequenceofzeroormorestatementsenclosedbya pair 40 Statementsyntaxtemplate NullStatementDeclarationAssignmentStatementOutputStatementBlock 41 3 TheC Preprocessor 预处理 Afilewhosenameappearsinan includedirectiveiscalledaheaderfile Headerfilescontainconstant variable datatype andfunctiondeclarationneededbyaprogram include includeusingnamespacestd 42 3 TheC Preprocessor 预处理 Afilewhosenameappearsinan includedirectiveiscalledaheaderfile Headerfilescontainconstant variable datatype andfunctiondeclarationneededbyaprogram include include 43 compiler 3 TheC Preprocessor 预处理 44 4 AnIntroductiontoNamespace 名空间介绍 Theheaderfileiostream infact everystandardheaderfile declaresallitsidentifierstobeinanamespacecalledstd Wecanaccessanidentifierthatishiddeninsideanamespacewithtwooptions 45 4 AnIntroductiontoNamespace 名空间介绍 includeintmain std cout good std endl return0 Firstoptionexample 46 4 AnIntroductiontoNamespace 名空间介绍 Secondoptionexample includeusingnamespacestd intmain cout good endl return0 47 includeusingnamespacestd intSquare int declaresthesetwointCube int value returningfunctionsintmain cout Thesquareof27is Square 27 endl functioncallcout Thecubeof27is Cube 27 endl functioncallreturn0 intSquare intn returnn n intCube intn returnn n n 48 includeusingnamespacestd intSquare int declaresthesetwo intCube int value returningfunctionsintmain cout Thesquareof27is Square 27 endl functioncallcout Thecubeof27is Cube 27 endl functioncallreturn0 intSquare intn returnn n intCube intn returnn n n include 49 2 3Moreaboutoutput MovetoanewlinetooutputThemanipulator 操纵符 endlcanmaketheoutputonthescreenmovetothebeginningofanewline OutputadoublequoteinastringIfadoublequoteisoutputinastring itmustbewri
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025吉林松原市教育局直属学校招聘教育部直属六所师范大学应届毕业生44人模拟试卷(含答案详解)
- 2025年春季中国邮政储蓄银行上海分行校园招聘模拟试卷附答案详解
- 2025广西贵港市公安局港南分局面向社会招聘警务辅助人员16人模拟试卷附答案详解(黄金题型)
- 2025年中国黄虫灯行业市场分析及投资价值评估前景预测报告
- 2025年中国环氧浇注层压树脂行业市场分析及投资价值评估前景预测报告
- 2025江西吉安市泊士停车管理有限公司万安分公司派遣人员招聘1人考前自测高频考点模拟试题及答案详解(典优)
- 2025金沙县城乡建设发展集团有限公司模拟试卷及答案详解(夺冠系列)
- 2025年安徽中医药大学第二附属医院博士人才招聘4人考前自测高频考点模拟试题参考答案详解
- 2025北京大学电子学院招聘劳动合同制1人模拟试卷及1套完整答案详解
- 2025春季上海建工集团校园招聘正式启动考前自测高频考点模拟试题附答案详解(完整版)
- 幼儿园大班艺术课件:《国旗国旗红红的哩》
- 医院感染相关法律法规培训课件
- 中考数学解题的思维模式设计与分析探讨
- 头部手术备皮方法
- 企业内部控制培训课件完整版
- 气瓶检验员考试题库
- 五年级上册生命与健康教案
- 学位申请书单位评语
- 新能源汽车火灾事故处置程序及方法
- 九年级语文上册-谈骨气-吴晗-课件
- 教育专业的大学生职业规划书
评论
0/150
提交评论