第3章 數(shù)據(jù)和C
復(fù)習(xí)題
1、對下面的各種數(shù)據(jù)使用合適的數(shù)據(jù)類型:
a.East Simpleton的人口
b.DVD影碟的價(jià)格
c.本章出現(xiàn)次數(shù)最多的字母
d.這個(gè)字母出現(xiàn)的次數(shù)
答:a.int類型,可以是short、unsigned或unsigned short;人口數(shù)是一個(gè)整數(shù)。
b.float類型;價(jià)格不太可能正好是一個(gè)整數(shù)(您也可以使用double,但實(shí)際上并不需要那么高的精度)。
c.char類型。
d.int類型,可以是unsigned。
2、需要用long類型變量代替int類型變量的原因是什么?
答:一個(gè)原因是在您的系統(tǒng)中l(wèi)ong可以容納比int類型更大的數(shù);一個(gè)原因是如果您確實(shí)需要處理更大的值,那么使用一種在所有系統(tǒng)上都能保證至少是32位的類型會使程序的可移植性更好。
3、獲得一個(gè)32位的有符號整數(shù),可以使用哪些可移植的數(shù)據(jù)類型?每種選擇的原因是什么?
答:要獲得正好是32位的數(shù),您可以使用int32_t(如果在您的系統(tǒng)上有這一定義的話)。要獲得可存儲至少32位的最小類型,可以使用int_least32_t。如果要在32位的類型中獲得提供最快計(jì)算速度的類型,可以選擇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常量,十六進(jìn)制格式
e.double常量
5、Dottie Cawm寫的下面這個(gè)程序中有很多錯(cuò)誤,找出這些錯(cuò)誤。
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行:應(yīng)該是#include <stdio.h>
第2行:應(yīng)該是int main(void)
第3行:使用{,而不是(
第4行:在g和h之間應(yīng)該是逗號而不是分號
第5行:無錯(cuò)誤
第6行:(空行)無錯(cuò)誤
第7行:在e之前應(yīng)該至少有一個(gè)數(shù)字,.1e21或1.e21都是正確的,盡管這個(gè)數(shù)有點(diǎn)大。
第8行:無錯(cuò)誤,至少在語法上沒有
第9行:使用},而不是)
缺少的行:首先,rate沒有被賦值。其次,變量h從來沒有被使用。而且程序永遠(yuǎn)不會把它的計(jì)算結(jié)果通知給您。這些錯(cuò)誤都不會阻止程序的運(yùn)行(盡管可能會向您出示一個(gè)警告以說明變量沒有被使用),但是它們確實(shí)減弱了程序本來就不多的功能。而且在結(jié)尾處應(yīng)該有一個(gè)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、指出下表中各常量的數(shù)據(jù)類型(在聲明語句中使用的數(shù)據(jù)類型)及其在printf()中的格式說明符。
| 常量 | 類型 | 說明符 |
| 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、指出下表中各常量的數(shù)據(jù)類型(在聲明語句中使用的數(shù)據(jù)類型)及其在printf()中的格式說明符,假設(shè)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、假設(shè)一個(gè)程序開始處有如下的聲明:
1 int imate = 2;
2 long shot = 53456;
3 char grade = 'A';
4 float log = 2.71828;
在下面printf()語句中添上合適的類型說明符:
1 printf("The odds against the %___ were %___ to 1.\n", imate, shot);
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、假設(shè)ch為char類型變量。使用轉(zhuǎn)義序列、十進(jìn)制值、八進(jìn)制字符常量以及十六進(jìn)制字符常量等方法將其賦值為回車符(假設(shè)使用ASCII編碼)。
答:
1 char ch = '\r';
2 char ch = 13;
3 char ch = '\015';
4 char ch = '\xd';
10、改正下面程序(在C中/表示除法)。
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 }
答:
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、指出下列轉(zhuǎn)義字符的含義:
1 a.\n
2 b.\\
3 c.\"
4 d.\t
答:a.換行字符
b.反斜線字符
c.雙引號字符
d.制表字符
編程練習(xí)(如有錯(cuò)誤,希望指正!!!)1、
1 /* 整數(shù)上溢*/
2 #include <stdio.h>
3 int main(void)
4 {
5 int i = 2147483647;
6 unsigned int j = 4294967295;
7
8 /*
9 無符號整數(shù)j像一個(gè)汽車?yán)锍讨甘颈恚ㄐ稳莸奶昧耍蓞⒖肌队?jì)算機(jī)科學(xué)導(dǎo)論》第3章 數(shù)據(jù)存儲,有圖),
10 當(dāng)達(dá)到最大值時(shí),它將溢出到起始點(diǎn)。整數(shù)i也是同樣。它們的主要區(qū)別是unsigned int變量j的起始點(diǎn)是0(正像里程
11 指示表那樣),而int變量i的起始點(diǎn)則是-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 }
運(yùn)行結(jié)果:
2147483647 -2147483648 -2147483647
4294967295 0 1
浮點(diǎn)數(shù)的上溢和下溢???(
理解的不是很清楚,回頭再來寫)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、
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、
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、
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、
1 #include <stdio.h>
2 int main(void)
3 {
4 int num; // 夸脫數(shù)應(yīng)該為整數(shù)吧!!
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、
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 }