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
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
You can't, but you can print 0 or 1
source
I prefer an answer from Best way to print the result of a bool as 'false' or 'true' in c?, just like
If you like C++ better than C, you can try this:
ANSI C99/C11 don't include an extra printf conversion specifier for
bool
.But the GNU C library provides an API for adding custom specifiers.
An example:
Since it is a glibc extensions the GCC warns about that custom specifier:
Output:
In the tradition of
itoa()
:There isn't. But since any integral type shorter than
int
is promoted toint
when passed down toprintf()
s variadic arguments, you can use%d
:But why not
or better
or even better
instead?