• 《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
    ",sizeof(char));
        printf("int : %d
    ",sizeof(int));
        printf("long int: %d
    ",sizeof(long int));
    
        printf("SCHAR_MIN: %d	 SCHAR_MAX: %d
    ",SCHAR_MIN,SCHAR_MAX);
        printf("INT_MIN: %d	 INT_MAX:%d
    ",INT_MIN,INT_MAX);
        printf("LONG_MIN: %g	 LONG_MAX:%g
    ",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	 FLT_MIN:%g
    ",FLT_MAX,FLT_MIN);
        printf("DBL_MAX:%g	 DBL_MIN:%g
    ",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
    ",jar);
    jar = jar + PINT;
    printf("%s
    ",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 2distinct 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 -
    //黄色标记为易错点,我就错了...
  • 相关阅读:
    【java开发系列】—— 集合使用方法
    【java开发系列】—— spring简单入门示例
    解决win7远程桌面连接时发生身份验证错误的方法
    eoLinker-AMS接口管理系统
    CentOS 配置mysql允许远程登录
    Linux上安装ZooKeeper并设置开机启动(CentOS7+ZooKeeper3.4.10)
    Cent OS home下中文目录改成英文目录
    解决redis-cli command not found问题
    Centos7使用yum安装Mysql5.7.19的详细步骤(可用)
    取消centOS7虚拟机锁屏
  • 原文地址:https://www.cnblogs.com/monster-prince/p/6039310.html
Copyright © 2020-2023  润新知