What is the printf format specifier for bool?(bool 的 printf 格式说明符是什么?)
问题描述
从 ANSI C99 开始,通过 stdbool.h 有 _Bool 或 bool.但是是否还有一个 printf 用于 bool 的格式说明符?
Since ANSI C99 there is _Bool or bool via stdbool.h. But is there also a printf format specifier for bool?
我的意思是类似于那个伪代码:
I mean something like in that pseudo code:
bool x = true;
printf("%B
", x);
将打印:
true
推荐答案
bool 类型没有格式说明符.但是,由于任何小于 int 的整数类型在传递给 printf() 的可变参数时都会提升为 int,因此您可以使用 <代码>%d:
There is no format specifier for bool types. However, since any integral type shorter than int is promoted to int when passed down to printf()'s variadic arguments, you can use %d:
bool x = true;
printf("%d
", x); // prints 1
但为什么不呢:
printf(x ? "true" : "false");
或者,更好:
printf("%s", x ? "true" : "false");
或者,甚至更好:
fputs(x ? "true" : "false", stdout);
改为?
这篇关于bool 的 printf 格式说明符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:bool 的 printf 格式说明符是什么?
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
