Download presentation
Presentation is loading. Please wait.
1
Chapter 2 Constants, Variables and Data Types
PROGRAMMING IN ANSI C
2
1/18/2019 Chapter 2 The famous computer scientist N. Wirth proposed: program = data structure + algorithm The data structure is used to descript data, and the algorithm is used to descript the operation. The objective dealt with by the algorithm is the data, and the data has a certain special form. The data structure is just the organization form of the data. 数据结构表示数据间的关系,算法指明了对数据处理的步骤和方法。
3
Character Set The characters in C :(A subset of ASCII )
1/18/2019 Character Set The characters in C :(A subset of ASCII ) Letters: Uppercases: A~Z Lowercases: a~z Digits : 0~9 Special characters : ; , ( ) : + * etc. White spaces: Blank space, Horizontal tab Carriage return, New line Form feed The white spaces are used to separate two identifiers or keywords, and the complier ignores them unless they are a part of a string constant. indifferent中等的; 中立的; 无关紧要的; 漠不关心的; Between identifiers or keywords, it is indifferent to use which kind of white space or how many white spaces.
4
C Tokens C Tokens - the smallest individual character units
1/18/2019 C Tokens C Tokens - the smallest individual character units Keywords: int, float, return …… Identifiers: main, printf, number, define …… Constants: 100, , "I see, I remember!" …… Operators: + = * , ( ) [ ] …… Special Symbols: # { } …… 1. C makes a distinction between uppercases and lowercases, e.g. “number”, “Number” and “NUMBER” are not the same identifiers. 2. The length of identifiers is limited. Different development tools make different limitation. 3.Try your best to use significative identifiers. If it is necessary, you can use underscores, such as “student_number” or “sno”. Don’t always use simple “a”, “b”, “c”, of which we can’t make out the meaning. auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef unsigned union void volatile while In which option all identifiers are valid? (A) a1 b! 12_e (B) main f12 "abc" (C) a int PI (D) x2 number_1 main Valid identifiers consist of uppercases and lowercases, digits and the underscore characters. In C, the first character of an identifier must not be a digit, that is to say, the first character of an identifier may be a letter or an underscore. Constants Number Character Integer:10, -2 Real: 1.0, -0.88 Single char: 'a' String: “Hello" Direct Symbolic n a C source program, the basic element recognized by the compiler is the “token.” A token is source-program text that the compiler does not break down into component elements. Syntax token : keyword identifier constant string-literal operator punctuator Try your best 1.尽力做/尽力试。
5
Data Types short Integer int long Number float double Real Primary
1/18/2019 Data Types short Integer int long Number float Real double Primary long double character char void void Array Data Types Derived Structure struct Union union Pointers * typedef User-Defined Enumeration enum
6
Integer Constants decimal (general form) e.g. 123,-321,0,+0
1/18/2019 Integer Constants decimal (general form) e.g. 123,-321,0,+0 octal (consists of any combination of digits 0 ~ 7, with a leading 0) e.g (The decimal equivalent is 83.) hexadecimal (a sequence of digits 0 ~ 9 and alphabets A ~ F or a ~ f, preceded by 0x, or 0X) e.g. 0x123 (The decimal equivalent is 291.)
7
Conversion Between Different Number Systems
1/18/2019 Conversion Between Different Number Systems R-ary to decimal conversion If the number in R-ray is: an-1 an-2 … a1 a0 . a-1 … a-m Convert it to decimal number N: N = an-1×Rn-1+ an-2×Rn-2+ … a1×R1 + a0×R a-1×R-1…a-m×R-m ( )2 = 125+ 124+ 121+ 120 + 12-1 = (51.5)10 (A12)16 = 10162+ 1161+ 2160 = (2578)10 (1027)8 = 183+ 281+ 780 = (535)10
8
Conversion Between Different Number Systems
1/18/2019 Conversion Between Different Number Systems Decimal to R-ary conversion Integral part:Divided by R and get the remainder remainder 66 2 …… (B0) 2 33 …… (B1) 2 16 Convert (66)10 to a binary number. …… (B2) 2 8 4 …… (B3) 2 2 …… (B4) 2 …… (B5) 2 1 (66)10 = ( )2 …… (B6)
9
Conversion Between Different Number Systems
1/18/2019 Conversion Between Different Number Systems Decimal to R-ary conversion Integral part:Divided by R and get the remainder remainder 66 8 …… (O0) 8 8 …… (O1) 8 1 Convert (66)10 to an octal number. …… (O2) (66)10 = (102)8
10
Conversion Between Different Number Systems
1/18/2019 Conversion Between Different Number Systems Decimal to R-ary conversion Integral part:Divided by R and get the remainder remainder 66 16 …… (H0) 16 4 …… (H1) Convert (66)10 to a hexadecimal number. (66)10 = (42)16
11
Conversion Between Different Number Systems
1/18/2019 Conversion Between Different Number Systems Fractional part:Multiplied by R and get the integral part of the product (0.8125)10 = (0.1101)2 Convert (0.8125)10 to a binary number. high low …… (B-1) …… (B-2) …… (B-3) …… (B-4)
12
Conversion Between Different Number Systems
1/18/2019 Conversion Between Different Number Systems Fractional part:Multiplied by R and get the integral part of the product Convert (0.8125)10 to an octal number. …… (O-1) …… (O-2) (0.8125)10 = (0.64)8
13
Conversion Between Different Number Systems
1/18/2019 Conversion Between Different Number Systems Fractional part:Multiplied by R and get the integral part of the product Convert (0.8125)10 to a hexadecimal number. …… D (H-1) (0.8125)10 = (0.D)16
14
Conversion Between Different Number Systems
1/18/2019 Conversion Between Different Number Systems Conversion between octal and binary One octal digit corresponds to 3 binary digits. 2 7 4 ( )2=(2074)8
15
Conversion Between Different Number Systems
1/18/2019 Conversion Between Different Number Systems Conversion between hexadecimal and binary One hexadecimal digit corresponds to 4 binary digits. 4 3 C ( )2=(43C)16
16
Integer Variables x = + 1001011 [x]c= 01001011
1/18/2019 Integer Variables x = [x]c= x = [x]c= [+ 0]c= = [- 0]c int number; Complemental code To get the complemental code of a positive binary number, you can just add the sign digit 0 on the highest digit; To get the complemental code of a negative binary number, you can reverse each digit of the complemental code of its positive equivalents (so, the sign digit become 1), then add 1 to it. 相等的东西( equivalent的名词复数 ); 对应词
17
Integer Variables int number; number=10; 10 1 1 number=-10; -10 1
1/18/2019 Integer Variables int number; number=10; 10 1 reverse 1 add 1 number=-10; -10 1
18
Integer Variables 1 1 1/18/2019 Type Shortening Byte Range signed int
-215 ~ (215-1) unsigned int unsigned 0 ~ (216-1) signed short int short 1 -27 ~ (27-1) unsigned short int unsigned short 0 ~ (28-1) signed long int long 4 -231 ~ (231-1) unsinged long int unsigned long 0 ~ (232-1) 1 The maximum of the signed int (215-1) The minimum of the signed int (-215 ) 1 The maximum of the unsigned int (216-1) The minimum of the unsigned int 0
19
the maximum int data is 32767 (215-1)
1/18/2019 Data Overflow the maximum int data is 32767 1 the maximum int data is (215-1) the minimum int data is -32768(-215 ) main() { int a, b; a = 32767; b = a + 1; printf("a=%d,b=%d",a,b); } Add 1 to 32767: 1 a = 32767, b =
20
Data Overflow the maximum int data is 32767 1 1 a = 8, b = 9 main() {
1/18/2019 Data Overflow the maximum int data is 32767 main() { int a, b; a = 65544; b = a + 1; printf("a=%d,b=%d",a,b); } 1 a 1 b a = 8, b = 9
21
Declaration of Integer Variables
1/18/2019 Declaration of Integer Variables Error .. 6: Expression syntax in function main Error .. 7: Undefined symbol c in function main Error .. 7: Undefined symbol d in function main All variables must be declared before any other executable statement. main() { int a,b,c,d; unsigned u; a = 12; b = -24; u=10; c=a+u; d=b+u; printf("c=%d,d=%d",c,d); } main() { int a,b; unsigned u; a = 12; b = -24; u=10; int c,d; c=a+u; d=b+u; printf("c=%d,d=%d",c,d); } main() { int a,b; unsigned u; a = 12; b = -24; u=10; int c,d; c=a+u; d=b+u; printf("c=%d,d=%d",c,d); } Compound statement
22
Type of Integer Constants
1/18/2019 Type of Integer Constants If an integer constant in the range ~ (-215 ~ 215-1), by default, it represents an int type data. If it beyond above range and still in the range ~ (-231 ~ 231-1), it represents a long type data. We can specify it as an unsigned number by appending the character “U” or “u”. e.g u. We also can specify it as a long type by appending the character “L” or “l”. e.g L.
23
Real Constants Decimal notation: e.g. 0.123 0.0 123. .123
1/18/2019 Real Constants Decimal notation: e.g Exponential notation: e.g. 1.23e E e5 Notice:The mantissa before “e” or “E” can’t be omitted, and the exponent after “e” or “E” must be an integer. e.g. e e2.3 e are all invalid Mantissa: 尾数
24
Real Variables Type Byte Significant figure Range float 4 6~7
1/18/2019 Real Variables Type Byte Significant figure Range float 4 6~7 -3.4×10-38~ 3.4×1038 double 8 15~16 -1.7×10-308~ 1.7×10308 long double 16 18~19 -1.2× ~1.2×104932 main() { float a,b; a = e5; b = a + 20; printf("a=%f\nb=%f",a,b); } 首先说一下: 范围是3.4E-38 ——3.4E+38,可提供7位有效数字。 上述这两个量都是近似值,各个编译器不太一样的。 下面我就将标准值是怎么定义的,和你说一下: 这个比较复杂,建议你找一下IEEE754标准看一下。 这个简单说一下吧: 在IEEE754标准中进行了单精度浮点数(float)和双精度数浮点数(double)的定义。float有32bit,double有64bit。它们的构成包括符号位、指数位和尾数位。 这些位的构成如下: 种类 符号位 指数位 尾数位---- float---第31位(占1bit)---第30-23位(占8bit)----第22-0位(占23bit) double--第63位(占1bit)---第62-52位(占11bit)---第51-0位(占52bit) 取值范围主要看指数部分: float的指数部分有8bit(2^8),由于是有符号型,所以得到对应的指数范围-128~128。 double的指数部分有11bit(2^11),由于是有符号型,所以得到对应的指数范围-1024~1024。 由于float的指数部分对应的指数范围为-128~128,所以取值范围为: -2^128到2^128,约等于-3.4E38 — +3.4E38 精度(有效数字)主要看尾数位: float的尾数位是23bit,对应7~8位十进制数,所以有效数字有的编译器是7位,也有的是8位 a = b =
25
1/18/2019 Real Variables float pi = ; > > > × 21 1 the fractional part of the significant figures of the mantissa the sign digit of the exponential the sign digit of the mantissa exponential value 尾数中小数的有效数位 前两天仔细看了看, 觉得研究计算机如果不说说如何表示浮点数就太不厚道了.很多人也写过, 这里自己再写一道, 一是为了加深点印象, 第二是希望自己能写的深入浅出一点, 希望看过的人都能很容易看懂. 先说说32 位的 float型. 一个浮点数 X, 在计算机中表示为: X = a * 2e 这里 e 代表指数, a 代表尾数, 在 计算机内部, 他们都是用二进制表示的. 其中 a 用二进制的科学表示法表示, 由于科学表示法第一位总是1 (0除外) , 所以第一位略去不计. e 表示的时候, 因为要表示出负数, 所以 要加上127 , 实际运算的时候要减去 IEEE 规定, 32 位 float型被拆开成以下格式, 左边为高位 : 0 最高位,第32位 第 31-23位,共8位 第23-1位 符号位 指数位 尾数位 0为正,1为负 -127~+127 0~0x 7f ff ff float 的范围是 * e38 ~ * e38 一般在人看来是 十进制的数, 要转换成二进制. 十进制转二进制, 大于1 的部分就是除以2 取余, 小于1 的部分乘2 取整. 比如 8.5 转换成二进制就是 , 处理成这一步, 还要用科学表示法表示, 就成了 * 23 , 注意: 由于 第一个1 要去掉, 所以成了 0001 , 3 需要加上 127 就成了 130 , 二进制就是 套用上面话就表示为: 0 16 进制 就是: 0x , 一般来说 , intel 系列的 CPU 都使用的是 小尾存放, 就是 高字节放在后面, 刚好要掉过来就是: 0x , 这样就完成了一次浮点数的表示. 注意: 浮点数 0.0 在计算机中表示为 0x 那么浮点数的精度是怎么回事情呢? 当我们使用二进制表示 大于1 的部分的时候, 没有问题, 除以2,一直下去, 最后一位肯定不是1 就是 0; 那么小数部分呢? 举个例子, 比如 0.8 表示 0.8 * 2 = 0.6 * 2 = 0.2 - 0 .* 2 *2 这样就循环了 就是说 0.8 的二进制 就是 一直循环下去, 而我们计算机如果表示0.8只能取0后面的前几十位, 这就说明 如果是 (循环超过100次) , 它表示出来的值其实是和 0.8 一样, 所以我们比较float型的数字 用 a == b 其实是不严谨的(精度问题), 一般都是 用 abs(a - b) < 之类就默认是相等. 那么 double型呢? 咱们可以照 float 型的葫芦 来画了. double 型 只是说 取 64 位, 比float型的位 多一倍 IEEE 规定 double 型 , 第64位 符号位 指数位 ( ) 尾数位 所以 double型的范围是 * e308 ~ * e308 多用了几位, 表示范围大了很多, 其实本质跟float型一样.
26
1/18/2019 Type of Real Constants C compiler defaults a real number to be a double type, so that the precision can be hold as much as possible. If we want to define the data to be float, we must append the letter “f” or “F” to the number. e.g f F e4F If we want to define the data to be long double, we must append the letter “l” or “L” to the number.
27
1/18/2019 Character Type In C, the data concerning character includes single character data and string. A single character data contains a single character enclosed in a pair of single quote marks. e.g. 'a' A string data is a sequence of characters enclosed in double quotes. e.g. “Hello”. If there is no any character in double quotes it is an empty string. A string actually is a character array.
28
Character Type 'A' – 'c' = ? ('a' – 32) – ('a' + 2) = -34 1/18/2019
In memory, a character requires one byte storage to store the ASCII (American Standard Code for Information Interchange) of the character. The ASCII value of '0' is 48, and the ASCII value of '1' is one greater than that of '0'. The uppercases or lowercases are all arranged in turn. If you know the ASCII value of ‘a’ is 97, you can calculate the ASCII value of any lowercase. The ASCII value of a lowercase is 32 greater than its uppercase equivalent. So the ASCII value of ‘A’ is 65. The ASCII value of newline is 10. 'A' – 'c' = ? ('a' – 32) – ('a' + 2) = -34 美国信息交换用标准码(ASCII)
29
Character Constants In C, there are 2 kinds of character constants
1/18/2019 Character Constants In C, there are 2 kinds of character constants The single character enclosed within a pair of single quote marks. e.g. 'a' 'B' '3' '+' The escape sequence character,also known as backslash character.
30
1/18/2019 String Constants String constants are clearly different with character constants. A string constant is a sequence of 0 or more characters enclosed in double quotes. Even if there is no any character enclosed in double quotes, it still is a string constant, known as empty string. A single character data only is a single character enclosed in a pair of single quote marks.
31
1/18/2019 String Constants A character constant only requires one byte storage, storing its ASCII value. The characters in string are stored in turn. Moreover, a null character '\0' is stored after the last character of the string. The null character servers as the “end-of-string” marker. Therefore, if the string contains N characters, it requires N+1 bytes storage at least.
32
String Constants c h i n a \0 String "china" 97 \0 String "a" 1 97
1/18/2019 String Constants c h i n a \0 String "china" 97 \0 String "a" 1 97 Char 'a' 1 48 \0 String "0" 1
33
Character Variables char c1, c2, c3; c1 = 'a'; c2 = '\n'; c3 = 65;
1/18/2019 Character Variables char c1, c2, c3; c1 = 'a'; c2 = '\n'; c3 = 65; main() { char ch1, ch2; ch1 = 'a'; ch2 = 98; printf("%c, %c\n ", ch1, ch2); printf("%d, %d\n ", ch1, ch2); } a, b 97, 98
34
Character Variables char c1, c2, c3; c1 = 'a'; c2 = '\n'; c3 = 65;
1/18/2019 Character Variables char c1, c2, c3; c1 = 'a'; c2 = '\n'; c3 = 65; main() { char ch; int i; ch = 97; i = 353; printf("%c, %d\n", ch, ch); printf("%c, %d\n", i, i); } main() { char ch; int i; ch = 97; i = 'a'; printf("%c, %d\n", ch, ch); printf("%c, %d\n", i, i); } a, 97 a, 353 a, 97
35
Character Variables char c1, c2, c3; c1 = 'a'; c2 = '\n'; c3 = 65;
1/18/2019 Character Variables char c1, c2, c3; c1 = 'a'; c2 = '\n'; c3 = 65; main() { char c1 = 'A', c2; c1 = c1 + 3; c2 = c1 + 32; printf("%c, %d\n ", c1, c1); printf("%c, %d\n ", c2, c2); } main() { char c1 = 'A', c2; c1 = c1 + 3; c2 = c1 + ('a' - 'A'); printf("%c, %d\n ", c1, c1); printf("%c, %d\n ", c2, c2); } D, 68 d, 100
36
Homework Review Questions P48
1/18/2019 Homework Review Questions P48 2.1(a)~(j) & 2.16~2.19 write down in your exercise book 2.20 write down the correction of the program and the output you think in your homework book , and then check your thought through programming experiments. 。 2.4~2.11 think whether you can understand all of them
Similar presentations