c - bool 的 printf 格式说明符是什么?

标签 c boolean printf

自 ANSI C99 以来,_Boolbool 通过 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/

相关文章:

C字符串函数错误

c - 使用 memcpy 从缓冲区复制到结构数组时出现 SEGFAULT

c - 如何将一些数据打印到文件中,并在Excel打开时将它们分成不同的列

c++ - 无法从 LMDB 获取值

c - 提取c中字符串的出现次数

SQL Server : how to return 1 row Boolean/string from in SELECT from multiple rows?

java - 尝试用随机值填充 boolean 矩阵

c - 在 C 中使用#include <stdbool.h>

C - 在函数末尾添加打印到文件选项

c - 为什么 printf 会导致段错误?