第2章数据类型运算符和表达式(完整).ppt

上传人:本田雅阁 文档编号:2252587 上传时间:2019-03-11 格式:PPT 页数:97 大小:1.01MB
返回 下载 相关 举报
第2章数据类型运算符和表达式(完整).ppt_第1页
第1页 / 共97页
第2章数据类型运算符和表达式(完整).ppt_第2页
第2页 / 共97页
第2章数据类型运算符和表达式(完整).ppt_第3页
第3页 / 共97页
亲,该文档总共97页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《第2章数据类型运算符和表达式(完整).ppt》由会员分享,可在线阅读,更多相关《第2章数据类型运算符和表达式(完整).ppt(97页珍藏版)》请在三一文库上搜索。

1、第2章 数据类型、运算符与表达式,南京审计学院 信息科学与技术学院 孙玉星,C程序设计,本章学习内容,标识符命名; 变量和常量; 数据类型;(整型、浮点型、字符型) 常用运算符和表达式;3.34.2 运算符的优先级与结合性,C Program Structure,例2.1:一个简单的C程序例子,#include /*函数功能:计算两个整数相加之和 入口参数:整型数据a和b 返回值: 整型数a和b之和 */ int Add(int a, int b) return (a + b); /*主函数*/ main() int x, y, sum = 0; printf(“Input two integ

2、ers:“); scanf(“%d%d“, /*输出x和y相加之和*/ ,并列的两个函数 其中一个是 程序的入口,程序注释,编译预处理命令,C程序常见符号分类,关键字(Keyword) 又称保留字( C Reserved Word ) A word that has special meaning in C 标识符(Identifier) C Standard Identifier(系统预定义标识符) A word having special meaning but may be redefined (but is not recommended!) 用户自定义标识符 变量,函数名,,C程序

3、常见符号分类,运算符(Operator) 34种,详见附录C 分隔符(Separator) 空格、回车/换行、逗号等 其它符号 “”和“”标识函数体或语句块 “/*”和“*/”程序注释的定界符 常量(Constant),标识符命名,变量名,函数名 由英文字母、数字和下划线组成,大小写敏感 不可以是数字开头 直观,见名知意,便于记忆和阅读 最好使用英文单词或其组合 切忌使用汉语拼音 下划线和大小写通常用来增强可读性 variablename variable_name variableName 不允许使用关键字作为标识符的名字 int, float, for, while, if等 某些功能的变

4、量采用习惯命名 如:for语句所采用的循环变量习惯用i, j, k,Windows 风格,UNIX 风格,何谓变量(Variable )?,A name associated with a memory cell whose value can change,如何衡量变量所占空间大小?,bit,中文叫法:位 Byte,中文叫法:字节 Kilobyte(KB),中文叫法: K Megabyte(MB),中文叫法:兆 Gigabyte(GB),中文叫法:G Terabyte(TB),中文叫法:T,1 TB = 1,024 GB,1 GB = 1,024 MB,1 MB = 1,024 KB,1 K

5、B = 1,024 B,1 B = 8 b,一个位有多大? 只能是“0”或者“1”,二进制 一个字节有多大? 可以表示数字0255之间的整数 保存一个字符(英文字母、数字、符号) ASCII(美国标准信息交换码)编码(附录A),如何衡量变量所占空间大小?,Needs to be declared: 变量类型 变量名;,Example: int sum; int x,y,sum=0;,变量声明(Variable Declaration),变量声明(Variable Declaration),使用变量的基本原则 变量必须先定义,后使用 所有变量必须在第一条可执行语句前定义 声明的顺序无关紧要 一条

6、声明语句可声明若干个同类型的变量 声明变量是初始化变量的最好时机 不被初始化的变量,其值为随机数,结果会是什么?,Example: int number1, number2; number1 = 25; number2 = 23; number1 = number2; ,25,23,23,变量赋值(Variable Assignment),Algorithm 变量 表达式 Syntax 变量 = 表达式 ; Rules:类型一致 Expressions type must be the same as variables type,Valid Example: Invalid Example:

7、 int x; int y; x = 12; y = 5.75;,变量赋值(Variable Assignment),Example: Calculate and display the price of a number of apples if the quantity in kg and price per kg are given. Input: quantity and pricePerkg Output: price Process: price = quantity * pricePerkg,变量赋值(Variable Assignment),Example: int quant

8、ity; float pricePerkg, price; quantity = 5; pricePerkg = 4.50; price = quantity * pricePerkg; ,How does this program work?,变量赋值(Variable Assignment),Example: int quantity; float pricePerkg, price; quantity = 2; pricePerkg = 4.50; price = quantity * pricePerkg; ,4.50,9.00,2,变量赋值(Variable Assignment),

9、Example: int quantity; float pricePerkg; float price;,变量类型(Variable Type),数据类型(Data Type),为什么要区分类型? 不同类型有什么不同? 数据表示形式 合法的取值范围 占用内存空间大小 可参与的运算种类,数据类型(Data Type),基本数据类型,int 整数,在目前绝大多数机器上占4个字节 TC2.0,2个字节 float 单精度浮点数,4个字节 double 双精度浮点数,8个字节 char 字符,1个字节 表示256个ASCII字符,或0255的整数,数据类型修饰符,short short int,简写

10、为short,短整数,2个字节 long long int,简写为long,长整数,4个字节 long double,长双精度(高精度)浮点数,10个字节 unsigned 用来修饰char、int、short和long 无符号整数(正整数和0),不同类型取值范围不同,C语言直接提供的任何类型都有取值范围。 P22,整型类型的取值范围,浮点类型的取值范围,不同类型取值范围不同,C语言直接提供的任何类型都有取值范围。 当向其赋超过此范围的数值时,结果会怎样呢? 产生数值类型溢出,得到一个不正确的结果。,小蛇能吞下大象吗?,typeoverflow.c,何谓类型溢出(Overflow)?,生活中的

11、例子: 千年虫问题 阿利亚娜号火箭发射失败 现象与危害: 溢出后的数值是可预料的,但不同平台会有所不同 当程序从高位计算机向低位计算机移植(比如从64位系统移植到32位系统)时,以前从不出现的溢出问题可能出现,during execution of a data conversion from 64-bit floating point to 16-bit signed integer value. The floating point number which was converted had a value greater than what could be represented b

12、y a 16-bit signed integer. This resulted in an Operand Error.,解决方案?,预先估算运算结果的可能范围,采用取值范围更大的类型。 1+2+3+ 1!+2!+3!+ 13+23+33+ 在运算还没开始之前就判断运算数是否在合理的取值范围内。如果超出,则停止运算,转错误处理。,100! = 9.3326215443944 * 10157,13+23+n3=(1+2+3+n)2=(n(n+1)/2)2 =5050*5050,不同类型占用的内存字节数不同,因为 同种类型在不同的平台其占字节数不尽相同。如int在16位、32位和64位系统分别占

13、2、4和8个字节。 不要对变量所占的内存空间字节数想当然 用sizeof获得变量或者数据类型的长度 现象与危害: 在平台间移植时会出现问题,导致数据丢失或者溢出,注意!,sizeof到底是什么?,C语言的关键字,并非函数 计算类型占用的字节数 两种语法形式 sizeof(类型) 结果为类型占用的字节数 sizeof(表达式) 结果为表达式值所属类型占用的字节数 一般都使用sizeof(变量名),现场演示例2.3 在TC和VC的运行结果,#include main() printf(“Data type Number of bytesn“); printf(“- -n“); printf(“ch

14、ar %dn“, sizeof(char); printf(“int %dn“, sizeof(int); printf(“short int %dn“, sizeof(short); printf(“long int %dn“, sizeof(long); printf(“float %dn“, sizeof(float); printf(“double %dn“, sizeof(double); ,不同类型数据 在内存中的存储形式不同,字符型 整型 实型 N=S2j,所占位数决定 实数的取值范围,所占位数决定 实数的精度,常量(Constant),A value that will not

15、 change Consists of: 整型(e.g. 0 67 -2 123L 123u 022 0x12) 缺省为int 实型(e.g. 2.3 1.2e-5 2.73F 2.73L) 缺省为double 字符型(e.g. z 3 $ n ) 用开头的字符为转义字符, 代表1个字符 字符串(e.g. “UKM“ “1“ “5a“ ),字符常量,转义字符 一些特殊字符(无法从键盘输入或者另有它用)用转义字符表示,字符常量,字符常数就是一个普通整数,也可参与各种数学运算 每个字符具有一个0255之间的数值,可从ASCII表查出 注意:5和整数5的区别 5的ASCII码值是53 字符的数学运算

16、在密码学内 用得比较多,例2.5:小写字母转换为大写字母,#include main() char ch = b; printf(“%c, %dn“, ch, ch); ch = b - 32; printf(“%c, %dn“, ch, ch); ,#include main() char ch = b; printf(“%c, %dn“, ch, ch); ch = b - (a - A) ; printf(“%c, %dn“, ch, ch); ,b, 98 B, 66,b, 98 B, 66,相当于97-65,字符串常量,用双引号括住的由0个或多个字符组成的字符序列 “I am a s

17、tring“ “表示空字符串 除注释外,是唯一可以出现中文的地方 C语言内部用0表示字符串的结束 “x“和x是不同的 里定义了一系列专门的字符串处理函数 转义字符也可在字符串中使用 字符串“t“NameAddressn“的长度? 15,宏常量,#define 标识符 字符串 宏常量 也称符号常量 一般采用全大写字母表示 宏定义不是语句,而是一种编译预处理命令,例2.2 :计算圆的周长和面积,#include #define PI 3.14159 #define R 5.3 main() printf(“area = %fn“, PI * R * R); printf(“circumferenc

18、e = %fn“, 2 * PI * R); ,area = 88.247263 circumference = 33.300854,相当于执行 #include main() printf(“area = %fn“, 3.14159 * 5.3 * 5.3); printf(“circumference = %fn“, 2 * 3.14159 * 5.3); ,宏替换,例2.2 :计算圆的周长和面积,#include #define PI 3.14159; #define R 5.3; main() printf(“area = %fn“, PI * R * R); printf(“circ

19、umference = %fn“, 2 * PI * R); ,相当于执行 #include main() printf(“area = %fn“, 3.14159;*5.3;*5.3;); printf(“circumference = %fn“, 2*3.14159;*5.3;); ,语法错误,为什么需要常量?,假如不使用常量,直接使用常数,会有什么影响? 程序的可读性变差 容易发生书写错误 当常数需要改变时,要修改所有使用它的代码,工作量大,还可能有遗漏 解决方案: 避免使用幻数(直接使用的常数) 把幻数定义为宏常量,运算符( Operator ),34种,详见附录c 常见的运算符 算术

20、运算符 赋值运算符 类型强转 关系运算符 逻辑运算符 增和减 位运算符,运算符和操作数 (Operator and Operand),What are operator and operand?,表达式 Expression,An expression may contain 2 or more operators 每一个表达式都有一个值,算术运算符(Arithmetic Operators),Multiplication(*), addition(+) and subtraction(-) are the simplest to use Division(/) is easy, but so

21、me precautions need to be taken Modulus(%) is the one that normally confuses novices So, lets study in detail the Division and Modulus,算术运算符(Arithmetic Operators),除法(Division),Example:,整数除法(Integer Division),8 / 2 = 4,Example:,12 / 5 = 2,整数除法(Integer Division),Example:,实数除法(Floating Division),12.0 /

22、 5 = 2.4,What will be the answer if an integer is divided by 0?,Something to ponder ,求余(Modulus),It returns the remainder that occurs after performing the division of 2 operands Rule: 操作数必须是整数,Example:,12 % 5 = 2,12,5,2,10,2,求余(Modulus),How about if one of the operands is a negative integer?,Somethi

23、ng to ponder ,Example:,-7 % 3 = -1,-7,3,-2,-6,-1,求余(Modulus),Example:,7 % -3 = 1,7,-3,-2,6,1,求余(Modulus),Example:,12.0 % 3 = ?,求余(Modulus),An expression may contain 2 or more arithmetic operators Main issue: 运算顺序 ORDER OF PRECEDENCE 优先级,算术表达式 (Arithmetic Expression),Examples:,5 + 6,5 + 6 * 2,2.5 + 6

24、 2 * 2,12 / 6.0 2 * 2,= 11,= 22 or 17?,= ?,= ?,= 17,算术表达式 (Arithmetic Expression),Wait a minute,优先级(Order of Precedence) High: * / % Low: + - All operators have a precedence level. 不同优先级时的运算顺序: High precedence level operators are evaluated before lower ones. 相同优先级时的运算顺序: Operators of the same preced

25、ence level are evaluated from left to right(左结合),算术表达式 (Arithmetic Expression),Example:,?,4,8.5,2.5 + 6 , 4,4.5,2.5 + 6 2 * 2 =,算术表达式 (Arithmetic Expression),4.5,巧妙使用圆括号改变运算顺序 All expressions in parentheses must be evaluated prior to values outside brackets Nested parenthesized expressions must be e

26、valuated from the inside out, with the innermost expression evaluated first,Example:,( 9 ( 3 + 2 ) ) * 3 = ?,算术表达式 (Arithmetic Expression),赋值语句 (Assignment Statement),There are 3 types of assignment: Simple简单赋值 Multiple多重赋值 Shorthand简写的复合赋值,简单赋值 Simple Assignment,Syntax: 变量 = 表达式 ;,Every assignment

27、expression has a value,#include main( ) float price, discount, total; printf(“Buying price : “); scanf(“%f”, ,Buying price: _,Buying price: 10.00 Discount rate: _,2.50,Buying price: 10.00 Discount rate: 0.25 _,Buying price: 10.00 Discount rate: 0.25 The total price is 2.50 _,Example:,简单赋值 Simple Ass

28、ignment,Syntax: 变量1 = 变量2 = 表达式 ;,多重赋值 Multiple Assignment,Example: int number, total; float start_x, start_y; . . . number = total = 0; start_x = start_y = 100.0;,多重赋值 Multiple Assignment,从右向左赋值,Syntax: 变量x = 变量x 运算符op 表达式 ; 变量x 运算符op = 表达式;,简写的复合赋值 Shorthand Assignment,这种形式看起来更直观,且执行效率一般也更高一些,When

29、ever the expression on the right contains the variable on the left (to which the value is assigned),Example: num = num + 5;,15 + 5,20,20,简写的复合赋值 Shorthand Assignment,Expressions can also be stated using shorthand assignment operators,Example: num += 5;,similar to num = num + 5,简写的复合赋值 Shorthand Assi

30、gnment,已知 int a = 3; 执行 a += a -= a *= a 后,变量a的值? a += a -= a *= a a += a -= 9 a += 0 a = 0,简写的复合赋值 Shorthand Assignment,3,9,0,0,简写的复合赋值 Shorthand Assignment,自动类型转换,相同类型数据的运算结果,还是该类型 不同类型数据的运算结果,是两种类型中取值范围大的那种 long double double float long int short char,自动类型转换,取值范围小的类型赋值给取值范围大的类型是安全的 反之是不安全的 若大类型的值

31、在小类型能容纳的范围之内,则平安无事 但是,浮点数转为整数,会丢失小数部分,非四舍五入 反之,转换后的结果必然是错误的,具体结果与机器和实现方式有关 避免如此使用,好的编译器会发出警告,Example: int x = 10; float y; y = (float)x;,(float)10,10.000000,类型强转(Casting),消除从大到小的警告 x = (int)y; 通过下面方式把表达式的值转为任意类型 (类型)表达式,不改变x,Example: int total, number; float average; average = total / number;,15 / 2

32、,7,类型强转(Casting),两个整数运算的结果 还是整数,不是浮点数,Example: int total, number; float average; average = (float)total / number;,15.000000 / 2,7.500000,类型强转(Casting),关系运算符 ( Relational Operators ),优先级 算术运算符 = = != 赋值运算符 左结合,a b = c d = a b ch a + 1 d = a + b c 3 = x = 5 b - 1 = a != c,(a b)= c d = (a b) ch (a + 1)

33、 d = (a + b) c) (3 = x) = 5 (b - 1) = a) != c,char ch = w; int a = 2, b = 3, c = 1, d, x=10;,0 0 1 1 0 1,Symbol Description & 与(AND)当且仅当两者都为真,则结果为真 | 或(OR) 只要两者中有一个为真,结果就为真 ! 非(NOT),逻辑运算符 ( Logical Operators ),! & |,高,低,逻辑运算符 ( Logical Operators ),优先级 ! 算术运算符 关系运算符 & | 赋值运算符 左结合,复合表达式 (Compound Expr

34、ession),Arithmetic, relational and mantic operators can be integrated/combined in one expression,Example: ! ( c a ), ! ( 1 ),! ( 15 2 ), 0,Example: (a = 1) & (b = 5),( 2 = 1 ) & ( b = 5 ),1 & ( b = 5 ),1 & ( 5 = 5 ),1 & 1,1,复合表达式 (Compound Expression),Example: (c = ( b * 3 ) ) | (a = 3),( c = ( 5 *

35、3 ) ) | ( a = 3),1 | ( a = 3 ),1 | ( 2 = 3 ),1 | 0,( 15 = 15 ) | ( a = 3),1,复合表达式 (Compound Expression),Example: ! ( ( a d ) ),! ( ( 2 d ) ),! ( 1 | ( 15 17 ) ),! ( 1 | 0 ),! 1,! ( 1 | ( c d ) ),0,复合表达式 (Compound Expression),实例 ch是英文大写字母 (ch = A) & (ch = Z) 判断某一年year是否是闰年的条件是满足下列二者之一 能被4整除,但不能被100整除

36、; 能被400整除; year%4=0 & year%100!=0 | year%400=0 优先级: % = (!=) & | (year%4=0) & (year%100!=0) | (year%400=0),复合表达式 (Compound Expression),增一和减一运算符 (Increment and Decrement),n+,n-,+n,-n +让参与运算的变量加1,-让参与运算的变量减1 作为后缀(postfix)运算符时,先取n的值,然后加/减1,增一和减一运算符 (Increment and Decrement),n+,n-,+n,-n +让参与运算的变量加1,-让参与

37、运算的变量减1 作为后缀(postfix)运算符时,先取n的值,然后加/减1 作为前缀(prefix)运算符时,先加/减1,然后取n的值,Example: j = +i - 2,similar to i = i + 1; j = i 2;,4,6,前缀( Prefix )增一和减一运算符,Example: j = i+ - 2,similar to j = i 2; i = i + 1;,后缀(Postfix)增一和减一运算符,3,6,增一和减一运算符,良好的程序设计风格提倡:在一行语句中,一个变量只能出现一次增1或者减1运算 过多的增1和减1运算混合,不仅可读性差,而且因为编译器实现的方法不

38、同,导致不同编译器产生不同的运行结果,优先级与结合性,( ) - . ! + - + - * & (类型) sizeof * / % + - = = != & | & | ? : = += -= *= /= %= &= = |= = ,能背下优先级表的人凤毛麟角 用括号来控制运算顺序,更直观、方便,并减少出错的概率 多数为左结合,少数为右结合: 一元运算符 赋值运算符 条件运算符(本书没有介绍),本章知识点,标识符的命名规则 数据类型 变量(声明、赋值、初始化) 常量 运算符及其优先级和结合性 算术 赋值 类型强转 关系 逻辑 增一/减一 位运算 sizeof,作业,位操作运算符,& 按位与运算 | 按位或运算 按位异或运算, 按位右移运算 按位求反,位操作运算符,P66,3.4(2)逻辑运算与位运算的不同 main() int x=12, y=8; printf(“n%5d%5d%5d“, !x, x|y, x ,0 1 1 65523 12 8 -13 12 8,逻辑非的结果 不是按位取反,正数的补码 与其原码相同, 负数的补码为 其反码加1,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1