第3章 數據和C
復習題
1、對下面的各種數據使用合適的數據類型:
a.East Simpleton的人口
b.DVD影碟的價格
c.本章出現次數最多的字母
d.這個字母出現的次數
答:a.int類型,可以是short、unsigned或unsigned short;人口數是一個整數。
b.float類型;價格不太可能正好是一個整數(您也可以使用double,但實際上并不需要那么高的精度)。
c.char類型。
d.int類型,可以是unsigned。
2、需要用long類型變量代替int類型變量的原因是什么?
答:一個原因是在您的系統中long可以容納比int類型更大的數;一個原因是如果您確實需要處理更大的值,那么使用一種在所有系統上都能保證至少是32位的類型會使程序的可移植性更好。
3、獲得一個32位的有符號整數,可以使用哪些可移植的數據類型?每種選擇的原因是什么?
答:要獲得正好是32位的數,您可以使用int32_t(如果在您的系統上有這一定義的話)。要獲得可存儲至少32位的最小類型,可以使用int_least32_t。如果要在32位的類型中獲得提供最快計算速度的類型,可以選擇int_fast32_t。(參考3.4.5 可移植的類型:inttypes.h,理解的不是很清楚!!!)
4、指出下列常量的類型和意義(如果有的話):
a.'\b'
b.1066
c.99.44
d.0XAA
e.2.0e30
答:a.char常量(但以int類型存儲)
b.int常量
c.double常量
d.unsigned int常量,十六進制格式
e.double常量
5、Dottie Cawm寫的下面這個程序中有很多錯誤,找出這些錯誤。
第2行:應該是int main(void)
第3行:使用{,而不是(
第4行:在g和h之間應該是逗號而不是分號
第5行:無錯誤
第6行:(空行)無錯誤
第7行:在e之前應該至少有一個數字,.1e21或1.e21都是正確的,盡管這個數有點大。
第8行:無錯誤,至少在語法上沒有
第9行:使用},而不是)
缺少的行:首先,rate沒有被賦值。其次,變量h從來沒有被使用。而且程序永遠不會把它的計算結果通知給您。這些錯誤都不會阻止程序的運行(盡管可能會向您出示一個警告以說明變量沒有被使用),但是它們確實減弱了程序本來就不多的功能。而且在結尾處應該有一個return語句。
下面是正確版本之一:
答:
7、指出下表中各常量的數據類型(在聲明語句中使用的數據類型)及其在printf()中的格式說明符,假設int類型為16位長。
答:
8、假設一個程序開始處有如下的聲明:
1 printf("The odds against the %d were %ld to 1.\n", imate, shot);
2 printf("A score of %f is not an %c grade.\n", log, grade);
9、假設ch為char類型變量。使用轉義序列、十進制值、八進制字符常量以及十六進制字符常量等方法將其賦值為回車符(假設使用ASCII編碼)。
答:
b.反斜線字符
c.雙引號字符
d.制表字符
編程練習(如有錯誤,希望指正!!!)
1、
2、
復習題
1、對下面的各種數據使用合適的數據類型:
a.East Simpleton的人口
b.DVD影碟的價格
c.本章出現次數最多的字母
d.這個字母出現的次數
答:a.int類型,可以是short、unsigned或unsigned short;人口數是一個整數。
b.float類型;價格不太可能正好是一個整數(您也可以使用double,但實際上并不需要那么高的精度)。
c.char類型。
d.int類型,可以是unsigned。
2、需要用long類型變量代替int類型變量的原因是什么?
答:一個原因是在您的系統中long可以容納比int類型更大的數;一個原因是如果您確實需要處理更大的值,那么使用一種在所有系統上都能保證至少是32位的類型會使程序的可移植性更好。
3、獲得一個32位的有符號整數,可以使用哪些可移植的數據類型?每種選擇的原因是什么?
答:要獲得正好是32位的數,您可以使用int32_t(如果在您的系統上有這一定義的話)。要獲得可存儲至少32位的最小類型,可以使用int_least32_t。如果要在32位的類型中獲得提供最快計算速度的類型,可以選擇int_fast32_t。(參考3.4.5 可移植的類型:inttypes.h,理解的不是很清楚!!!)
4、指出下列常量的類型和意義(如果有的話):
a.'\b'
b.1066
c.99.44
d.0XAA
e.2.0e30
答:a.char常量(但以int類型存儲)
b.int常量
c.double常量
d.unsigned int常量,十六進制格式
e.double常量
5、Dottie Cawm寫的下面這個程序中有很多錯誤,找出這些錯誤。
1 include <stdio.h>
2 main
3 (
4 float g; h;
5 float tax, rate;
6
7 g = e21;
8 tax = rate * g;
9 )
答:第1行:應該是#include <stdio.h>2 main
3 (
4 float g; h;
5 float tax, rate;
6
7 g = e21;
8 tax = rate * g;
9 )
第2行:應該是int main(void)
第3行:使用{,而不是(
第4行:在g和h之間應該是逗號而不是分號
第5行:無錯誤
第6行:(空行)無錯誤
第7行:在e之前應該至少有一個數字,.1e21或1.e21都是正確的,盡管這個數有點大。
第8行:無錯誤,至少在語法上沒有
第9行:使用},而不是)
缺少的行:首先,rate沒有被賦值。其次,變量h從來沒有被使用。而且程序永遠不會把它的計算結果通知給您。這些錯誤都不會阻止程序的運行(盡管可能會向您出示一個警告以說明變量沒有被使用),但是它們確實減弱了程序本來就不多的功能。而且在結尾處應該有一個return語句。
下面是正確版本之一:
1 #include <stdio.h>
2 int main(void)
3 {
4 float g, h;
5 float tax, rate;
6 rate = 0.08;
7 g = 1.0e5;
8
9 tax = rate * g;
10 h = g + tax;
11 printf("You owe $%f plus $%f in taxes for a total of $%f.\n", g, tax, h);
12 return 0;
13 }
6、指出下表中各常量的數據類型(在聲明語句中使用的數據類型)及其在printf()中的格式說明符。2 int main(void)
3 {
4 float g, h;
5 float tax, rate;
6 rate = 0.08;
7 g = 1.0e5;
8
9 tax = rate * g;
10 h = g + tax;
11 printf("You owe $%f plus $%f in taxes for a total of $%f.\n", g, tax, h);
12 return 0;
13 }
常量 | 類型 | 說明符 | |
12 | |||
0x3 | |||
'C' | |||
2.34E07 | |||
'\040' | |||
7.0 | |||
6L | |||
6.0f |
答:
常量 | 類型 | 說明符 | |
12 | int | %d | |
0x3 | unsigned int | %#x | |
'C' | char | %c | |
2.34E07 | double | %f | |
'\040' | char | %c | |
7.0 | double | %f | |
6L | long | %ld | |
6.0f | float | %e |
7、指出下表中各常量的數據類型(在聲明語句中使用的數據類型)及其在printf()中的格式說明符,假設int類型為16位長。
常量 | 類型 | 說明符 | |
012 | |||
2.9e05L | |||
's' | |||
100000 | |||
'\n' | |||
20.0f | |||
0x44 | |||
答:
常量 | 類型 | 說明符 | |
012 | int | %#0 | |
2.9e05L | long double | %Le | |
's' | char | %c | |
100000 | long | %ld | |
'\n' | char | %c | |
20.0f | float | %f | |
0x44 | unsigned int | %#x | |
8、假設一個程序開始處有如下的聲明:
1 int imate = 2;
2 long shot = 53456;
3 char grade = 'A';
4 float log = 2.71828;
在下面printf()語句中添上合適的類型說明符:2 long shot = 53456;
3 char grade = 'A';
4 float log = 2.71828;
1 printf("The odds against the %___ were %___ to 1.\n", imate, shot);
2 printf("A score of %___ is not an %___ grade.\n", log, grade);
答:2 printf("A score of %___ is not an %___ grade.\n", log, grade);
1 printf("The odds against the %d were %ld to 1.\n", imate, shot);
2 printf("A score of %f is not an %c grade.\n", log, grade);
9、假設ch為char類型變量。使用轉義序列、十進制值、八進制字符常量以及十六進制字符常量等方法將其賦值為回車符(假設使用ASCII編碼)。
答:
1 char ch = '\r';
2 char ch = 13;
3 char ch = '\015';
4 char ch = '\xd';
10、改正下面程序(在C中/表示除法)。2 char ch = 13;
3 char ch = '\015';
4 char ch = '\xd';
1 void main(int) / this progarm is perfect /
2 {
3 cows, legs integer;
4 printf("How many cow legs did you count?\n);
5 scanf("%c", legs);
6 cows = legs / 4;
7 printf("That implies there are %f cows.\n", cows)
8 }
答:2 {
3 cows, legs integer;
4 printf("How many cow legs did you count?\n);
5 scanf("%c", legs);
6 cows = legs / 4;
7 printf("That implies there are %f cows.\n", cows)
8 }
1 #include <stdio.h>
2 int main(void) /* this progarm is perfect */
3 {
4 int cows, legs;
5 printf("How many cow legs did you count?\n");
6 scanf("%d", &legs);
7 cows = legs / 4;
8 printf("That implies there are %d cows.\n", cows);
9 return 0;
10 }
11、指出下列轉義字符的含義:2 int main(void) /* this progarm is perfect */
3 {
4 int cows, legs;
5 printf("How many cow legs did you count?\n");
6 scanf("%d", &legs);
7 cows = legs / 4;
8 printf("That implies there are %d cows.\n", cows);
9 return 0;
10 }
1 a.\n
2 b.\\
3 c.\"
4 d.\t
答:a.換行字符2 b.\\
3 c.\"
4 d.\t
b.反斜線字符
c.雙引號字符
d.制表字符
編程練習(如有錯誤,希望指正!!!)
1、
1 /* 整數上溢*/
2 #include <stdio.h>
3 int main(void)
4 {
5 int i = 2147483647;
6 unsigned int j = 4294967295;
7
8 /*
9 無符號整數j像一個汽車里程指示表(形容的太好了,可參考《計算機科學導論》第3章 數據存儲,有圖),
10 當達到最大值時,它將溢出到起始點。整數i也是同樣。它們的主要區別是unsigned int變量j的起始點是0(正像里程
11 指示表那樣),而int變量i的起始點則是-2147483648。——參考《C Primer Plus》
12 */
13 printf("%d %d %d\n", i, i+1, i+2);
14 printf("%u %u %u\n", j, j+1, j+2);
15 return 0;
16 }
運行結果:2 #include <stdio.h>
3 int main(void)
4 {
5 int i = 2147483647;
6 unsigned int j = 4294967295;
7
8 /*
9 無符號整數j像一個汽車里程指示表(形容的太好了,可參考《計算機科學導論》第3章 數據存儲,有圖),
10 當達到最大值時,它將溢出到起始點。整數i也是同樣。它們的主要區別是unsigned int變量j的起始點是0(正像里程
11 指示表那樣),而int變量i的起始點則是-2147483648。——參考《C Primer Plus》
12 */
13 printf("%d %d %d\n", i, i+1, i+2);
14 printf("%u %u %u\n", j, j+1, j+2);
15 return 0;
16 }
2147483647 -2147483648 -2147483647
4294967295 0 1
浮點數的上溢和下溢???(理解的不是很清楚,回頭再來寫)4294967295 0 1
2、
1 #include <stdio.h>
2 int main(void)
3 {
4 int asc;
5 printf("Please enter an ASCII value: ");
6 scanf("%d", &asc);
7 printf("The code is %c.\n", asc);
8 return 0;
9 }
3、2 int main(void)
3 {
4 int asc;
5 printf("Please enter an ASCII value: ");
6 scanf("%d", &asc);
7 printf("The code is %c.\n", asc);
8 return 0;
9 }
1 #include <stdio.h>
2 int main(void)
3 {
4 printf("\aStartled by the sudden sound, Sally shouted, ");
5 printf("\"By the Great Pumpkin,what was that!\"\n");
6 return 0;
7 }
4、2 int main(void)
3 {
4 printf("\aStartled by the sudden sound, Sally shouted, ");
5 printf("\"By the Great Pumpkin,what was that!\"\n");
6 return 0;
7 }
1 #include <stdio.h>
2 int main(void)
3 {
4 float number;
5 printf("Please enter a float value: ");
6 scanf("%f", &number);
7 printf("The input is %f or %e", number, number);
8 return 0;
9 }
5、2 int main(void)
3 {
4 float number;
5 printf("Please enter a float value: ");
6 scanf("%f", &number);
7 printf("The input is %f or %e", number, number);
8 return 0;
9 }
1 #include <stdio.h>
2 int main(void)
3 {
4 int age;
5 printf("Please enter your age: ");
6 scanf("%d", &age);
7 printf("Your age has %e s", age*3.156e7);
8 return 0;
9 }
6、2 int main(void)
3 {
4 int age;
5 printf("Please enter your age: ");
6 scanf("%d", &age);
7 printf("Your age has %e s", age*3.156e7);
8 return 0;
9 }
1 #include <stdio.h>
2 int main(void)
3 {
4 int num; // 夸脫數應該為整數吧!!
5 printf("Please enter water: ");
6 scanf("%d", &num);
7 printf("The water has %e ", num*950/3.0e-23);
8 return 0;
9 }
7、2 int main(void)
3 {
4 int num; // 夸脫數應該為整數吧!!
5 printf("Please enter water: ");
6 scanf("%d", &num);
7 printf("The water has %e ", num*950/3.0e-23);
8 return 0;
9 }
1 #include <stdio.h>
2 int main(void)
3 {
4 float height;
5 printf("Please enter your height: ");
6 scanf("%f", &height);
7 printf("Your height is %.2f cm.\n", height*2.54);
8 return 0;
9 }
2 int main(void)
3 {
4 float height;
5 printf("Please enter your height: ");
6 scanf("%f", &height);
7 printf("Your height is %.2f cm.\n", height*2.54);
8 return 0;
9 }