本章問題
1.What is the range for characters and the various integer types on your machine?
(在你的機器上,字符型和整型的范圍是多少?)
answer : Depends,look in <limits.h> for the definitions,The location of this include file may vary; on UNIX system it is typically found in the directory /usr/include. For Borland compilers,look in the include directory found where the compiler was installed.
(取決於你的機器,在<limits.h>中查看定義,include文件的位置可能不同,UNIX系統中一般可以在usr/include中找到,Borland編譯器中在你安裝編譯器的目錄下查找)
//懶得去翻include文件了,直接用程序打印出來比較明顯 #include <stdio.h> #include <limits.h> int main(void) { printf("char: %d\n",sizeof(char)); printf("int : %d\n",sizeof(int)); printf("long int: %d\n",sizeof(long int)); printf("SCHAR_MIN: %d\t SCHAR_MAX: %d\n",SCHAR_MIN,SCHAR_MAX); printf("INT_MIN: %d\t INT_MAX:%d\n",INT_MIN,INT_MAX); printf("LONG_MIN: %g\t LONG_MAX:%g\n",LONG_MIN,LONG_MAX); return 0; } //注意輸出長整型時的格式,一般整型可能包含不了,這是我的機器輸出 char: 1 int : 4 long int: 8 SCHAR_MIN: -128 SCHAR_MAX: 127 INT_MIN: -2147483648 INT_MAX:2147483647 LONG_MIN: 0 LONG_MAX:-5.48612e+303
2.What is the range for each of the floating-point types on your machine?
(在你的機器上,每種浮點數類型的范圍是多少?)
answer : Depends,look in <float.h> for the definitions,See above for the location for this file.
(取決於機器,在<float.h>中查看定義)
//同樣直接把它們打印出來 #include <stdio.h> #include <float.h> int main(void) { printf("FLT_MAX:%g\t FLT_MIN:%g\n",FLT_MAX,FLT_MIN); printf("DBL_MAX:%g\t DBL_MIN:%g\n",DBL_MAX,DBL_MIN); return 0; } //我的機器的結果 FLT_MAX:3.40282e+38 FLT_MIN:1.17549e-38 DBL_MAX:1.79769e+308 DBL_MIN:2.22507e-308
3. Suppose you are writing a program that must run on two machines whose default integers are different sizes; 16 bits and 32 bits, The size of long integers on these machines is 32 bits and 64 bits, respectively(分別的), Some of the values used in this program are small enough to fit in a default integer on either machine, but some values are large enought to require 32 bits,One possible solution is to simply use long integers for all values,but this approach wastes time and space on the 16-bit machine for those values that would fit in 16 bits and wastes time and space on the 32-bit machine as well.How can you write the declarations for these variables so that they are the right size for both machines? The correct approach avoids the raced to modify these declarations when compiling the program on either machine.Hint:Try including a header file that contains declarations specific to each particular machine.
(假定你要寫一個程序要在兩台機器上都能運行,它們的默認整型大小不同,分別是16位和32位,長整型大小分別是32位和64位,一些足夠小的值可以適合任意一台機器,但是一些比較大的值要求用32位,一個可能簡單的辦法是對所有的值都使用長整型,但是這種方法對於能夠在16位機器上運行的就會浪費時間和空間,在32位機器上也存在時間和空間的浪費問題,你如何聲明這些變量使他們在兩種機器上都能運行,正確的方法是避免在任意一台機器上編譯程序之前更改它們的聲明,提示:嘗試在一個頭文件中包含對不同機器的特殊聲明)
answer : Declare integer variables that must be a particular size with names like int8 ,int16, int32 ,For integers that you want to be the default size,use names like defint8, defint16, defint32 depending on the largest value they must hold,Then create a file called int_size.h for each machine containning typedef's that select the best integer size for each of your names,On a typical 32-bit machine this file would contain:
(聲明特殊長度的整型變量可以用int8,int16,int32,對你想默認的整型變量的大小,可以使用defint8,defint16,defint32取決於機器所能容納的最大值,然后創建一個int_size.h的文件,每台機器都用typedef用來選擇整型大小,在一般的32位機器上的文件應該包含:)
typedef signed char int8; typedef short int int16; typedef int int32; typedef int defint8; typedef int defint16; typedef int defint32; //具體選擇defint8還是defint16還是defint32,是根據機器所能容納的最大值
On a typical machine with 16-bit integers,the file would contain:
(在一般的16位機器中應該包含:)
typedef signed char int8; typedef int int16; typedef long int int32; typedef int defint8; typedef int defint16; typedef long int defint32;
#define's could also be used.
(也可以使用#difine)
4.Suppose you have a program that assign(賦予) a long integer variable to a short integer variable,What happen when you compile this program?What happens when you run it? Do you think that other compilers will give the same results?
(假定你有一個程序,把一個長整型的變量賦值給短整型的變量,當你編譯這個程序的時候會發生什么呢?運行的時候呢?你認為其他的編譯器也會給出相同的結果嗎?)
answer : Many compilers will give a warning message.The standard defines the runtime behavior roughly(大約) this way:If the value to be assigned is small enough to fit in the shorter variable,its value is preserved;otherwise,it is implementation dependent.The carefully worded description implies(暗示) that the implementation may simply discard(丟棄) the high-order bits that don't fit,which on most machines gives the most efficient object code.This is obviously not portable(方便).
(大部分編譯器會給出警告信息,標准定義按這種方式運行,如果一個值被賦值給一個足夠容納這個值的更小的類型,那么這個值將被保存,否則看情況而定,詳細的描述暗示可能會簡單的丟棄高位值,可能會對大多數機器造成很大的影響,這顯然不是很方便)
5.Suppose you have a program that assigns a double variable to a float.What happens when you compile this program?What happens when you run it?
(如果你把一個double類型的值賦值給float類型,編譯的時候將會發生什么,運行的時候又將發生什么?)
answer : When you compile it,you may get a warning message.The runtime behavior is defined in much the same manner as for integers: If the value fits in the smaller variable,it works;otherwise it is implementation dependent.With floating-point values,though(然而),a value "doesn't fit" only if its exponent(指數) is larger than the shorter type can hold. if the exponent fits.there is still the mantissa(尾數).which might have more significance(有效位) than the shorter type can maintian.In this case,the value is replaced with nearest value that can be represented in the shorter variable;it is implementation dependent whether this rounds(四舍五入),truncates(截去),or does something else.
(當你編譯的時候將會得到一條警告信息,運行的時候類似整型,如果這個值能夠被float裝下就不會改變,否則視情況而定,然而,只有在指數大於短類型的時候可以采用“不適合”的值,如果指數適合,還有尾數的有效位可能多余短類型能存儲的尾數,這種情況下,這個值將會被最接近它的短類型值取代,有可能是四舍五入,也有可能截去或者采取其他措施)
6.Write a declaration for an enumeration that defines values for coins,using the symbols PENNY,NICKEL,and so forth.
(編寫一個枚舉定義硬幣,用PENNY,NICKEL的符號)
answer:
enum Change{ PENNY = 1,NICKEL = 5, DIME = 10, QUARTER = 25, HALF_DOLLAR = 50, DOLLAR = 100};
7.What is printed by this fragment(片段) of code?
(下面這個代碼段會打印出什么?)
enum Liquid{OUNCE = 1,CUP = 8,PINT = 16, QUART = 32,GALLON = 128}; enum Liquid jar; ... jar = QUART; printf("%s\n",jar); jar = jar + PINT; printf("%s\n",jar);
answer : The variable jar is an enumerated type,but its value is actually an integer,However,the printf format code %s is used to print strings,not integers,Consequently,the output cannot be determined.Had the format code been %d,then the output would have been:32 48.
(變量jar是一個枚舉類型,然而它實際是整型,而print格式%s是打印字符串的,不是整型,總的來說,將不會有輸出,將格式碼改為%d后,輸出將為32,48)
//還是會有警告的 main.c:10:8: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘unsigned int’
8.Does your C implementation allow string literals to be modified by the program?Are there any compiler options that allow the modification of string literals to be enabled or disabled?
(你的C工具允許你在程序中更改字符串常量嗎?其他的編譯器有選項可以更改字符串常量的值嗎?)
answer: Depends on the implementation;consult(查詢) the documentation.
(視情況而定,查詢文件)
轉自:http://www.cnblogs.com/yjkai/archive/2011/04/23/2025556.html //請在(且只能在TC2.0)中運行下面代碼,先不要看結果,想想會得到什么: #include<stdio.h> #include<stdlib.h> int main( int argn, char* argv[] ) { char* szStringA = "Hello,world!"; char* szStringB = "Hello,world!"; *szStringA = '-'; puts( szStringB ); return 0; } 輸出結果是:"-ello,world!"; //在linux下反正是段核心錯誤,大家可以去TC2.0中試試 //ANSI C明確說明:修改字符串常量,效果是未定義的。 /* 首先我們得清楚,如何才能得到字符串常量?
只有一種方式:char* szString = “Hello,world!”;
這個聲明得到一個字符串常量。
那么char szString[] = “Hello,world!”;
可以嗎?不可以!這樣得到的是字符串變量。*/ //試試下面代碼: #include<stdio.h> #include<stdlib.h> int main( int argn, char* argv[] ) { char szStringA[] = "Hello,world!"; char szStringB[] = "Hello,world!"; szStringA[0] = '-'; puts( szStringB ); return 0; } 運行結果:Hello,world!
9.If the integer types are normally signed,what is the purpose of the signed keyword?
(如果整型被正確的賦值,那么signed關鍵字的目的是什么?)
answer : It is needed only for charactes,and even then only on machines where the default character is unsigned,It is allowed in other contexts(e.g...signed int) only for consistency(一致).
(符號需要和當機器上默認字符為unsigned int類型的時候,能夠是環境保持一致)
10.Can an unsigned variable hold a larger range of values than a signed variable of the same size?
(一個無符號變量比一個相同大小的有符號變量的范圍大嗎?)
answer:No,In any given number of bits n,there are only 2n distinct combinations(組合) of the bit values,The only thing that changes between a signed and an unsigned value is how half of those values are interpreted(譯碼).In a signed number,they are negative;in an unsigned number,they are the larger positive values.
(不,任意給定位數n,只有2的n次方中不同的位組合,signed和unsigned之間唯一的區別是它們中間的值不同,signed是負數,而unsigned是一個比較大的正數)
11.Assuming that they are both 32 bits long,which type of variable can hold more distinct value,an int or a float?
(假定都是32位長,int類型和float類型哪一個類型能容納更多獨特的值)
answer:The float has a greater range than int,but it cannot have more distinct values without using more bits,The logic in the previous(以前) answer implies(暗示) that they hold the same number of distinct values,but for most floating-point systems this answer is wrong,There are usually lots of representations(表示) for zero,and by using unnormalized fractions(分數,有理數),lots of representations for other values as well,Thus the number of distinct values is less than that of an int.
(浮點型比整型的范圍要大,但是卻沒有比int型更多的位數,也不能存儲更多特殊的值,以前的邏輯暗示我們它們應該能表示相同多的特殊值,不過在大多數浮點型系統中這個答案是錯誤的,0通常有很多表示形式,通過使用不規范的分數,其他值也有許多表示形式,因此float能表示的值少於int )
12.The two fragments of code shown below came from the beginning of a fuction.How do they differ in what they accomplish?
(下面兩個代碼段有什么區別?)
//code 1 int a = 25; //code 2 int a; a = 25;
answer : There is no difference.
13.If the declarations in the code fragments in question 12 included the word const,how would they differ in what they accomplished?
(如果問題12中的代碼段包含關鍵字const,那么它們完成任務的方式有何區別?)
The code 1 declaration still does what it previously did,but the statements on the code 2 are in error,you cannot assign to a constant variable.
(code 1將會按照原來的方式運行,而code 2 將會產生錯的,你不能修改常量)
14.A variable declared in a block may be accessed(訪問) by name from anywhere in that block,True or False?
(一個在代碼塊中聲明的變量可以在有代碼塊的任意地方訪問名字,對還是錯?)
answer:True,except when a nested block declares another variable with the same name,which hides the earlier variable and makes it inaccessible(難以達到) from within the nested block.
(正確,除了在嵌套代碼塊中使用相同名字的變量,將會隱藏之前的變量,使之失效)
15.Suppose function a declares an automatic integer variable called x,You can access the variable x from a different function by using a declaration such as this one.
extern int x;
True or False?
(假定在函數中聲明一個自動變量x,你如果想在別的函數中調用變量x就要使用下面這一行聲明,對還是錯?)
answer:False,The only automatic variables are those with block scope,and these cannot ever be accessed by name from other blocks.
(錯誤,自動變量只有代碼塊作用域並且不能被其他代碼塊訪問)
16.Suppose the variable x in question 15 had been declared static,Does this modification change your answer?
(如果把問題15中x變量改為static聲明,是否可以改變你之前的回答?)
answer : No ,That chages its storage class,but not its scope,the answer is still false.
(不,這只是改變它的存儲類型,不改變作用域,答案還是錯誤)
17.Suppose the file a.c begins with this declaration:
int x;
What declarationg( if any) would be needed to access this variable from a function found later in the same file?
(假定在a.c文件的開始聲明int x;如果你需要在這個文件中后面的函數中訪問這個變量,你需要聲明什么?)
answer:None is needed
(什么都不需要)
18.Suppose the declaration in question17 included the keyword static,would this modification change your answer?
(在問題17中,如果包括關鍵字static,那么你的答案是否發生改變?)
answer:NO,it still has internal linkage,so every funcion that comes later in the file may access it.
(不發生改變,它仍然是內部鏈接屬性,所以在這個文件中的任何函數都可以訪問它)
//並且它的作用域是文件作用域
19.Suppose the file a.c begins with this declaration:
int x;
What declarationg( if any) would be needed to access this variable from a function found in different source file?
(假定在a.c文件的開始聲明int x;如果你需要在不同源文件中的函數中訪問這個變量,你需要聲明什么?)
answer:
extern int x;
20.Suppose the declaration in qustion 19 included the keyword static.Would this modification change your answer?
(如果在問題19中的聲明包括關鍵字static,你會更改你的答案嗎?)
answer:Yes,Now ther is no declaration that will enable you to access x from a different source file,
(是的,現在你不能在別的源文件中使用x變量)
21.Suppose a function that contains automatic variables is called twice in a row,Is it possible that the variables will have the same vlues at the beginning of the second call that they had at the end of the first call?
(假設函數中有一個自動變量,這個函數在同一行中被調用了兩次,這個變量在第二次調用的開始和第一次調用的結束的值是否有可能相同)
anwser:Yes,it is possible,but you should not count on it,It is also quite possible that they will not,even if ther were no intervening(干涉) function calls,On some architectures(結構),a hardware interrupt will push machine state information on the stack,which could destroy the variables.
(是的,有可能相同,不過你不應該去計算它是否相同,也有可能不同,即使中途沒有函數調用干涉,在一些結構中,硬件中斷會改變機器堆棧中的信息,會銷毀變量)
22.What is the difference in the behavior of the declaration
int a = 5;
When it appears in side of a function as compared to when it appears outside?
(下面這個聲明發生在函數內和函數外有何不同?)
answer:Inside:the variable is automatic,it is reinitialized each time the function is called.its scope is limited to the function,it has no linkage.Outside:the variable is static,it is initialized only once before the program begins to run,it has file scope,and external linkage.
(在里面的時候,是一個自動變量,每次調用這個函數時,變量都會被重新初始化,它的作用域是在函數內部,沒有鏈接屬性。在外面的時候,是靜態變量,只有在程序運行之前就被初始化一次,且僅初始化一次,作用域為文件作用域,並且擁有外部鏈接屬性)
23.Suppose you wanted to write two function,x and y,in the same source file,that use the following variables:
Name | Type | Storage Class | Linkage | Scope | Initialized to |
a | int | static | external | accessible to x;not y | 1 |
b | char | static | none | accessible to x and y | 2 |
c | int | automatic | none | local to x | 3 |
d | float | static |
none | local to x | 4 |
How and where would you write the decaration for these variables?
note:All initialization must be made in the declarations themselves,not by any executable statements in the function.
(假定你要寫兩個函數x和y,兩個函數在同一個源文件中,它們要使用下列變量,你將怎么聲明這些變量,提示:所有初始化必須在聲明的時候完成,而不是通過調用語句來執行初始化)
answer:
static char b = 2; void y(void) { } int a = 1; void x(void) { int c = 3; static float d = 4; }
//個人覺得b應該是有internal的鏈接屬性
24.Identify(識別) any errors contained in the following program,(you may wish to try compiling it to be sure) After you have removed the errors,determine(判斷) the storage class,scope and linkage of every identifier(標識符),What will be the initial value of each variable?There are many duplicate(重復的) identifiers;do they refer to the same variable or no different ones?From where can each of these function be called?
(找出下面這個程序中所有的錯誤,你可以嘗試編譯來確認,在你去掉所有的錯誤之后,判斷每個標識符的存儲類型,作用域以及鏈接屬性,每個變量的初始值是多少,有些是重復的變量,它們是同一個嗎?每一個函數可以在哪里被調用)
1 static int w = 5; 2 extern int x;
3 static float 4 func1(int a,int b,int c) 5 { 6 int c,d,e = 1; 7 ... 8 { 9 int d,e,w; 10 ... 11 { 12 int b,c,d; 13 static int y = 2; 14 ... 15 } 16 } 17 ... 18 { 19 register int a,d,x; 20 extern int y; 21 ... 22 } 23 }
24 static int y;
25 float 26 func2(int a) 27 { 28 extern int y; 29 static int z; 30 ... 31 }
answer:There is one error:The declaration of c in line 6 confilcts with the function parameter c.some compilers have been seen to flag line24 as an error,saying it confilcts with the declaration in line 20,This should not be an error,as the scope of y from line 20 runs out at line22,so there is no conflict.
(只有一個錯誤,第六行的c變量與函數形參沖突,有些編譯器認為24行的y變量也錯了,說與20行的y變量沖突,但它們應該不算一個錯誤,因為20行的y變量作用域只有20到22,所以這不沖突)
Name(Line) | Storage Class | Scope | Linkage | Initial Value |
w(1) | static | file 1-8; 17-31 |
internal | 5 |
x(2) | static | file 2-18; 23-31 |
external | 0 |
a(4) | automatic | prototype 5-18; 23 |
none | * |
b(4) | automatic | prototype 5-11; 16-23 |
none | * |
c(4) | automatic | prototype 5-11; 16-23 |
none | * |
d(6) | automatic | block 6-8; 17,23 |
none | - |
e(6) | automatic | block 6-8; 17-23 |
none | 1 |
d(9) | automatic | block 9-11; 16 |
none | - |
e(9) | automatic | block 9-16 |
none | - |
w(9) | automatic | block 9-16 |
none | - |
b(12) | automatic | block 12-15 |
none | - |
c(12) | automatic | block 12-15 |
none | - |
d(12) | automatic | block 12-15 |
none | - |
y(13) | static | block 13-15 |
none | 2 |
a(19) | register | block 19-22 |
none | - |
d(19) | register | block 19-22 |
none | - |
x(19) | register | block 19-22 |
none | - |
y(20) | static | block 20-22 |
external | 0 |
y(24) | static | file 24-31 |
internal | 0 |
a(26) | automatic | prototype 27-31 |
none | * |
y(28) | static | block 28-31 |
internal | - |
z(29) | static | block 29-31 |
none | - |
func1 | file 4-31 |
internal | - | |
func2 | file 26-31 |
external | - |
//黃色標記為易錯點,我就錯了...