《C与指针》第三章练习
本章问题
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 ;
} //注意输出长整型时的格式,一般整型可能包含不了,这是我的机器输出
char:
int :
long int:
SCHAR_MIN: - SCHAR_MAX:
INT_MIN: - INT_MAX:
LONG_MIN: 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 ;
} //我的机器的结果
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 = ,NICKEL = , DIME = ,
QUARTER = , HALF_DOLLAR = , DOLLAR = };
7.What is printed by this fragment(片段) of code?
(下面这个代码段会打印出什么?)
enum Liquid{OUNCE = ,CUP = ,PINT = ,
QUART = ,GALLON = };
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::: warning: format ‘%s’ expects argument of type ‘char *’, but argument 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 ;
}
输出结果是:"-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[] = '-';
puts( szStringB ); return ;
}
运行结果: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 = ; //code 2
int a;
a = ;
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 = ;
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 = ; void
y(void)
{
} int a = ; void
x(void)
{
int c = ;
static float d = ;
}
//个人觉得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?
(找出下面这个程序中所有的错误,你可以尝试编译来确认,在你去掉所有的错误之后,判断每个标识符的存储类型,作用域以及链接属性,每个变量的初始值是多少,有些是重复的变量,它们是同一个吗?每一个函数可以在哪里被调用)
static int w = ;
extern int x;
static float
func1(int a,int b,int c)
{
int c,d,e = ;
...
{
int d,e,w;
...
{
int b,c,d;
static int y = ;
...
}
}
...
{
register int a,d,x;
extern int y;
...
}
}
static int y;
float
func2(int a)
{
extern int y;
static int z;
...
}
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 | - |
//黄色标记为易错点,我就错了...
《C与指针》第三章练习的更多相关文章
- C和指针 第三章--数据
简要概述: <C和指针>第三章对数据进行了描述. 其中主要讲解了---变量的三个属性:作用域.链接属性和存储类型. 这三个属性决定了该变量在“什么地方可以使用”以及“该变量的值能够保持多久 ...
- C和指针 第三章 变量的储存类型 auto、static、register以及static关键词
变量的储存类型决定标量何时创建,何时销毁以及他的值保持多久.有三个地方可以储存变量: 普通内存static 运行时堆栈auto 硬件寄存器register 变量的缺省储存类型取决于它的声明位置: 静态 ...
- C和指针 第三章 链接属性 extern、internal、none
三种链接属性 组成一个程序有多个源文件,如果相同的标识符出现在多个源文件中,那么标识符的链接属性决定如何处理在不同文件中出现的标识符. 链接属性有三种: external:外部 多个源文件中的相同标识 ...
- C和指针 第三章 指针常量与常量指针
c语言中声明常量的两种方式 const int value int const value 如果要声明常量的指针,即指向常量的指针,则可以参考上面的常量声明修改一下 const int *ptr in ...
- C和指针 第三章 习题
在一个源文件中,有两个函数x和y,定义一个链接属性external储存类型static的变量a,且y可以访问,x不可以访问,该如何定义呢? #include <stdio.h> void ...
- C和指针 第三章 四种作用域
代码块作用域: 任何位于一对花括号之间是一个代码块,代码块内声明的标识符具有代码块作用域,嵌套代码块内,内部变量会屏蔽外部相同标示的标示符,非嵌套代码块,不会同时处于活动状态所以不会屏蔽. int m ...
- C和指针 (pointers on C)——第三章——数据
第三章 数据 本章是非常重要的,在特定范围内使用.链接属性.存储类型.const.extern和statickeyword使用.几乎所有的公司是C++在采访的第一个问题. 总结: 具有external ...
- C++第三章课后作业答案及解析---指针的使用
今天继续完成上周没有完成的习题---C++第三章课后作业,本章题涉及指针的使用,有指向对象的指针做函数参数,对象的引用以及友元类的使用方法等 它们具体的使用方法在下面的题目中会有具体的解析(解析标注在 ...
- 《Linux内核设计与实现》读书笔记 第三章 进程管理
第三章进程管理 进程是Unix操作系统抽象概念中最基本的一种.我们拥有操作系统就是为了运行用户程序,因此,进程管理就是所有操作系统的心脏所在. 3.1进程 概念: 进程:处于执行期的程序.但不仅局限于 ...
- 《Linux内核设计与实现》课本第三章自学笔记——20135203齐岳
<Linux内核设计与实现>课本第三章自学笔记 进程管理 By20135203齐岳 进程 进程:处于执行期的程序.包括代码段和打开的文件.挂起的信号.内核内部数据.处理器状态一个或多个具有 ...
随机推荐
- ubunt tmux X Error of failed request
X Error of failed request: BadName (named color or font does not exist) Major opcode of failed req ...
- 12款最佳Linux命令行终端工具, 20款优秀的 Linux 终端仿真器
12款最佳Linux命令行终端工具 如果你跟我一样,整天要花大量的时间使用Linux命令行,而且正在寻找一些可替代系统自带的老旧且乏味的终端软件,那你真是找对了文章.我这里搜集了一些非常有趣的 ...
- STL中的查找算法
STL中有很多算法,这些算法可以用到一个或多个STL容器(因为STL的一个设计思想是将算法和容器进行分离),也可以用到非容器序列比如数组中.众多算法中,查找算法是应用最为普遍的一类. 单个元素查找 1 ...
- AngularJS事件绑定的使用详解
本文和大家分享的主要是AngularJS中事件绑定相关知识点,希望通过本文的分享,对大家学习和使用AngularJS有所帮助. 1.绑定事件:表达式.事件方法名: 2.绑定点击事件实例:显示.隐藏页面 ...
- Linux下使用autoconf 和 automake 编译简单的HelloWorld
使用过开源C/C++项目的同学都知道,标准的编译过程已经变成简单的三部曲:./configure /make/make install,使用起来很方便,不像平时自己写代码,要手写一堆复杂的makefi ...
- 如何myEclipse修改工程项目的运行环境和编译环境
修改工程运行环境(开发环境)JRE 右击工程名-----选择properties----选择对话框左侧的java build path----查看使用的JRE 选择Library选项卡中的,选中使用中 ...
- 【转】 TCP协议中的三次握手和四次挥手(图解)
建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的. 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资 ...
- CentOS PPTP配置FreeRADIUS+DaloRADIUS实现高级用户控制+流量控制
前提条件 阅读本文前,您需要搭建好PPTP,如果仍未搭建,可以参考:http://www.xj123.info/2301.html 如果您需要配置DaloRADIUS,那么您还需要安装LAMP,可以参 ...
- 安装xampp 后 发现 apache 启动不起来
这种事情很常见的.启动不起来 第一,改个端口
- js模拟类
ECMAScript6已经支持了class,但之前版本都不支持类,但是可以通过一些方法来模拟类. js中的类,既是重点,也是难点,很多时候都感觉模棱两可. 首先强调一下js中很重要的3个知识点:thi ...