gpt4 book ai didi

c - _Bool 类型是在哪个版本的 C 中引入的?

转载 作者:太空宇宙 更新时间:2023-11-03 23:26:23 24 4
gpt4 key购买 nike

this 的答案关于 SO 的问题指定 _Bool 是在 C99 中引入的(特别是BobbyShaftoe 的回答)。

但是下面的代码可以与 gcc -std=c90 -pedantic test.cgcc -std=c89 -pedantic test.c 完美编译,产生输出 1(我的gcc版本是4.7.1)

#include<stdio.h>
#include<stdlib.h>

int main(){

_Bool a = 0;
printf("%u\n",sizeof(a) );
return 0;
}

那么 _Bool 是在哪个版本中引入的?

最佳答案

正如@0xC0000022L 所指出的,您需要区分标准实现和实际实现。有趣的是,GCC(还有 Clang)处理 _Bool 的方式有些不一致。 C99 还引入了 _Complex 类型,其中有诊断消息:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
printf("%lu\n", (unsigned long) (sizeof(_Complex)));
return 0;
}

结果进入(GCC 4.4.7):

$ gcc -ansi -pedantic-errors check.c 
check.c: In function ‘main’:
check.c:5: error: ISO C90 does not support complex types
check.c:5: error: ISO C does not support plain ‘complex’ meaning ‘double complex’

你可以检查源代码,然后你会发现确实 _Complex 类型是根据标准版本检查的,而 _Bool 不是:

gcc/po/gcc.pot:19940

#: c-decl.c:7366
#, gcc-internal-format
msgid "ISO C90 does not support complex types"
msgstr ""

gcc/c-decl.c:7380

case RID_COMPLEX:
dupe = specs->complex_p;
if (!flag_isoc99 && !in_system_header)
pedwarn (input_location, OPT_pedantic, "ISO C90 does not support complex types");

我不会将其称为错误,而是开发人员做出的决定。

关于c - _Bool 类型是在哪个版本的 C 中引入的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26358359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com