12

In the following example, I'm trying to scan the value of a Boolean type of variable. When I compile in GCC, I get the following warning,

warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘_Bool *’ [-Wformat=]
  scanf("%d", &b);

Code:

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

int main()
{
    bool b;

    scanf("%d", &b);
    printf("%d\n", b);
}

Is there a format specifier of bool in C?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 8
    There isn't one. As an alternative, you might want `printf("%s\n", b ? "true" : "false");`. – Grzegorz Szpetkowski Sep 23 '16 at 08:28
  • This isn't 100% dupe, since this is about printing and http://stackoverflow.com/questions/12920694/format-specifier-in-scanf-for-bool-datatype-in-c is about `scanf()`. But still, good enough I guess. :) – unwind Sep 23 '16 at 08:30
  • @unwind: You're correct. I'll reopen, with apologies. Now it's time to dig the C standard out. – Bathsheba Sep 23 '16 at 08:30
  • 1
    I'm not even sure it makes sense for an application to input/output a raw bool, apart from debugging purposes. The bool always stands for something and the user is only interested in whatever that something is. The prompt for user input would be something like "Continue (y/n)?", in which case the input would be characters or whatever. You wouldn't prompt the user "It is assumed you want to continue" after which they would type "true"/"false". Even if you did, it would still not be a bool type used. – Lundin Sep 23 '16 at 08:45
  • @Lundin: I completely agree. I also go one step further and think that the introduction of bool-stuff into C was a mistake. – Bathsheba Sep 23 '16 at 08:48
  • @Bathsheba The main mistake imo was that they didn't drop backwards compatibility and introduced a true boolean type which isn't an `int` in disguise. And then re-make all logical etc operators so that they return a bool type instead of an int (like C++). And then re-make all conditional statements so that they must have a bool type as input. Would have been a monumental improvement of the language in terms of type safety. Billions upon billions of bugs would disappear from C programs by language design. But creating a safe, rational, bug-free language was never a priority of the C committee. – Lundin Sep 23 '16 at 08:59
  • man microprocessors/microcontrollers actually have addressable boolean registers and arrays of boolean bits. so it is not a mistake to have a 'bool' type (found in stdbool.h) In general, it is better to not use 'bool' except where it makes sense for the hardware being used. – user3629249 Sep 24 '16 at 21:12
  • Related: *[What is the printf format specifier for bool?](https://stackoverflow.com/questions/17307275/what-is-the-printf-format-specifier-for-bool)*. See also [the linked questions](https://stackoverflow.com/questions/linked/17307275?sort=votes). – Peter Mortensen Aug 07 '23 at 20:22

2 Answers2

6

There is no format specifier for the bool type in C.

For printf, you can rely on the implicit promotion to int, and use %d as the specified formatter.

For scanf, you ought to read it into an int, and convert appropriately. Again, using %d.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
-6

There isn't any specifier in C for bool. You should typecast it in printf() to avoid warnings if you want.

But there isn't any dedicated format specifier for representing the bool type.

Try the below to avoid the scanf() warning:

scanf("%d", (int*)&b)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stubborn
  • 780
  • 1
  • 5
  • 20
  • Does `bool` necessarily have the same size as `int`? Otherwise `(int*)&b` looks unsafe to me. – Martin R Sep 23 '16 at 08:50
  • No its not necessary, generally bool(not everytime) size is 1 byte , this is just for warning only. – Stubborn Sep 23 '16 at 08:55
  • 1
    But then `scanf` will store an int-sized value at that location, so the behaviour is undefined. – Martin R Sep 23 '16 at 08:57
  • Then the conversion method will always be there. – Stubborn Sep 23 '16 at 09:05
  • 3
    Downvoted for advice to use `scanf("%d",(int*)&b)`, which is clear *undefined behaviour*. – user694733 Sep 23 '16 at 09:54
  • 1
    It may *seem* to work on gcc. That's the nasty thing about [undefined behaviour](http://stackoverflow.com/q/2397984/694733). It may not work on next run or after next compilation. Or it may have unrelated side-effects, like corrupting memory somewhere else in the program. Or something else. C standard is very clear that this is illegal usage, but unfortunately the casting disables any possible warnings that compiler could give you. Testing alone is never enough tell if something is legal or not in C. – user694733 Sep 26 '16 at 07:48