Chapter 3 Data types, Operators, and Expressions Preparations ◆各种进制之间的转换 ● convert Binary、 Octal、 Hexadecimal into decimal 例(110112=1×23+1×2+1×23+0×22+1×2+1×2=(59) 例(136) 8=1x82+3×81+6×80=(94)0 例(1F2A)6=1×16+15×162+2×16+10×16=(7978
2 Preparations ❖各种进制之间的转换 ⚫convert Binary 、Octal、Hexadecimal into Decimal 1 0 5 4 3 2 1 0 2 例 (111011) = 1 2 +1 2 +1 2 + 0 2 +1 2 +1 2 =(59) 1 0 2 1 0 例 (136)8 = 18 + 38 + 68 =(94) 1 0 3 2 1 0 1 6 例 (1F2A) = 116 +1516 + 216 +1016 =(7978) Chapter 3 Data types , Operators , and Expressions
Chapter 3 Data types, Operators, and Expressions 各种进制之间的转换 ● convert Binary、 Octal、 Hexadecimal into decimal 方法:连续除以基,从低到高记录余数,直至商为0 例把十进制数59转换成二进制数 例把十进制数159转换成八进制数 例把召址付9转癫大进制数 229 1209条3 1802余2 16争余1 201 159)=(232B 01 (459)10=(1CB)6 (59)0=(111011)2
3 方法:连续除以基,从低到高记录余数,直至商为0 例 把十进制数59转换成二进制数 2 59 2 29 2 14 2 7 2 3 2 1 0 (59)10=(111011)2 1 1 0 1 1 1 1 1 1 0 1 1 余 余 余 余 余 余 例 把十进制数159转换成八进制数 8 159 8 19 8 2 0 (159)10=(237)8 2 3 7 余 7 余 3 余 2 例 把十进制数459转换成十六进制数 16 459 16 28 16 1 0 (459)10=(1CB)16 1 C B 余11 余12 余 1 Chapter 3 Data types , Operators , and Expressions ❖各种进制之间的转换 ⚫convert Binary 、Octal、Hexadecimal into Decimal
Chapter 3 Data types, Operators, and Expressions ● Binary and Octal Binary into Octal:从右向左,每3位一组(不足3位 左补0),转换成八进制 令 Octal into Binary:用3位二进制数代替每一位八制数 000~0 001~1 例(1101001)2=(001101001)2=(151)|00~2 011~3 100~4 例(246)=(010,100110=(101001102101~5 111~7
4 ⚫Binary and Octal ❖Binary into Octal:从右向左,每3位一组(不足3位 左补0),转换成八进制 ❖Octal into Binary :用3位二进制数代替每一位八制数 例 (1101001)2=(001,101,001)2=(151)8 例 (246)8=(010,100,110)2=(10100110)2 000 ~ 0 001 ~ 1 010 ~ 2 011 ~ 3 100 ~ 4 101 ~ 5 110 ~ 6 111 ~ 7 Chapter 3 Data types , Operators , and Expressions
Chapter 3 Data types, Operators, and Expressions o Binary and Hexadecimal 0000~0 0001~1 0010~2 ☆ Binary into Hexadecimal:从右向左,100-3且(不足4 位左补0)01004六进制 0101~5 0110~6 冷 Hexadecimal into Binary:用4位二进011-7一位十 六进制数10008 1001~9 1010~A 1011~B 1100~C 1101~D 1110~E l111~F 例(11010101111012(0011,0101,01111101)2=(357D)16 例(4B9E)6=(0100,1011,1001,1110)2=(100101110011102 5
5 ⚫Binary and Hexadecimal ❖Binary into Hexadecimal :从右向左,每4位一组(不足4 位左补0),转换成十六进制 ❖Hexadecimal into Binary :用4位二进制数代替每一位十 六进制数 例 (11010101111101)2=(0011,0101,0111,1101)2=(357D)16 例 (4B9E)16=(0100,1011,1001,1110)2=(100101110011110)2 0000 ~ 0 0001 ~ 1 0010 ~ 2 0011 ~ 3 0100 ~ 4 0101 ~ 5 0110 ~ 6 0111 ~ 7 1000 ~ 8 1001 ~ 9 1010 ~ A 1011 ~ B 1100 ~ C 1101 ~ D 1110 ~ E 1111 ~ F Chapter 3 Data types , Operators , and Expressions
Chapter 3 Data types, Operators, and Expressions 口 Byte and Bit 765432 0 Memory以Byte为单元组成 每个Byte有一个 Address 一个Byte一般由8个Bt组成 每个Bit的值是0或1 765432 23456789 10
6 ❑Byte and Bit • Memory以Byte为单元组成 • 每个Byte有一个Address • 一个Byte一般由8个Bit组成 • 每个Bit的值是0或1 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 ……... 7 6 4 3 2 5 1 Chapter 3 Data types , Operators , and Expressions
Chapter 3 Data types, Operators, and Expressions 数值的表示方法——原码、反码和补码 原码:最高位为符号位,其余各位为数值本身的绝对值 反码: 正数:反码与原码相同 负数:符号位为1,其余位对原码取反 补码: 负数:最高位为1,其余位为原码取反,再对整个数加1 正数:原码、反码、补码相同 (用一字节表示数) 原码 反码 补码 7 00000111 00000111 00000111 7 10000111 11111000 11111001
7 – 数值的表示方法——原码、反码和补码 • 原码:最高位为符号位,其余各位为数值本身的绝对值 • 反码: – 正数:反码与原码相同 – 负数:符号位为1,其余位对原码取反 • 补码: – 负数:最高位为1,其余位为原码取反,再对整个数加1 – 正数:原码、反码、补码相同 < 原码 反码 补码 +7 00000111 00000111 00000111 -7 10000111 11111000 11111001 (用一字节表示数) Chapter 3 Data types , Operators , and Expressions
Chapter 3 Data types, Operators, and Expressions 3.1 Data types short int integer Int Arithmetic long int types Basic single precision floating point float floating type double precision floating point double Character types char 数据类型决定 enum 1.数据占内存字节数 Data 2.数据取值范围 Types arrays 3.其上可进行的操作 Constructed structures types unIons pointers VOI d
8 3.1 Data types 数据类型决定: 1. 数据占内存字节数 2. 数据取值范围 3. 其上可进行的操作 Chapter 3 Data types , Operators , and Expressions Data Types Constructed types pointers void Character types char enum integer floating single precision floating point float double precision floating point double short int long int int arrays structures unions Basic types Arithmetic types
Chapter 3 Data types, Operators, and Expressions Basic types Type Keywords Bits Available range of values (signed)int 16 -32768~32767 (Signed )short 16 -32768~32767 (signed )long 32 -2147483648~2147483647 integer unsigned int 16 0~65535 unsigned short 16 0~65535 unsigned long 32 0~4294967295 float 32 le-37~le38 floating double 64 le-307~le308 long double 80 le-4931~1e4932 character char 说明∶数据类型所占位数随机器硬件不同而不同,上表以微机为例 ●o9
9 Basic types Type Keywords Bits Available range of values integer (signed)int 16 -32768~32767 (signed)short 16 -32768~32767 (signed)long 32 -2147483648~2147483647 unsigned int 16 0~65535 unsigned long 32 0~4294967295 unsigned short 16 0~65535 float 32 1e-37~1e38 double 64 1e-307~1e308 char 8 说明:数据类型所占位数随机器硬件不同而不同,上表以微机为例: long double 80 1e-4931~1e4932 character Chapter 3 Data types , Operators , and Expressions
Chapter 3 Data types, Operators, and Expressions b3.2 Constants and variables Identifiers(标志符) An identifier is a sequence of letters, digits and underscore The first character must be a letter or an underscore Upper and lower case letters are different Can not use keywords as variable names It's wise to choose variable names that are related to the 例:判断下列标识符号合法性 sum Sum M.D.John day Date 3days student name #33 lotus 1 2 3 char a>b above $123 10
10 3.2 Constants and Variables – Identifiers (标志符) • An identifier is a sequence of letters , digits and underscore _ ; • The first character must be a letter or an underscore _; • Upper and lower case letters are different ; • Can not use keywords as variable names ; • It’s wise to choose variable names that are related to the purpose of the variable ; Notice : such as l and I, o and 0…….. 例:判断下列标识符号合法性 sum Sum M.D.John day Date 3days student_name #33 lotus_1_2_3 char a>b _above $123 M.D.John 3days #33 char a>b $123 Chapter 3 Data types , Operators , and Expressions
Chapter 3 Data types, Operators, and Expressions Constants Symbolic constants: A#define line defines a symbolic name or symbolic constant to be a particular string of characters tdefine identifier replacement text (constant) Use upper case letters It is macro definition例符号常量举例(ch21c) 如# define Direct constants #define PRICe 30 Integer constants main 运行结果:tota=300 Floating-point con Character constant int num. total String constants num=10 total=num PRICE printf("total=%od", total) 11
11 – Use upper case letters – It is macro definition ,not statements. ➢ Direct constants Integer constants Floating-point constants Character constants String constants 如 #define PRICE 30 Constants ➢ Symbolic constants: A #define line defines a symbolic name or symbolic constant to be a particular string of characters . #define identifier replacement text (constant) > > > > 例 符号常量举例(ch2_1.c) #define PRICE 30 main() { int num,total; num=10; total=num*PRICE; printf("total=%d",total); } 运行结果:total=300 Chapter 3 Data types , Operators , and Expressions