-1

There are times when I use stdbool.h while practicing coding. At this time, if the format modifier of scanf is given as %d, the following error message occurs.

c:\project\hello\hello\hello.c(11): warning C4477: 'scanf' : format string '%d' requires an argument of type 'int *' but variadic argument 3 has type 'bool'

It seems to compile, but it doesn't seem to properly recognize true/false or 0/1 inputs at runtime. I wonder if there's something I'm missing out on.

pm100
  • 48,078
  • 23
  • 82
  • 145
HJS
  • 3
  • 1

1 Answers1

0

You are passing a bool (or _Bool) to scanf. When using %d, you should pass the address of an int.

If your bool is named x, then use:

int temporary;
if (1 != scanf("%d", &temporary))
{
    fprintf(stderr, "Error, scanf did not work as expected.\n");
    exit(EXIT_FAILURE);
}
x = temporary;

(For exit, insert #include <stdlib.h> in your source code.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312