C程序设计第二章基本数据类型及运算.ppt_第1页
C程序设计第二章基本数据类型及运算.ppt_第2页
C程序设计第二章基本数据类型及运算.ppt_第3页
C程序设计第二章基本数据类型及运算.ppt_第4页
C程序设计第二章基本数据类型及运算.ppt_第5页
已阅读5页,还剩78页未读 继续免费阅读

下载本文档

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

文档简介

1,C的基本数据类型及运算,信息对抗,电子工程学院,2,标识符,标识符就是给程序中的变量、常量、函数、数组、结构体以及文件所起的名字。 1命名规则: ()以字母或下划线开头,由字母、数字和下划线组成; ()不能和系统关键字同名; ()尽量不要用下划线开头; ()一般对长度无规定; ()大小写是不同的字母(大小写敏感)。,例:判断下列标识符号合法性 sum Sum M.D.John day Date 3days student_name #33 lotus_1_2_3 char ab _above $123,M.D.John,3days,#33,char,$123,ab,3,标识符, 好的命名习惯: ()尽量做到见名知意;(sum,area) ()变量名、函数名用小写,符号常量用大写; ()在易混淆地方,尽量避免用易认错的字母; e.g. 0(数字) (大写字母) o(小写字母) 1 (数字) I(I的大写字母) l(L的小写字母) (数字)(大写字母) z(小写字母),4,Keywords and Reserved Identifiers,ISO/ANSI C Keywords auto enum restrict unsigned break extern return void case float short volatile char for signed while const goto sizeof _Bool continue if static _Complex default inline struct _Imaginary do int switch double long typedef else register union,5,注意:1C语言区分大小写,2标识符的长度一般不超过32个字符,如:Price 和 price,系统会认为是两个不同的标识符。,具体情况视不同系统而定。,6,数据类型 Data Types,程序应包括两个方面的内容: 数据的描述。 操作步骤,即动作的描述。 数据不仅是操作的对象,而且操作结果会改变数据的状况。 计算机科学家沃思(Niklklaus Wirth)提出一个公式: 数据结构十算法=程序 程序算法十数据结构十程序设计方法十 语言工具和环境 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,例:浮点数0xC1 0x48 0x00 0x00的值 二进制表示:11000001 01001000 00000000 00000000 符号位S1,故为负数 阶码(10000010)1271301273 尾数1.1001000 00000000 00000000 1.5625 所以:其代表的值为-1231.5625-12.5,15,常量 constants,Some data are preset before a program is used and keep their values unchanged throughout the life of the program. These are constants.(常量:具有固定的值,且不会被程序改变) 数值常量整型常量,实型常量,双精度常量 字符常量 字符串常量 符号常量,16,整型常量,整型常量即整常数。整常数可用以下三种形式表示: 十进制整数。如123、-456、0。 八进制整数。以0开头的数是八进制数。如0123表示八进制数123,即(123)8等于十进制数83。-011表示八进制数 -11,即十进制数-9。 十六进制整数。以0x开头的数是进制数。 如0x123,代表16进制数123,即(123)161162163160=256323=291。-0x12等于十进制数一18。,整型常量的类型 根据其值所在范围确定其数据类型 在整常量后加字母l或L,认为它是long int 型常量,例 12 与 12L,例 30000 为int型 65536 为long int 型,17,实型常量,实数在语言中又称浮点数,实数有两种表示形式: 1.小数形式。它由数字和小数点组成(注意必须有小数点)。0.123、.123、123.0、123.、0.0都是十进制数形式。 2.指数形式。如123e3或123E3都代表123103但注意字母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,如 A65, a97, 048 , n10,如 a A ? n 101,转义字符:反斜线后面跟一个字符或一个代码值表示,例 转义字符举例 main() printf(“101 x42 Cn“); printf(“I say:“How are you?“n“); printf(“C Programn“); printf(“Turbo C“); ,运行结果:(屏幕显示) A B C Isay:”How are you?” C Program Turbo C,例 main() printf(“Yb=n”); ,运行结果: 屏幕显示:= 打印机输出:,字符常量,19,字符常量与字符串常量不同,定义:用双引号(“”)括起来的字符序列 存储:每个字符串尾自动加一个 0 作为字符串结束标志,例: char ch; ch=“A”;,字符串常量,20,举例,#include main() printf(“ ab ct denftgn“); printf(“ ab ct derftgn“); printf(“htibbj kn“); printf(“htij kn“); printf(“h12345678ij kn“); ,21,Constants and the C Preprocessor,Sometimes you need to use a constant in a program. For example, you could give the circumference of a circle as follows: circumference = 3.14159 * diameter; Here, the constant 3.14159 represents the world-famous constant pi (). To use a constant, just type in the actual value, as in the example. However, there are good reasons to use a symbolic constant instead. That is, you could use a statement such as the following and have the computer substitute in the actual value later: circumference = pi * diameter;,22,Why to use a symbolic constant?,First, a name tells you more than a number does. Compare the following two statements: owed = 0.015 * housevalue; owed = taxrate * housevalue; If you read through a long program, the meaning of the second version is plainer. Also, suppose you have used a constant in several places, and it becomes necessary to change its value. After all, tax rates do change. Then you only need to alter the definition of the symbolic constant, rather than find and change every occurrence of the constant in the program.,23,how set up a symbolic constant?,One way is to declare a variable and set it equal to the desired constant. You could write this: float taxrate; taxrate = 0.015; This provides a symbolic name, but taxrate is a variable, so your program might change its value accidentally. Fortunately, C has a couple better ideas.,24,#define NAME value,Just add a line like the following at the top of the file containing your program: #define TAXRATE 0.015 When your program is compiled, the value 0.015 will be substituted everywhere you have used TAXRATE. This is called a compile time substitution.,25,符号常量 Symbolic Constants,C语言允许将程序中的常量定义为一个标识符,称为符号常量。符号常量一般使用大写英文字母表示,以区别于一般用小写字母表示的变量。符号常量在使用前必须先定义,定义的形式是: #define 是宏定义预处理命令,不是C语句 例如: #define PI 3.1415926 #define TRUE 1 #definr FALSE 0 #define STAR * 这里定义PI、TRUE、FLASE、STAR为符号常量,其值分别为3.1415926,1,0,*。,26,compile time substitution,27,examples,The #define statement can be used for character and string constants, too. Just use single quotes for the former and double quotes for the latter. The following examples are valid: #define BEEP a #define TEE T #define ESC 033 #define OOPS “Now you have done it!“ Remember that everything following the symbolic name is substituted for it. Dont make this common error: /* the following is wrong */ #define TOES = 20 If you do this, TOES is replaced by = 20, not just 20. In that case, a statement such as digits = fingers + TOES; is converted to the following misrepresentation: digits = fingers + = 20;,28,The #undef Directive,The #undef directive cancels an earlier #define definition. That is, suppose you have this definition: #define LIMIT 400 Then the directive #undef LIMIT removes that definition. Now, if you like, you can redefine LIMIT so that it has a new value. Even if LIMIT is not defined in the first place, it is still valid to undefine it.,29,The #undef Directive,#define PI 3.14159 Main() #undef PI /* 从此往后,PI不在代表3.14159 */ F1() ,30,带参数的宏定义,除了字符串的替换,还要进行参数替换。 定义形式: #define S(a,b) a*b . . area = S(3,2); 预处理后: area=3*2; 注意:1,宏名和参数之间不能有空格 2,注意括号的使用,保证运算次序。,31,The const Modifier,C90 added a second way to create symbolic constantsusing the const keyword to convert a declaration for a variable into a declaration for a constant: const int MONTHS = 12; / MONTHS a symbolic constant for 12 This makes MONTHS into a read-only value. That is, you can display MONTHS and use it in calculations, but you cannot alter the value of MONTHS.,32,概念:其值可以改变的量 变量名与变量值 变量定义的一般格式: 数据类型 变量1,变量2,变量n;,变量初始化:定义时赋初值,例: int a,b,c; float data;,决定分配字节数 和数的表示范围,合法标识符,例: int a=2,b,c=4; float data=3.67; char ch=A; int x=1,y=1,z=1; int x=y=z=1; X,变量的使用:先定义,后使用,例1 int student; stadent=19; /Undefined symbol stadent in function main,例2 float a,b,c; c=a%b; /Illegal use of floating point in function main,变量定义位置:一般放在函数开头,变量 Variables,33,占字节数随机器不同而不同,一般占一个机器字 shortintlong 可用sizeof(类型标识符)测量,实型变量 float:占4字节,提供7位有效数字 double:占8字节,提供1516位有效数字,字符型变量 字符变量存放字符ASCII码 char与int数据间可进行算术运算,例1: float a; a=111111.111; /* a=111111.1*/ 例2: double b; b=111111.111; /* b=111111.111*/,例 a=D; /* a=68; */ x=A+5; /* x=65+5; */ s=!+G /* s=33+71; */,没有字符串变量,用字符数组存放,整型变量,34,#define PRICE 12.5 main() int num=3; float total; char ch1,ch2=D; total=num*PRICE; ch1=ch2-A+a; printf(“total=%f,ch1=%cn”,total,ch1); ,运行结果: total=37.500000, ch1=d,例子,35,Enumerated Types,declare symbolic names to represent integer constants. By using the enum keyword, you can create a new “type“ and specify the values it may have. (Actually, enum constants are type int, therefore they can be used wherever you would use an int.) The purpose of enumerated types is to enhance the readability of a program. For example, you can make these declarations: enum spectrum red, orange, yellow, green, blue, violet ; enum spectrum color; The identifiers within the braces enumerate the possible values that a spectrum variable can have.,establishes spectrum as a tag name,makes color a variable of that type,36,Usage,Therefore, the possible values for color are red, orange, yellow, and so on. Then, you can use statements like the following: int c; color = blue; if (color = yellow) .; for(color =red;color = violet;color+) .;,37,Enum Constants,Just what are blue and red? Technically, they are type int constants. For example, given the preceding enumeration declaration, you can try this: printf(“red = %d, orange = %dn“, red, orange); Here is the output: red = 0, orange = 1 What has happened is that red has become a named constant representing the integer 0. Similarly, the other identifiers are named constants representing the integers 1 through 5.,38,Default Values,By default, the constants in the enumeration list are assigned the integer values 0, 1, 2, and so on. Therefore, the declaration enum kids nippy, slats, skippy, nina, liz ; results in nina having the value 3. Assigned Values You can choose the integer values that you want the constants to have. Just include the desired values in the declaration: enum levels low = 100, medium = 500, high = 2000 ;,39,Assigned Values,If you assign a value to one constant but not to the following constants, the following constants will be numbered sequentially. For example, suppose you have this declaration: enum feline cat, lynx = 10, puma, tiger ; Then cat is 0, by default, and lynx, puma, and tiger are 10, 11, and 12, respectively.,40,变量赋初值,1.初始化:在定义变量的同时为变量赋初值 2.形式: 类型标识符 变量名=常量或常量表达式 例如: int x=10 ; char ch=a ; 变量赋初值允许使用符号常量 例如: #define PI 3.1415926 .float x=PI ; . 可对被定义的变量的一部分赋初值 例如: int a , b, c=1, d=2; float r=2 ,l, s; 可对几个变量赋以同一个初值 例如: int a=6, b=6, c=6; 不要写为: int a=b=c=6;,41,初始化,初始化不是在编译阶段完成的(只有静态存储变量和外部变量的初始化是在编译阶段完成的),而是在程序运行时执行本函数时赋以初值的。相当于有一个赋值语句,例如: int a=3; 相当于: int a; *指定a为整型变量*/ a=3; *赋值语句,将3赋予a* 又如 int a,b, c=5; 相当于: int a,b, c; *指定a、b、c为整型变量* c5; *将5赋给c*/,42,C 运 算 符,算术运算符:(+ - * / % + -) 关系运算符:( = !=) 逻辑运算符:(! & |) 位运算符 :( | &) 赋值运算符:(= 及其扩展) 条件运算符:(?:) 逗号运算符:(,) 指针运算符:(* &) 求字节数 :(sizeof) 强制类型转换:(类型) 分量运算符:(. -) 下标运算符:() 其它 :(( ) -),Operators, Expressions 运算符和表达式,43,Operators, Expressions 运算符和表达式,运算符功能 与运算量关系 要求运算量个数 要求运算量类型 运算符优先级别 结合方向 结果的类型,学习运算符应注意,44,运算结合方向,两种: 1. 自左向右,为左结合性。 2. 自右向左,为右结合性。 若在运算量的两侧的运算符有相同的优先级,则按它们的结合方向顺序处理。,例: a b / c 1.5 + ad e;,a=b=c=d;,相当于: a=(b=(c=d);,45,Arithmetic Operators,+ 加法,正值。 如: 3+6, +3, 减法,负值。 如: 6 4, 5, 乘法。 如: 38,/ 除法。 如: 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,Division Operator : integer division,两个整数相除结果为整数,如5/3的结果值为1,舍去小数部分。 但是如果除数或被除数中有一个为负值,则舍入的方向是不固定的。 例如,53在有的机器上得到结果1,有的机器则给出结果2。 多数机器采取“向零取整”方法,即5/31, 53= 1,取整后向零靠拢。 如果参加运算的两个数中有一个数为实数,则结果是double型,因为所有实数都按double型进行运算。,48,The divide.c Program,/* divide.c - divisions we have known */ #include int main(void) printf(“integer division: 5/4 is %d n“, 5/4); printf(“integer division: 6/3 is %d n“, 6/3); printf(“integer division: 7/4 is %d n“, 7/4); printf(“floating division: 7./4. is %1.2f n“, 7./4.); printf(“mixed division: 7./4 is %1.2f n“, 7./4); return 0; ,integer division: 5/4 is 1 integer division: 6/3 is 2 integer division: 7/4 is 1 floating division: 7./4. is 1.75 mixed division: 7./4 is 1.75,Notice how integer division does not round to the nearest integer, but always truncates, that is, discards the entire fractional part.,49,Modulus Operator: %,used in integer arithmetic. gives the remainder that results when the integer to its left is divided by the integer to its right. For example, 13 % 5 (read as “13 modulo 5“) has the value 3, because 5 goes into 13 twice, with a remainder of 3. Dont bother trying to use this operator with floating-point numbers. It just wont work. One common use is to help you control the flow of a program.,50,What about negative numbers?,Before C99 settled on the “truncate toward zero“ rule for integer division, there were a couple of possibilities. But with the rule in place, you get a negative modulus value if the first operand is negative and a positive modulus otherwise: 11 / 5 is 2 and 11 % 5 is 1 11 / -5 is -2 and 11 % -2 is 1 -11 / -5 is 2 and -11 % -5 is -1 -11 / 5 is -2 and -11 % 5 is -1 If your system shows different behavior, it hasnt caught up to the C99 standard. In any case, the standard says, in effect, that if a and b are integer values, you can calculate a%b by subtracting (a/b)*b from a. For example, you can evaluate “-11%5” this way: -11 - (-11/5) * 5 = -11 -(-2)*5 = -11 -(-10) = -1,51,Increment and Decrement Operators: + and -,The increment operator performs a simple task; it increments (increases) the value of its operand by 1. This operator comes in two varieties. The first variety has the + come before the affected variable; this is the prefix mode. The second variety has the + after the affected variable; this is the postfix mode. The two modes differ with regard to the precise time that the incrementing takes place.,52,自增、自减运算符(+ and -),作用是使变量的值增1或减1,如: + i, - i (在使用i之前,先使i的值加(减)1) i + ,i - (在使用i之后,使i的值加(减)l) +i和i+的作用相当于ii+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,Assignment Operator: =,In C, the equal sign does not mean “equals.“ Rather, it is a value-assigning operator. The statement bmw = 2002; assigns the value 2002 to the variable named bmw. That is, the item to the left of the = sign is the name of a variable, and the item on the right is the value assigned to the variable. The = symbol is called the assignment operator. Again, dont think of the line as saying, “bmw equals 2002.“ Instead, read it as “assign the value 2002 to the variable bmw.“ The action goes from right to left for this operator.,56,The statement i = i + 1;.,i = i + 1; As mathematics, this statement makes no sense. If you add 1 to a finite number, the result isnt “equal to“ the number you started with, but as a computer assignment statement, it is perfectly reasonable. It means “Find the value of the variable named i; to that value, add 1, and then assign this new value to the variable named i“,57,Modifiable lvalue,A statement such as 2002 = bmw; makes no sense in C (indeed, is invalid) because 2002 is just a constant. You cant assign a value to a constant; When you sit down at the keyboard,remember that item to the left of the = sign must be the name of a variable. Actually, the left side must refer to a storage location. The simplest way is to use the name of a variable, but, as you will see later, a “pointer“ can be used to point to a location. More generally, C uses the term modifiable lvalue to label those entities to which you can assign values.,58,Data Objects, Lvalues, Rvalues, and Operands,A data object is a general term for a region of data storage that can be used to hold values. C uses the term lvalue to mean a name or expression that identifies a particular data object. The name of a variable is an lvalue, so object refers to the actual data storage, but lvalue is a label used to identify, or locate, that storage. Not all objects can have their values changed, so C uses the term modifiable lvalue to identify objects whose value can be changed. left side of an assignment operator should be a modifiable lvalue. L Left,59,rvalue,refers to quantities that can be assigned to modifiable lvalues. For instance, consider the following statement: bmw = 2002; Here, bmw is a modifiable lvalue, and 2002 is an rvalue. As you probably guessed, the r in rvalue comes from right. Rvalues can be constants, variables, or any other expression that yields a value. As long as you are learning the names of things, the proper term for what we have called an “item“ (as in “the item to the left of the =“) is operand. Operands are what operators operate on.,60,triple assignment,/* golf.c - golf tournament scorecard */ #include int main(void) int jane, tarzan, cheeta; cheeta = tarzan = jane = 68; printf(“ cheeta tarzan janen“); printf(“First round score %4d %8d %8dn“,cheeta,tarzan,jane); return 0; ,The assignments are made right to left: First jane gets the value 68, and then tarzan does, and finally cheeta does. Therefore, the output is as follows: cheeta tarzan jane First round score 68 68 68,61,简单赋值运算符,符号: = 格式: 变量标识符=表达式 作用:将一个数据(常量或表达式)赋给一个变量,例 a=3; d=func(); c=d+2;,62,复合赋值运算符,种类:+= -= *= /= %= = &= = |= 含义: exp1 op= exp2 exp1 = exp1 op exp2,63,复合赋值运算符,结合方向:自右向左

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论