C标准是否将真值显式表示为0或1?(Does the C standard explicitly indicate truth value as 0 or 1?)

编程入门 行业动态 更新时间:2024-10-14 10:42:49
C标准是否将真值显式表示为0或1?(Does the C standard explicitly indicate truth value as 0 or 1?)

我们知道任何不等于0数字都被认为是C,所以我们可以写:

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

但是,我想知道在C中是否将/ F值定义为1/0,所以我尝试了以下代码:

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

C标准是否明确地将true和false的真值分别表示为1和0 ?

We know that any numbers that are not equal to 0 are viewed as true in C, so we can write:

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

However, I was wondering whether true / false are defined as 1/0 in C, so I tried the code below:

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

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

最满意答案

C标准是否明确地将真值和true值分别表示为0和1 ?

C标准将stdbool.h中的宏定义为true和false ,分别扩展为1和0 。

C11-§7.18:

其余三个宏适用于#if预处理指令。 他们是

true

其扩展到整数常数1 ,

false

它扩展到整数常数0 [...]

关于运算符==和!=

C11-§6.5.9/ 3:

== (等于)和!= (不等于)运算符类似于关系运算符,除了它们的较低优先级。 108)如果指定的关系为真,则每个运算符产生1如果为false则返回0 。 结果具有int类型。 对于任何一对操作数,其中一个关系是正确的。

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

The C standard defines true and false as macros in stdbool.h which expand to 1 and 0 respectively.

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 [...]

Regarding the operators == and !=

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.

更多推荐

本文发布于:2023-08-06 21:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455783.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:真值   标准   standard   explicitly   truth

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!