自 ANSI C99 以来,_Bool
或 bool
通过 stdbool.h
。但是 bool 是否也有 printf
格式说明符?
我的意思是类似于伪代码中的内容:
bool x = true;
printf("%B\n", x);
这将打印:
true
最佳答案
bool
类型没有格式说明符。但是,由于任何小于 int
的整数类型在传递给 printf()
的可变参数时都会被提升为 int
,因此您可以使用 %d
:
bool x = true;
printf("%d\n", x); // prints 1
但为什么不呢:
printf(x ? "true" : "false");
或者,更好的是:
printf("%s", x ? "true" : "false");
或者,甚至更好:
fputs(x ? "true" : "false", stdout);
代替?
关于c - bool 的 printf 格式说明符是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17307275/