Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do I get an error when I use %d in scanf when I use stdbool.h?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading