为什么 gcc 支持 std=gnu89 的布尔类型?

Why does gcc support a boolean type with std=gnu89?

为什么 gcc 支持 std=gnu89 的布尔类型? 起初,我认为gcc不支持这个。

为了测试这一点,我编写了一个如下所示的 c 文件。

#include <stdio.h>
#include <stdbool.h>
int main(int argc, const char *argv[]) {
    bool x;
    printf("size of bool:%lu\n",sizeof(x));
    return 0;
}

我运行 gcc:

$ gcc -std=gnu89 my_ex.c

但是成功了。

即使 -std=gnu89 我们也可以使用布尔类型吗? 如果有,原因是什么?

在线C语言参考states that

The C programming language, as of C99, supports Boolean arithmetic with the built-in type _Bool (see _Bool). When the header is included, the Boolean type is also accessible as bool.

进一步,从这个 GNU reference

You may also select an extended version of the C language explicitly with -std=gnu89 (for C89 with GNU extensions) or -std=gnu99 (for C99 with GNU extensions). The default, if no C language dialect options are given, is -std=gnu89; this will change to -std=gnu99 in some future release when the C99 support is complete. Some features that are part of the C99 standard are accepted as extensions in C89 mode.

The ISO C standard defines (in clause 4) two classes of conforming implementation. A conforming hosted implementation supports the whole standard including all the library facilities; a conforming freestanding implementation is only required to provide certain library facilities: those in , , , and ; since AMD1, also those in ; and in C99, also those in and .

从上面突出显示的部分可以看出,C99 标准中的一些功能在 C89 模式下被接受为扩展。 bool 就是其中的一个特征