gpt4 book ai didi

C _Generic 错误 - '_Bool' 之前的预期表达式

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

我正在学习 C _Generic。这是问题:为什么我无法成功编译下面的代码?它只会将错误作为标题发出。

#include <stdio.h>
#define EVALUATE(X) _Generic((X), _Bool : "boolean", default : "not boolean")

int main(void)
{
printf("%s\n", EVALUATE(5));

return 0;
}

最佳答案

_Generic 在 gcc 中不受支持 until 4.9.0 . _Bool 在您当前的编译器中受支持,但由于编译器认为 _Generic 是常规隐式函数,它会发出有关奇数参数的警告。

海湾合作委员会 4.7.1

[9:25am][wlynch@apple /tmp] /opt/gcc/4.7.1/bin/gcc -std=c11 foo.c
foo.c: In function ‘main’:
foo.c:7:5: warning: implicit declaration of function ‘_Generic’ [-Wimplicit-function-declaration]
foo.c:7:20: error: expected expression before ‘_Bool’

海湾合作委员会 4.8.2

[9:26am][wlynch@apple /tmp] /opt/gcc/4.8.2/bin/gcc -std=c11 foo.c
foo.c: In function ‘main’:
foo.c:7:5: warning: implicit declaration of function ‘_Generic’ [-Wimplicit-function-declaration]
printf("%s\n", EVALUATE(5));
^
foo.c:3:35: error: expected expression before ‘_Bool’
#define EVALUATE(X) _Generic((X), _Bool : "boolean", default : "not boolean")
^
foo.c:7:20: note: in expansion of macro ‘EVALUATE’
printf("%s\n", EVALUATE(5));
^

海湾合作委员会 4.9.0

[9:26am][wlynch@apple /tmp] /opt/gcc/4.9.0/bin/gcc -std=c11 foo.c
[9:27am][wlynch@apple /tmp] ./a.out
not boolean

关于C _Generic 错误 - '_Bool' 之前的预期表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29279166/

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