&引用;无效值不应被忽略,因为它应该是;当使用gcc时

&引用;无效值不应被忽略,因为它应该是;当使用gcc时,c,gcc,C,Gcc,这是我在gcc c99中的代码,我的代码中有“void value”吗 int main(int argc, char **argv) { char *s = "smth"; int r2 = ({ if (__builtin_types_compatible_p(__typeof__(s), char*)) { true; } else { false; }}); return (0); }; 更新 更糟糕的是,下面的代码也有同样的错误 int main(int ar

这是我在gcc c99中的代码,我的代码中有“void value”吗

int main(int argc, char **argv) {
    char *s = "smth";
    int r2 = ({ if (__builtin_types_compatible_p(__typeof__(s), char*)) { true; } else { false; }});
    return (0);
};
更新

更糟糕的是,下面的代码也有同样的错误

int main(int argc, char **argv) {
    char *s = "smth";
    int r2 = ({if (1) {1;} else {0;}});
    return (0);
};

您试图将
if
语句分配给
int
。该语句没有类型,因此您会看到错误

您想要的是:

如果
条件
的计算结果为true,则表达式的值为
true\u值
,否则它的值为
false\u值

所以你想要的是:

int r2 = (__builtin_types_compatible_p(__typeof__(s), char*)) ? true : false;
或者,由于这两个值只是
true
false

int r2 = __builtin_types_compatible_p(__typeof__(s), char*);
声明

if (condition) {statement}  
将返回
void
,您不能用于初始化/分配变量。最好使用三元运算符
?:

int r2 = __builtin_types_compatible_p(__typeof__(s), char*) ? true : false  
或者更好

bool r2 = __builtin_types_compatible_p(__typeof__(s), char*)  // Use stdbool.h
因为如果给定的类型相同,则返回
1
,否则返回
0

1),这是由于多种原因造成的。2) 您认为选择声明将返回什么?-无论如何,这是不符合标准的3)为什么不坚持标准并使用条件表达式-或者4)您根本不需要条件构造。1)当已经使用布尔常量初始化时,为什么不使用
bool
?2) 为什么是有条件的呢?只需
boolr2=\uuuuuu内置类型\uu兼容\up(\uuuuuu typeof\uuus,char*)
就足够了,而且更清晰(它应该有一个自解释的名称)。
bool r2 = __builtin_types_compatible_p(__typeof__(s), char*)  // Use stdbool.h