What is the printf format specifier for 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\n", x);

which would print:

true

8 s
8

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\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?

Leave a Comment