Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

What is the printf format specifier for bool?

Since ANSI C99 there is _Bool or bool through stdbool.h. However, is there likewise 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
by

2 Answers

rahul07
There is no format specifier for bool. You can print it using some of the existing specifiers for printing integral types or do something more fancy:

printf("%s", x?"true":"false");
RoliMishra
ANSI C99/C11 don't include an extra printf conversion specifier for bool. However, since any integral type shorter than int is promoted to int when passed down to printf(), you can use %d:

bool x = true;
printf("%d\n", x); // prints 1

Login / Signup to Answer the Question.