本文介绍了什么是printf格式说明符布尔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于ANSI C99有 _Bool
或布尔
通过 stdbool.h
。但有也布尔一个的printf
格式说明?
我的意思是这样在伪code:
布尔X =真实的;
的printf(%B \\ N,X);
这将打印:
真
解决方案
有没有。但是,由于任何整型比 INT
被提升到更短的 INT
时传下来的的printf( )
取值可变参数,你可以使用%d个
:
布尔X =真实的;
的printf(%d个\\ N,X); //输出1
但是,为什么不
的printf(X真:假);
或更好
的printf(%S,X真:假);
甚至更好
的fputs(X真:假,标准输出);
呢?
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\n", x);
which would print:
true
解决方案
There isn't. But 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\n", x); // prints 1
But why not
printf(x ? "true" : "false");
or better
printf("%s", x ? "true" : "false");
or even better
fputs(x ? "true" : "false", stdout);
instead?
这篇关于什么是printf格式说明符布尔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!