32768 1 0 1 32767 0111 1111 32766









![5. 1. 2 基本数据类型 n 整型 有符号整型 无符号整型 数据长度 int unsigned [int] 16或 32位 5. 1. 2 基本数据类型 n 整型 有符号整型 无符号整型 数据长度 int unsigned [int] 16或 32位](https://slidetodoc.com/presentation_image_h/1eff39b8fc1df3b937cd7dc4b67c2172/image-10.jpg)
![基本数据类型-整型 扩展的整数类型:short long unsigned [int] 有符号整型 无符号整型 数据长度 int unsigned [int] 16或 32位 short 基本数据类型-整型 扩展的整数类型:short long unsigned [int] 有符号整型 无符号整型 数据长度 int unsigned [int] 16或 32位 short](https://slidetodoc.com/presentation_image_h/1eff39b8fc1df3b937cd7dc4b67c2172/image-11.jpg)
![整数类型的取值范围 int 32位 -2147483648 ~ 2147483647 -231 ~ 231 -1 short [int] 16位 -32768 整数类型的取值范围 int 32位 -2147483648 ~ 2147483647 -231 ~ 231 -1 short [int] 16位 -32768](https://slidetodoc.com/presentation_image_h/1eff39b8fc1df3b937cd7dc4b67c2172/image-12.jpg)






























































- Slides: 74
-32768 -1 0 1 32767 0111 1111 32766 0111 1111 1110 32765 0111 1111 1101 …… 1 0000 0001 0 0000 -1 1111 -2 1111 1110 …… -32767 1000 0000 0001 -32768 1000 0000 32767 + 1 = 32768 ? 1000 0000 = -32768 - 1 = -32769 ? 0111 1111 = 32767 8
5. 1. 2 基本数据类型 n 整型 有符号整型 无符号整型 数据长度 int unsigned [int] 16或 32位 short [int] unsigned short [int] 16位 long [int] unsigned long [int] 32位 n 字符型 char 8位 n 实型(浮点型) 单精度浮点型 float 32位 双精度浮点型 double 64位 10
基本数据类型-整型 扩展的整数类型:short long unsigned [int] 有符号整型 无符号整型 数据长度 int unsigned [int] 16或 32位 short [int] unsigned short [int] 16位 long [int] unsigned long [int] 32位 有符号 short 1 0000 -32768 -215 0 1111 32767 215 -1 无符号 unsigned short 0000 0 1111 65535 216 -1 11
整数类型的取值范围 int 32位 -2147483648 ~ 2147483647 -231 ~ 231 -1 short [int] 16位 -32768 ~ 32767 -215 ~ 215 -1 long [int] 32位 -2147483648 ~ 2147483647 -231 ~ 231 -1 unsigned [int] 32位 0 ~ 4294967295 0 ~ 232 -1 unsigned short [int] 16位 0 ~ 65535 0 ~ 216 -1 unsigned long [int] 32位 0 ~ 4294967295 0 ~ 232 -1 12
例5 -1 求球的表面积和体积 # include <stdio. h> # define PI 3. 14 int main(void) { double r, s, v; 加分号? printf(“Enter r: "); scanf("%lf", &r); s = 4. 0 * PI * r ; v = 4. 0 / 3. 0 * PI * r * r ; printf("s=%f, v=%fn", s, v); return 0; } 通常用大写字母 18
符号常量的优点 # include <stdio. h> int main(void) { double r, s, v; # include <stdio. h> 3. 14159 # define PI 3. 14 int main(void) { double r, s, v; scanf("%lf", &r); 3. 14159 s = 4. 0*r*r*3. 14 ; v = 4. 0/3. 0*r*r*r*3. 14 ; 3. 14159 . . . return 0; } scanf("%lf", &r); s = 4. 0*PI*r*r ; v = 4. 0/3. 0*PI*r*r*r ; . . . return 0; } 19
例6 -2(1) 输出整型数据 # include <stdio. h> int main(void) { 10, 12, a 10, 8, 16 10, a printf("%d, %o, %xn", 10, 10); printf("%d, %dn", 10, 0 x 10); printf("%d, %xn", 012); return 0; } 32
例6 -2(2) 输入整型数据 # include <stdio. h> input a, b: 17 17 int main(void) 15 17 { f, 17 int a, b; printf("input a, b: "); scanf("%o%d", &a, &b); %x%d printf("%d%5 dn", a, b); printf("%x, %dn", a, b); return 0; } 33
实型数据输出示例 # include <stdio. h> 3. 141593, 3. 14159 e+00 int main(void) 3. 142, 3. 14, 3. 14 { double d = 3. 1415926; printf("%f, %en", d, d); printf("%5. 3 f, %5. 2 f, %. 2 fn", d, d, d); return 0; } 一共 5位,小数 3位,小数点一位 35
实型数据输入输出示例 假定float的精度为 7位,double的精度为 16位 # include <stdio. h> input f, d: int main(void) 1234567890123. 123456 { 1234567890123. 123456 float f; f = 1234567954432. 000000 double d; d = 1234567890123. 123540 printf("input f, d: "); d = 1234567890123. 120120 scanf("%f%lf", &f, &d); printf("f = %fn d = %f n", f, d); d = 1234567890123. 12; printf("d = %f n", d); return 0; } 36
6. 3. 3 字符型数据输入输出 n scanf() 和 printf() %c char ch; scanf("%c", &ch); printf("%c", ch); n getchar() 和 putchar() char ch; ch = getchar( ); putchar(ch); 输入输出一个字符 37
例6 -3(1) 输入输出字符示例 # include <stdio. h> int main(void) { char ch 1, ch 2; ch 1=getchar(); ch 2=getchar(); putchar(ch 1); putchar('#'); putchar(ch 2); return 0; } Ab A#b 38
例6 -3(2) 输入输出字符示例 # include <stdio. h> int main(void) { char ch 1, ch 2, ch 3; Ab. C A#b#C A b. C A# #b scanf("%c%c%c", &ch 1, &ch 2, &ch 3); printf("%c%c%c", ch 1, '#', ch 2, '#', ch 3); return 0; } 39
'b'的ASCII码98 例6 -4 # include <stdio. h> int main(void) { char ch = 'b'; 输出字符型数据 b, 98 a, 97 B, 66 printf("%c, %dn", 'b'); printf("%c, %dn", 98); printf("%c, %dn", 97, 'b'-1); printf("%c, %dn", ch - 'a' + 'A'); return 0; } 40
6. 4. 1 自动类型转换(非赋值运算) n n 水平方向:自动 垂直方向:低 高 高 double float unsigned long unsigned short 低 int char, short 43
自动类型转换(非赋值运算) 'A' + 12 – 10. 05 65 77 66. 95 高 double float unsigned long unsigned short 低 int char, short 44
自动类型转换(赋值运算) double x; x = 1; x = ? int ai; ai = 2. 56; ai = ? short a = 1000; char b = 'A'; long c; c = ? c = a + b; short bi; bi = 0 x 12345678 L bi = ? 46
5. 4. 2 强制类型转换运算符 (类型名) 表达式 (double)3 (int)3. 8 (double)(5/2) (double)5/2 3. 0 3 2. 0 2. 5 47
强制类型转换示例 # include <stdio. h> int main(void) x = 3. 800000, i = 3 { (double)(int)x = 3. 000000 int i; x mod 3 = 0 double x; x = 3. 8; i = (int) x; printf("x = %f, i = %d n", x, i); printf("(double)(int)x = %fn", (double)(int)x); printf(" x mod 3 = %dn", (int)x % 3); return 0; } 48
自增运算和自减运算 int n, m; n=n+1 m=n n=2; m=++n; m=n+1 n=2; m=n++; n=3 m=3 n=3 m=2 52
6. 5. 5 条件表达式 exp 1 ? exp 2 : exp 3 0 非 0 exp 1 int n; (n>0) ? 2. 9 : 1 2. 9 n = 10 n = -10 1. 0 x+2 x>0 y = x 2 x<=0 y = (x>0) ? x+2 : x*x; if ( a>b ) z = a; else z = b; if ( x>0 ) y=x+2; else y=x*x; z = (a>b) ? a : b; 65
逗号表达式的用途 sum = 0; for(i = 0; i <= 100; i++) sum = sum + i; for(i = 0, sum = 0; i <= 100; i++) sum += i; 67
6. 5. 9 程序解析-大小写字母转换 #include <stdio. h> int main(void) { char ch; input 10 characters: Reold 123? r. EOLD 123? printf("input characters: "); ch = getchar(); while((ch = getchar()) != 'n') while(ch != 'n'){ if(ch >= 'A' && ch <= 'Z') ch = ch - 'A' + 'a'; else if((ch >= 'a' && ch <= 'z' ) ch = ch - 'a' + 'A'; putchar(ch); ch = getchar(); (ch = getchar()) != 'n' } ch = getchar() != 'n' rerurn 0; 等价吗? } 74