为什么 gcc 支持 std=gnu89 的 bool 类型? 起初,我认为 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
,我们也可以使用 bool 类型吗?
如果有,原因是什么?
最佳答案
在线 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
就是其中的一个特征。
关于c - 为什么 gcc 支持 std=gnu89 的 bool 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52032504/