C 中 'boolean' 的 Return 值

Return value of a 'boolean' in C

以下哪种 return 类型是以下相等性检查的正确类型?为什么?

int foo1(short x) {
    return x<0;
}

short foo2(short x) {
    return x<0;
}

char foo3(short x) {
    return x<0;
}

#include <stdbool.h>
bool foo4(short x) {
    return x<0;
}

所有这些都将以相同的方式工作。整数会自动转换为布尔值并返回到 C 中。

但是,如果使用 stdbool.h 中的 bool 类型,代码的 意图 是最清晰的。

在 1999 版标准添加 stdbool.h 之前的代码中,通常在某些 header 中定义了 BOOL 类型。这种模式不应该真正用于新项目,但在旧项目中保持相同的风格是有意义的。