gpt4 book ai didi

c - C 标准是否将真值明确表示为 0 或 1?

转载 作者:太空狗 更新时间:2023-10-29 16:15:35 25 4
gpt4 key购买 nike

我们知道任何不等于0的数字在C中都被视为true,所以我们可以这样写:

int a = 16;

while (a--)
printf("%d\n", a); // prints numbers from 15 to 0

但是,我想知道 true/false 在 C 中是否被定义为 1/0,所以我尝试了下面的代码:

printf("True = %d, False = %d\n", (0 == 0), (0 != 0));  // prints: True = 1, False = 0

C 标准是否明确指出 true 和 false 的真值分别为 10

最佳答案

Does the C standard explicitly indicate the truth values of true and false as 0 and 1 respectively?

C 标准将 truefalse 定义为 stdbool.h 中的宏,扩展为 10 分别。

C11-§7.18:

The remaining three macros are suitable for use in #if preprocessing directives. They are

true

which expands to the integer constant 1,

false

which expands to the integer constant 0 [...]

对于运算符 ==!= ,标准说

C11-§6.5.9/3:

The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence.108) Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. For any pair of operands, exactly one of the relations is true.

关于c - C 标准是否将真值明确表示为 0 或 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37291681/

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