我们知道任何不等于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 = 0C标准是否明确地将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 0However, 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 = 0Does 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
truewhich expands to the integer constant 1,
falsewhich 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.
更多推荐
发布评论