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

Solutions

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?

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");

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:

#include <stdio.h>
#include <printf.h>
#include <stdbool.h>

static int bool_arginfo(const struct printf_info *info, size_t n,
    int *argtypes, int *size)
{
  if (n) {
    argtypes[0] = PA_INT;
    *size = sizeof(bool);
  }
  return 1;
}
static int bool_printf(FILE *stream, const struct printf_info *info,
    const void *const *args)
{
  bool b =  *(const bool*)(args[0]);
  int r = fputs(b ? "true" : "false", stream);
  return r == EOF ? -1 : (b ? 4 : 5);
}
static int setup_bool_specifier()
{
  int r = register_printf_specifier('B', bool_printf, bool_arginfo);
  return r;
}
int main(int argc, char **argv)
{
  int r = setup_bool_specifier();
  if (r) return 1;
  bool b = argc > 1;
  r = printf("The result is: %B\n", b);
  printf("(written %d characters)\n", r);
  return 0;
}

Since it is a glibc extensions the GCC warns about that custom specifier:

$ gcc -Wall -g    main.c   -o main
main.c: In function ‘main’:
main.c:34:3: warning: unknown conversion type character ‘B’ in format [-Wformat=]
   r = printf("The result is: %B\n", b);
   ^
main.c:34:3: warning: too many arguments for format [-Wformat-extra-args]

Output:

$ ./main
The result is: false
(written 21 characters)
$ ./main 1
The result is: true
(written 20 characters)
Tags: C++ / C / Boolean

Similar questions

Invalid type in argument to printf format specifier and extra argument to printf format specifier
I have been debugging this RTOS code in Coverity and ran into few small errors. The errors are self explanatory as they are format specifier errors. Both of the errors are on the main function. The first error is on the line: Error: Invalid type in argument to printf format specifier (PRINTF_ARGS) invalid_type: Argument result to format specifier %...
How to write a vfprintf wrapper that adds a prefix to format specifier and passes the new format specifier to vfprintf in C89?
I am trying to write a wrapper around vfprintf function but with the requirement that I want to add a prefix to the format specifier and then pass the new format specifier to vfprintf. Now I don't know how to do this, but I have captured my intention in the following code. You can see in the above code, that I want to prefix "foo: error" to the for...
format specifier inside format specifier to change size of leading zeros
I was thinking to solve my issue that is described in the following and then I ended with a question that I couldn't find an answer for it on Google. Question: Is it possible to use format specifier inside another format specifier in printf()? The issue is with size of leading zeros in printf() in following line of code: Is it possible to use a for...
Format specifier in scanf for bool datatype in C
I am using bool datatype in C std99 whose definitions are defined in <stdbool.h>. Now I want the user to give me input. What format specifier I must use in scanf to input the boolean value of 1 byte from the user and then manipulate it afterwards in my program.
Is there any format specifier of bool in C?
In the following example, I'm trying to scan the value of boolean type of variable. When I compile in GCC, I get following warning, code: My question is, Is there any format specifier of bool in C?
My printf format specifier will not compile due to the format of my string, along with the arguments being used
I am trying to write a small program that will convert a person's, or any height from imperial to metric but I am getting the compiler error is telling me: "data argument is not used by format string"

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.