C程序设计第二章基本数据类型及运算.ppt

上传人:max****ui 文档编号:8616633 上传时间:2020-03-30 格式:PPT 页数:83 大小:872KB
返回 下载 相关 举报
C程序设计第二章基本数据类型及运算.ppt_第1页
第1页 / 共83页
C程序设计第二章基本数据类型及运算.ppt_第2页
第2页 / 共83页
C程序设计第二章基本数据类型及运算.ppt_第3页
第3页 / 共83页
点击查看更多>>
资源描述
1 C的基本数据类型及运算 信息对抗 电子工程学院 2 标识符 标识符就是给程序中的变量 常量 函数 数组 结构体以及文件所起的名字 1 命名规则 以字母或下划线开头 由字母 数字和下划线组成 不能和系统关键字同名 尽量不要用下划线开头 一般对长度无规定 大小写是不同的字母 大小写敏感 例 判断下列标识符号合法性sumSumM D JohndayDate3daysstudent name 33lotus 1 2 3chara b above 123 M D John 3days 33 char 123 a b 3 标识符 好的命名习惯 尽量做到见名知意 sum area 变量名 函数名用小写 符号常量用大写 在易混淆地方 尽量避免用易认错的字母 e g 0 数字 大写字母 o 小写字母 1 数字 I I的大写字母 l L的小写字母 数字 大写字母 z 小写字母 4 KeywordsandReservedIdentifiers ISO ANSICKeywordsautoenumrestrictunsignedbreakexternreturnvoidcasefloatshortvolatilecharforsignedwhileconstgotosizeof Boolcontinueifstatic Complexdefaultinlinestruct Imaginarydointswitchdoublelongtypedefelseregisterunion 5 注意 1 C语言区分大小写 2 标识符的长度一般不超过32个字符 如 Price和price 系统会认为是两个不同的标识符 具体情况视不同系统而定 6 数据类型DataTypes 程序应包括两个方面的内容 数据的描述 操作步骤 即动作的描述 数据不仅是操作的对象 而且操作结果会改变数据的状况 计算机科学家沃思 NiklklausWirth 提出一个公式 数据结构十算法 程序 程序 算法十数据结构十程序设计方法十语言工具和环境 C语言中 任何一个数据都必须属于一种类型 7 C语言对程序中要用到的每一个变量都要事先指定它的数据类型 为什么要指定数据类型 1 不同类型的数据在内存中占据不同长度的存储区 2 不同类型的数据取值范围不同 3 不同类型的数据有不同的操作 8 C语言数据类型 数据类型 基本类型 构造类型 指针类型 字符型char整型int实型float double空类型void 枚举型enum数组 Array 结构体struct共用体union 9 基本数据类型 Void 1 函数不返回任何值2 同一类型指针 10 类型修饰符 除void类型以外 基本数据类型可以带各种修饰符作为前缀 右边四个修饰符可以用于字符型和整型的基本类型 Long还可以用于双精度实型 整型数缺省状态为signed和short 11 基本类型和修饰符组合 12 数据类型的缺省等价形式 13 浮点数 在计算机中 定点数通常只用于表示整数或纯小数 而对于既有整数部分又有小数部分的数 由于其小数点的位置不固定 一般用浮点数表示 由两部分组成 尾数和2的幂 地址 0 内容 SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM 1 2 3 说明 S 符号位 1代表负 0代表正E 偏移127的幂 为阶码 即多少次方的意思 M 24位尾数保存在23位中 最高位为1省略 则 浮点数的值 符号 2 EEEEEEEE 127 尾数 14 例 浮点数0 xC10 x480 x000 x00的值二进制表示 11000001010010000000000000000000符号位S 1 故为负数阶码 10000010 127 130 127 3尾数 1 10010000000000000000000 1 5625所以 其代表的值为 1 23 1 5625 12 5 15 常量constants Somedataarepresetbeforeaprogramisusedandkeeptheirvaluesunchangedthroughoutthelifeoftheprogram Theseareconstants 常量 具有固定的值 且不会被程序改变 数值常量 整型常量 实型常量 双精度常量字符常量字符串常量符号常量 16 整型常量 整型常量即整常数 整常数可用以下三种形式表示 十进制整数 如123 456 0 八进制整数 以0开头的数是八进制数 如0123表示八进制数123 即 123 8等于十进制数83 011表示八进制数 11 即十进制数 9 十六进制整数 以0 x开头的数是 进制数 如0 x123 代表16进制数123 即 123 16 1 16 2 16 3 160 256 32 3 291 0 x12等于十进制数一18 整型常量的类型根据其值所在范围确定其数据类型在整常量后加字母l或L 认为它是longint型常量 例12与12L 例30000为int型65536为longint型 17 实型常量 实数在 语言中又称浮点数 实数有两种表示形式 1 小数形式 它由数字和小数点组成 注意必须有小数点 0 123 123 123 0 123 0 0都是十进制数形式 2 指数形式 如123e3或123E3都代表123 103但注意字母e 或E 之前必须有数字 且e后面指数必须为整数 如e3 2 1e3 5 e等都不是合法的指数形式 规范化 标准化 指数形式 在字母e 或E 之前的小数部分中 小数点左边应有一位 且只能有一位 非零的数字 实型常量的类型默认double型在实型常量后加字母f或F 认为它是float型 18 定义 用单引号括起来的单个普通字符或转义字符 字符常量的值 该字符的ASCII码值 如 101 A 012 n 376 x61 a 60 0 483 例 A 101 x41 65 如 A 65 a 97 0 48 n 10 如 a A n 101 转义字符 反斜线后面跟一个字符或一个代码值表示 例转义字符举例main printf 101 x42C n printf Isay Howareyou n printf CProgram n printf Turbo C 运行结果 屏幕显示 ABCIsay Howareyou CProgram Turbo C 例main printf Y b n 运行结果 屏幕显示 打印机输出 字符常量 19 字符常量与字符串常量不同 定义 用双引号 括起来的字符序列存储 每个字符串尾自动加一个 0 作为字符串结束标志 例 charch ch A 字符串常量 20 举例 includemain printf abc tde nf tg n printf abc tde rf tg n printf h ti b bjk n printf h tijk n printf h12345678ijk n 21 ConstantsandtheCPreprocessor Sometimesyouneedtouseaconstantinaprogram Forexample youcouldgivethecircumferenceofacircleasfollows circumference 3 14159 diameter Here theconstant3 14159representstheworld famousconstantpi Touseaconstant justtypeintheactualvalue asintheexample However therearegoodreasonstouseasymbolicconstantinstead Thatis youcoulduseastatementsuchasthefollowingandhavethecomputersubstituteintheactualvaluelater circumference pi diameter 22 Whytouseasymbolicconstant First anametellsyoumorethananumberdoes Comparethefollowingtwostatements owed 0 015 housevalue owed taxrate housevalue Ifyoureadthroughalongprogram themeaningofthesecondversionisplainer Also supposeyouhaveusedaconstantinseveralplaces anditbecomesnecessarytochangeitsvalue Afterall taxratesdochange Thenyouonlyneedtoalterthedefinitionofthesymbolicconstant ratherthanfindandchangeeveryoccurrenceoftheconstantintheprogram 23 howsetupasymbolicconstant Onewayistodeclareavariableandsetitequaltothedesiredconstant Youcouldwritethis floattaxrate taxrate 0 015 Thisprovidesasymbolicname buttaxrateisavariable soyourprogrammightchangeitsvalueaccidentally Fortunately Chasacouplebetterideas 24 defineNAMEvalue Justaddalinelikethefollowingatthetopofthefilecontainingyourprogram defineTAXRATE0 015Whenyourprogramiscompiled thevalue0 015willbesubstitutedeverywhereyouhaveusedTAXRATE Thisiscalledacompiletimesubstitution 25 符号常量SymbolicConstants C语言允许将程序中的常量定义为一个标识符 称为符号常量 符号常量一般使用大写英文字母表示 以区别于一般用小写字母表示的变量 符号常量在使用前必须先定义 定义的形式是 define是宏定义预处理命令 不是C语句例如 definePI3 1415926 defineTRUE1 definrFALSE0 defineSTAR 这里定义PI TRUE FLASE STAR为符号常量 其值分别为3 1415926 1 0 26 compiletimesubstitution 27 examples The definestatementcanbeusedforcharacterandstringconstants too Justusesinglequotesfortheformeranddoublequotesforthelatter Thefollowingexamplesarevalid defineBEEP a defineTEE T defineESC 033 defineOOPS Nowyouhavedoneit Rememberthateverythingfollowingthesymbolicnameissubstitutedforit Don tmakethiscommonerror thefollowingiswrong defineTOES 20Ifyoudothis TOESisreplacedby 20 notjust20 Inthatcase astatementsuchasdigits fingers TOES isconvertedtothefollowingmisrepresentation digits fingers 20 28 The undefDirective The undefdirectivecancelsanearlier definedefinition Thatis supposeyouhavethisdefinition defineLIMIT400Thenthedirective undefLIMITremovesthatdefinition Now ifyoulike youcanredefineLIMITsothatithasanewvalue EvenifLIMITisnotdefinedinthefirstplace itisstillvalidtoundefineit 29 The undefDirective definePI3 14159Main undefPI 从此往后 PI不在代表3 14159 F1 30 带参数的宏定义 除了字符串的替换 还要进行参数替换 定义形式 defineS a b a b area S 3 2 预处理后 area 3 2 注意 1 宏名和参数之间不能有空格2 注意括号的使用 保证运算次序 31 TheconstModifier C90addedasecondwaytocreatesymbolicconstants usingtheconstkeywordtoconvertadeclarationforavariableintoadeclarationforaconstant constintMONTHS 12 MONTHSasymbolicconstantfor12ThismakesMONTHSintoaread onlyvalue Thatis youcandisplayMONTHSanduseitincalculations butyoucannotalterthevalueofMONTHS 32 概念 其值可以改变的量变量名与变量值变量定义的一般格式 数据类型变量1 变量2 变量n 变量初始化 定义时赋初值 例 inta b c floatdata 决定分配字节数和数的表示范围 合法标识符 例 inta 2 b c 4 floatdata 3 67 charch A intx 1 y 1 z 1 intx y z 1 X 变量的使用 先定义 后使用 例1intstudent stadent 19 Undefinedsymbol stadent infunctionmain 例2floata b c c a b Illegaluseoffloatingpointinfunctionmain 变量定义位置 一般放在函数开头 变量Variables 33 占字节数随机器不同而不同 一般占一个机器字short int long可用sizeof 类型标识符 测量 实型变量float 占4字节 提供7位有效数字double 占8字节 提供15 16位有效数字 字符型变量字符变量存放字符ASCII码char与int数据间可进行算术运算 例1 floata a 111111 111 a 111111 1 例2 doubleb b 111111 111 b 111111 111 例a D a 68 x A 5 x 65 5 s G s 33 71 没有字符串变量 用字符数组存放 整型变量 34 definePRICE12 5main intnum 3 floattotal charch1 ch2 D total num PRICE ch1 ch2 A a printf total f ch1 c n total ch1 运行结果 total 37 500000 ch1 d 例子 35 EnumeratedTypes declaresymbolicnamestorepresentintegerconstants Byusingtheenumkeyword youcancreateanew type andspecifythevaluesitmayhave Actually enumconstantsaretypeint thereforetheycanbeusedwhereveryouwoulduseanint Thepurposeofenumeratedtypesistoenhancethereadabilityofaprogram Forexample youcanmakethesedeclarations enumspectrum red orange yellow green blue violet enumspectrumcolor Theidentifierswithinthebracesenumeratethepossiblevaluesthataspectrumvariablecanhave establishesspectrumasatagname makescoloravariableofthattype 36 Usage Therefore thepossiblevaluesforcolorarered orange yellow andsoon Then youcanusestatementslikethefollowing intc color blue if color yellow for color red color violet color 37 EnumConstants Justwhatareblueandred Technically theyaretypeintconstants Forexample giventheprecedingenumerationdeclaration youcantrythis printf red d orange d n red orange Hereistheoutput red 0 orange 1Whathashappenedisthatredhasbecomeanamedconstantrepresentingtheinteger0 Similarly theotheridentifiersarenamedconstantsrepresentingtheintegers1through5 38 DefaultValues Bydefault theconstantsintheenumerationlistareassignedtheintegervalues0 1 2 andsoon Therefore thedeclarationenumkids nippy slats skippy nina liz resultsinninahavingthevalue3 AssignedValuesYoucanchoosetheintegervaluesthatyouwanttheconstantstohave Justincludethedesiredvaluesinthedeclaration enumlevels low 100 medium 500 high 2000 39 AssignedValues Ifyouassignavaluetooneconstantbutnottothefollowingconstants thefollowingconstantswillbenumberedsequentially Forexample supposeyouhavethisdeclaration enumfeline cat lynx 10 puma tiger Thencatis0 bydefault andlynx puma andtigerare10 11 and12 respectively 40 变量赋初值 1 初始化 在定义变量的同时为变量赋初值2 形式 类型标识符变量名 常量或常量表达式例如 intx 10 charch a 变量赋初值允许使用符号常量例如 definePI3 1415926 floatx PI 可对被定义的变量的一部分赋初值例如 inta b c 1 d 2 floatr 2 l s 可对几个变量赋以同一个初值例如 inta 6 b 6 c 6 不要写为 inta b c 6 41 初始化 初始化不是在编译阶段完成的 只有静态存储变量和外部变量的初始化是在编译阶段完成的 而是在程序运行时执行本函数时赋以初值的 相当于有一个赋值语句 例如 inta 3 相当于 inta 指定a为整型变量 a 3 赋值语句 将3赋予a 又如inta b c 5 相当于 inta b c 指定a b c为整型变量 c 5 将5赋给c 42 C运算符 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 及其扩展 条件运算符 逗号运算符 指针运算符 求字节数 sizeof 强制类型转换 类型 分量运算符 下标运算符 其它 Operators Expressions运算符和表达式 43 Operators Expressions运算符和表达式 运算符功能与运算量关系要求运算量个数要求运算量类型运算符优先级别结合方向结果的类型 学习运算符应注意 44 运算结合方向 两种 1 自左向右 为左结合性 2 自右向左 为右结合性 若在运算量的两侧的运算符有相同的优先级 则按它们的结合方向顺序处理 例 a b c 1 5 a d e a b c d 相当于 a b c d 45 ArithmeticOperators 加法 正值 如 3 6 3 减法 负值 如 6 4 5 乘法 如 3 8 除法 如 8 5 求余 如 7 4的值为3 46 基本算术运算符 结合方向 从左向右优先级 2 3 4 说明 可为单目运算符时 右结合性两整数相除 结果为整数 要求两侧均为整型数据 例5 2 5 2 0 例5 2 5 2 1 10 5 1 5 5 2 算术运算符和表达式 2 2 5 1 1 1 0 47 DivisionOperator integerdivision 两个整数相除结果为整数 如5 3的结果值为1 舍去小数部分 但是如果除数或被除数中有一个为负值 则舍入的方向是不固定的 例如 5 3在有的机器上得到结果 1 有的机器则给出结果 2 多数机器采取 向零取整 方法 即5 3 1 5 3 1 取整后向零靠拢 如果参加运算的两个数中有一个数为实数 则结果是double型 因为所有实数都按double型进行运算 48 Thedivide cProgram divide c divisionswehaveknown includeintmain void printf integerdivision 5 4is d n 5 4 printf integerdivision 6 3is d n 6 3 printf integerdivision 7 4is d n 7 4 printf floatingdivision 7 4 is 1 2f n 7 4 printf mixeddivision 7 4is 1 2f n 7 4 return0 integerdivision 5 4is1integerdivision 6 3is2integerdivision 7 4is1floatingdivision 7 4 is1 75mixeddivision 7 4is1 75 Noticehowintegerdivisiondoesnotroundtothenearestinteger butalwaystruncates thatis discardstheentirefractionalpart 49 ModulusOperator usedinintegerarithmetic givestheremainderthatresultswhentheintegertoitsleftisdividedbytheintegertoitsright Forexample 13 5 readas 13modulo5 hasthevalue3 because5goesinto13twice witharemainderof3 Don tbothertryingtousethisoperatorwithfloating pointnumbers Itjustwon twork Onecommonuseistohelpyoucontroltheflowofaprogram 50 Whataboutnegativenumbers BeforeC99settledonthe truncatetowardzero ruleforintegerdivision therewereacoupleofpossibilities Butwiththeruleinplace yougetanegativemodulusvalueifthefirstoperandisnegativeandapositivemodulusotherwise 11 5is2and11 5is111 5is 2and11 2is1 11 5is2and 11 5is 1 11 5is 2and 11 5is 1Ifyoursystemshowsdifferentbehavior ithasn tcaughtuptotheC99standard Inanycase thestandardsays ineffect thatifaandbareintegervalues youcancalculatea bbysubtracting a b bfroma Forexample youcanevaluate 11 5 thisway 11 11 5 5 11 2 5 11 10 1 51 IncrementandDecrementOperators and Theincrementoperatorperformsasimpletask itincrements increases thevalueofitsoperandby1 Thisoperatorcomesintwovarieties Thefirstvarietyhasthe comebeforetheaffectedvariable thisistheprefixmode Thesecondvarietyhasthe aftertheaffectedvariable thisisthepostfixmode Thetwomodesdifferwithregardtotheprecisetimethattheincrementingtakesplace 52 自增 自减运算符 and 作用是使变量的值增1或减1 如 i i 在使用i之前 先使i的值加 减 1 i i 在使用i之后 使i的值加 减 l i和i 的作用相当于i i 1 但 i和i 不同之处在于 i是先执行i i 1后 再使用i的值 而i 是先使用i的值后 再执行i i 1 如果i的原值等于3 则 j i j的值为4 j i j的值为3 然后i变为4又如 i 3 printf d i 输出 4 若改为printf d i 则输出 3 例j 3 k j j 3 k j j 3 printf d j j 3 printf d j a 3 b 5 c a b a 3 b 5 c a b k 4 j 4 k 3 j 4 4 3 c 20 a 4 c 15 a 4 53 说明 只能用于变量 不能用于常量或表达式 如5 或 a b 都是不合法的 因为5是常量 常量的值不能改变 a b 也不可能实现 假如a b的值为5 那么自增后得到的6放在什么地方呢 无变量可供存放 的结合方向是 自右至左 运算符的结合方向为 自左而右 是大家所熟知的 如果有 i i的左面是负号运算符 右面是自加运算符 如果i的原值等于3 若按左结合性 相当于 i 而 i 是不合法的 对表达式不能进行自加自减运算 自增 减 运算符常用于循环语句中使循环变量自动加1 也用于指针变量 使指针指向下一个地址 54 说明 结合方向 自右向左优先级 2 3 4 例 i i i 3 printf d i 3 例 i i 3 printf d i 55 AssignmentOperator InC theequalsigndoesnotmean equals Rather itisavalue assigningoperator Thestatementbmw 2002 assignsthevalue2002tothevariablenamedbmw Thatis theitemtotheleftofthe signisthenameofavariable andtheitemontherightisthevalueassignedtothevariable The symboliscalledtheassignmentoperator Again don tthinkofthelineassaying bmwequals2002 Instead readitas assignthevalue2002tothevariablebmw Theactiongoesfromrighttoleftforthisoperator 56 Thestatementi i 1 i i 1 Asmathematics thisstatementmakesnosense Ifyouadd1toafinitenumber theresultisn t equalto thenumberyoustartedwith butasacomputerassignmentstatement itisperfectlyreasonable Itmeans Findthevalueofthevariablenamedi tothatvalue add1 andthenassignthisnewvaluetothevariablenamedi 57 Modifiablelvalue Astatementsuchas2002 bmw makesnosenseinC indeed isinvalid because2002isjustaconstant Youcan tassignavaluetoaconstant Whenyousitdownatthekeyboard rememberthatitemtotheleftofthe signmustbethenameofavariable Actually theleftsidemustrefertoastoragelocation Thesimplestwayistousethenameofavariable but asyouwillseelater a pointer canbeusedtopointtoalocation Moregenerally Cusesthetermmodifiablelvaluetolabelthoseentitiestowhichyoucanassignvalues 58 DataObjects Lvalues Rvalues andOperands Adataobjectisageneraltermforaregionofdatastoragethatcanbeusedtoholdvalues Cusesthetermlvaluetomeananameorexpressionthatidentifiesaparticulardataobject Thenameofavariableisanlvalue soobjectreferstotheactualdatastorage butlvalueisalabelusedtoidentify orlocate thatstorage Notallobjectscanhavetheirvalueschanged soCusesthetermmodifiablelvaluetoidentifyobjectswhosevaluecanbechanged leftsideofanassignmentoperatorshouldbeamodifiablelvalue L Left 59 rvalue referstoquantitiesthatcanbeassignedtomodifiablelvalues Forinstance considerthefollowingstatement bmw 2002 Here bmwisamodifiablelvalue and2002isanrvalue Asyouprobablyguessed therinrvaluecomesfromright Rvaluescanbeconstants variables oranyotherexpressionthatyieldsavalue Aslongasyouarelearningthenamesofthings thepropertermforwhatwehavecalledan item asin theitemtotheleftofthe isoperand Operandsarewhatoperatorsoperateon 60 tripleassignment golf c golftournamentscorecard includeintmain void intjane tarzan cheeta cheeta tarzan jane 68 printf cheetatarzanjane n printf Firstroundscore 4d 8d 8d n cheeta tarzan jane return0 Theassignmentsaremaderighttoleft Firstjanegetsthevalue68 andthentarzandoes andfinallycheetadoes Therefore theoutputisasfollows cheetatarzanjaneFirstroundscore686868 61 简单赋值运算符 符号 格式 变量标识符 表达式作用 将一个数据 常量或表达式 赋给一个变量 例a 3 d func c d 2 62 复合赋值运算符 种类 含义 exp1op exp2 exp1 exp1opexp2 63 复合赋值运算符 结合方向 自右向左优先级 14左侧必须是变量 不能是常量或表达式 赋值表达式的值与变量值相等 且可嵌套 赋值转换规则 使赋值号右边表达式值自动转换成其左边变量的类型 例3 x 2 y a b 3 例floatf inti i 10 f i 则f 10 0 例inti i 2 56 结果i 2 例 a b c 5a b 5 a 5 c 6 a b 4 c 6 a b 10 c 2 表达式值为5 a b c值为5 b 5 a 5 表达式值11 c 6 a 11 表达式值10 a 10 b 4 c 6 表达式值5 a 5 b 10 c 2 说明 1 64 复合赋值运算符 结合方向 自右向左优先级 14左侧必须是变量 不能是常量或表达式赋值转换规则 使赋值号右边表达式值自动转换成其左边变量的类型赋值表达式的值与变量值相等 且可嵌套 例 a 12 a a a a 例 inta 2 a 4 1 a a a a 3 a 264等价于a a a a a a a 0等价于a a a a a a a a 3 说明 2 65 关系运算符种类 结合方向 自左向右优先级别 例c a b c a b a b c a b ca bc a b c 关系表达式的值 是逻辑值 真 或 假 用1和0表示 例inta 3 b 2 c 1 d f a b a b cb cbf a b c 表达式值1 表达式值1 表达式值0 d 1 f 0 关系运算符和表达式 66 例若a 0 b 0 5 x 0 3 则a x b的值为 0 例5 2 7 8在C中是允许的 值为 0 例inti 1 j 7 a a i j 4 0 则a 2 例 a 0结果为 A 100结果为 1 0 关系运算注意 1 67 例注意区分 与 inta 0 b 1 if a b printf aequaltob elseprintf anotequaltob 例应避免对实数作相等或不等的判断如1 0 3 0 3 0 1 0结果为可改写为 fabs 1 0 3 0 3 0 1 0 1e 6 0 关系运算注意 2 68 逻辑运算符和表达式 1 逻辑运算符种类 逻辑运算真值表 C语言中 运算量 0表示 假 非0表示 真 运算结果 0表示 假 1表示 真 69 例ab x ya b x y a a b 优先级 结合方向 a x x b a b x y a b x y a a b 逻辑运算符和表达式 2 70 优先级 结合方向 例a 4 b 5 aa ba b a b4 0 25 3 2 8 4 0 c d 值为1 值为0 值为1 值为1 值为1 值为1 5 3 2 8 4 0 值为1 逻辑运算符和表达式 3 71 优先级 结合方向 短路特性 逻辑表达式求解时 并非所有的逻辑运算符都被执行 只是在必须执行下一个逻辑运算符才能求出表达式的解时 才执行该运算符 例a m a b n c d 结果m 0 n 1 逻辑运算符和表达式 4 72 C语言中提供的位运算符 1 按位取反运算符 形式 A功能 把A的各位都取反 即0变1 1变0 例如 intA 179 位运算 73 形式 A B功能 对A的各位与B的对应位进行比较 如果两者都为1 A B对应位上的值为1 否则为0 例如 intA 179 二进制10110011 intB 169 二进制10101001 按位与运算符 74 形式 A B功能 对A的各位与B的对应位进行比较 如果两者中有一个为1 A B对应位上的值为1 否则为0 例如 intA 179 二进制10110011 intB 169 二进制10101001 按位或运算符 75 形式 A B功能 对A的各位与B的对应位进行比较 如果两者不同 A B对应位上的值为1 否则为0 例如 intA 179 二进制10110011 intB 169 二进制10101001 按位异或运算符 76 形式 A n 其中n为一个大于0的整型表达式 功能 把A的值向左移动n位 右边空出的n位用0填补 当左移时移走的高位中全都是0时 相当于对A作n次乘以2的运算 例如 intA 27 二进制00011011 左移运算符 77 形式 A n 其中n为一个大于0的整型表达式 功能 把A的值向右移动n位 左边空出的n位用0填补 相当于对A作n次除以2的运算 例如 intA 179 二进制10110011 右移运算符 78 一般形式 expr1 expr2 expr3执行过程功能 相当于条件语句 但不能取代一般if语句 例求a b printf a b d n b 0 a b a b 例 a b Y N x 2 1 1 0 x 0 x x c a c z c a A c 条件运算符可嵌套如x 0 1 x 0 1 0 优先级 13 结合方向 自右向左如a b a c d c d a b a c d c d expr1 expr2 expr3类型可不同 表达式值取较高的类型 例x a b x 0 表达式值为 b x 0 表达式值为 a x y 1 1 5 x y 值为1 0 x y 值为1 5 条件运算符与表达式 79 逗号运算符和逗号表达式 1 逗号运算符 即 优先级 为所有运算符中级别最低的 15 结合性 从左向右2 逗号表达式形式 表达式1 表达式2 表达式n求解过程 顺次求解表达式1 表达式2 最后求解表达式n 逗号表达式的值为表达式n的值 用途 常用于循环for语句中例如 a 3 5 a 4a 3 5 a 4 a 5x a 4 3 a 1 a 10 80 逗号运算符和逗号表达式 例a 3 5 a 4a 3 5 a 4 a 5例x a 3 6 3 x a 3 6 a例a 1 b 2 c 3 printf d d d a b c printf d d d a b c b c a 15 表达式值60 a 15 表达式值20 赋值表达式 表达式值18 x 18 逗号表达式 表达式值18 x 3 1 2 3 3 2 3 例 includemain intx y 7 floatz 4 x y y 6 y z printf x d n x 运行结果 x 3 81 不同类型数据间的转换 隐式转换什么情况下发生运算转换 不同类型数据混合运算时赋值转换 把一个值赋给与其类型不同的变量时输出转换 输出时转换成指定的输出格式函数调用转换 实参与形参类型不一致时转换运算转换规则 不同类型数据运算时先自动转换成同一类型 82 charch inti floatf doubled ch i f d f i 10 a i f d l 例2 inti floatf doubled longl 例1 83 显式转换 强制转换TheCastOperator 一般形式 类型名 表达式 例 int x y int x y double 3 2 int 3 6说明 强制转换得到所需类型的中间变量 原变量类型不变 例main floatx inti x 3 6 i int x printf x f i d x i 结果 x 3 600000 i 3 精度损失问题
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!