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