_Bool 有什么优势?
What advantage does _Bool give?
如果 _Bool
类型的行为类似于整数并且不强制值为 true/false 或 1/0,例如:
_Bool bools[] = {0,3,'c',0x17};
printf("%d", bools[2]);
> 1
在那里有什么好处?它只是一种简单的方法来强制事物查看它们如何评估'truth-ness',例如:
printf("%d\n", (_Bool) 3);
> 1
或者这对 C 语言有什么帮助或用处?
优点是易读,仅此而已。例如:
bool rb() {
if (cond && f(y)) {
return true;
}
return false;
}
对战:
int rb() {
if (cond && f(y)) {
return 1;
}
return 0;
}
它真的没有其他好处。对于那些习惯于在没有 bool
的情况下使用 C 代码的人来说,它主要是装饰性的,但对于那些习惯于 C++ 及其 bool
的人来说,它可能会使编码感觉更加一致。
一如既往,“转换为布尔值”的简单方法就是双重否定,例如:
!!3
这会将其减少到 0
或 1
值。
考虑这个:
(bool) 0.5 -> 1
( int) 0.5 -> 0
如您所见,_Bool 不像一个整数。
What advantage does _Bool
give?
_Bool
的值是 0 或 1。与 int
不同,没有别的。
转换为 _Bool
始终将非零值转换为 1,仅将 0 转换为 0。
When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.
示例:
#include <math.h>
#include <stdlib.h>
_Bool all_false[] = { 0, 0.0, -0.0, NULL };
_Bool all_true[] = { 13, 0.1, 42.0, "Hello", NAN };
注意 conversion/casting 与 int
的区别; _Bool
:(int) 0.1
--> 0,但 (_Bool) 0.1
--> 1.
注意 conversion/casting 与 unsigned
的区别; _Bool
:(unsigned) 0x100000000
--> 0,但 (_Bool) 0x100000000
--> 1.
_Bool
使布尔运算更加清晰。
当与 _Generic
. 一起使用时,_Bool
是 int
、char
等的独特类型
在C99之前,C缺少_Bool
。许多早期代码形成了自己的类型 bool, Bool, boolean, bool8, bool_t, ...
。创建一个新类型 _Bool
为这种常见但不统一的做法带来了统一。 <stdbool.h>
可以使用 bool, true, false
。这允许不包含 <stdbool.h>
的旧代码不会中断,但新代码可以使用更清晰的名称。
OP 的示例“不强制值为 true/false 或 1/0”确实强制 bools[2]
的值为 1。它没有强制 'c'
,一个 int
,必须在 [0...1] 范围内,也不能属于 _Bool
类型,就像 int x = 12.345;
是允许的一样。在这两种情况下,都发生了转换。虽然第二个经常产生警告。
如果 _Bool
类型的行为类似于整数并且不强制值为 true/false 或 1/0,例如:
_Bool bools[] = {0,3,'c',0x17};
printf("%d", bools[2]);
> 1
在那里有什么好处?它只是一种简单的方法来强制事物查看它们如何评估'truth-ness',例如:
printf("%d\n", (_Bool) 3);
> 1
或者这对 C 语言有什么帮助或用处?
优点是易读,仅此而已。例如:
bool rb() {
if (cond && f(y)) {
return true;
}
return false;
}
对战:
int rb() {
if (cond && f(y)) {
return 1;
}
return 0;
}
它真的没有其他好处。对于那些习惯于在没有 bool
的情况下使用 C 代码的人来说,它主要是装饰性的,但对于那些习惯于 C++ 及其 bool
的人来说,它可能会使编码感觉更加一致。
一如既往,“转换为布尔值”的简单方法就是双重否定,例如:
!!3
这会将其减少到 0
或 1
值。
考虑这个:
(bool) 0.5 -> 1
( int) 0.5 -> 0
如您所见,_Bool 不像一个整数。
What advantage does
_Bool
give?
_Bool
的值是 0 或 1。与int
不同,没有别的。转换为
_Bool
始终将非零值转换为 1,仅将 0 转换为 0。
When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.
示例:
#include <math.h>
#include <stdlib.h>
_Bool all_false[] = { 0, 0.0, -0.0, NULL };
_Bool all_true[] = { 13, 0.1, 42.0, "Hello", NAN };
注意 conversion/casting 与 int
的区别; _Bool
:(int) 0.1
--> 0,但 (_Bool) 0.1
--> 1.
注意 conversion/casting 与 unsigned
的区别; _Bool
:(unsigned) 0x100000000
--> 0,但 (_Bool) 0x100000000
--> 1.
_Bool
使布尔运算更加清晰。
当与 _Bool
是int
、char
等的独特类型在C99之前,C缺少
_Bool
。许多早期代码形成了自己的类型bool, Bool, boolean, bool8, bool_t, ...
。创建一个新类型_Bool
为这种常见但不统一的做法带来了统一。<stdbool.h>
可以使用bool, true, false
。这允许不包含<stdbool.h>
的旧代码不会中断,但新代码可以使用更清晰的名称。
_Generic
. 一起使用时,OP 的示例“不强制值为 true/false 或 1/0”确实强制 bools[2]
的值为 1。它没有强制 'c'
,一个 int
,必须在 [0...1] 范围内,也不能属于 _Bool
类型,就像 int x = 12.345;
是允许的一样。在这两种情况下,都发生了转换。虽然第二个经常产生警告。