对K&R第2版的怀疑

roo*_*kea 0 c kernighan-and-ritchie c89

1. 8.2页面 Low Level I/O - Read and Write

    #include "syscalls.h"  
    int getchar(void)  
    {  
        char c;  
        return (read(0, &c, 1) == 1) ? (unsigned char) c : EOF;  
    }  
Run Code Online (Sandbox Code Playgroud)

unsigned char在return语句中转换c可以消除符号扩展的任何问题.

我们在这里谈论什么问题?为什么unsigned char呢?没有演员unsigned char会怎么样?

2. A7.4.8第204页 Sizeof Operator

当应用于结构或联合时,结果是对象中的字节数,包括使对象平铺数组所需的任何填充:n个元素的数组大小是一个元素大小的n倍

将对象平铺为数组是什么意思?我们在讨论结构和联合,突然之间这个数组是如何出现的?这个肯定看起来像一个错字,但我已经检查了勘误表的所有查询的勘误表.所以很可能我错过了一些东西或者给了我非英语的英语,我无法正确掌握它.

3. A7.17 Page 209 Assignment Expressions

其中一个必须满足以下条件的:<snip>; 或者两个操作数都是指向函数或对象的指针,这些函数或对象的类型相同,除了可能缺少constvolatile在右操作数中.

请用代码说明.类似的int a, b是具有相同类型的两个对象.等等

4. A12.5 Page 232 Conditional Compilation

除了检查条件的嵌套之外,忽略由条件的非活动臂控制的文本.

再次,请用代码解释.

5. B1.4第247页 ungetc(int c, FILE *stream)

每个流只有一个回退字符可以保证.

这是否意味着我ungetc只能在程序/软件中使用一次?


1. B.1.1第242页 File Operations

int rename(const char*oldname,const char*newname)

rename是一个stdio图书馆例程然后为什么它不在man -s3 rename但在man -s2 rename?我正在使用Ubuntu 13.10 BTW.

Ant*_*ala 6

  1. 如果EOF被定义为-1,并且如果在此编译器实现中char 是默认的signed(它是实现定义是否是不合格charsigned or unsigned;并且GCC甚至允许用-funsigned-char/ 更改它用于许多目标-fsigned-char),那么在不进行转换的情况下(unsigned char),读取字节255将与end无法区分-of文件.

  2. 平铺数组的对象意味着您具有:

    struct foo {
        int a, b;
        char c;
    };
    struct foo bar[5];
    
    Run Code Online (Sandbox Code Playgroud)

    那就是一系列结构; 整个数组的sizeof(struct foo)大小乘以数组中的元素数; 必然的,如果有对齐要求,sizeof(struct foo)必须考虑到这些.在这台计算机上int是4个字节和char1; 但结构的大小为12个字节(sizeof(struct foo) == 12);并且数组的大小正好sizeof(struct foo) * 5,不是sizeof(struct foo) * 5 + some.

  3. 正如我所理解的那样,这意味着,该段落中的所有先前的失败,指针可以从另一个指定,如果类型完全相同,或者左手指针限定指向的类型,volatile或者const不存在.右手类型:

    int a = 42;
    const volatile int *b = 0;
    b = &a;
    
    Run Code Online (Sandbox Code Playgroud)

    好的 左手大小有类型const volatile int *,右手大小有int *; 在RH,没有,const并且volatile没有

    const volatile int a = 42;
    int *b = 0;
    b = &a;
    
    Run Code Online (Sandbox Code Playgroud)

不行,因为左手边int *和RH有类型const int *; 这样的任务会丢弃限定词; gcc发出警告:

警告:初始化从指针目标类型中丢弃'const volatile'限定符[默认启用]

  1. 这意味着,如果你有

    #if 0
    
    // this is the inactive arm of #if
    // the code here is ignored *except* for checking
    // for closing #endif; and #if/#ifdef for nesting
    
    #if 0
    #endif // so that the preprocessor knows that this #endif
           // does not yet end the inactive arm of #if 
    
    #else
    // but here is an active arm
    #endif
    
    Run Code Online (Sandbox Code Playgroud)
  2. 一个ungetc必须跟至少1读取; 我们无法保证您可以ungetc一个接一个地成功完成ungetc.

  3. 直接来自Linux系统调用的所有内容都在手册第2节中.rename虽然它也符合C89,C99,但它是一个系统调用.