2

I have a question about this fragment of code I wrote in C:

 printf("Do you own a microwave?Enter 1 for Yes and 0 for No\n");
    while((scanf("%d",&microw))==0){
        printf("Please enter a valid number:\n");
    scanf("%d",&microw);}

My compiler says the following: "format %d expects argument of type int* but argument 2 has type _Bool*". As far as I am informed, boolean was considered an int type so I am not sure why this warning is received. How else would I be able to test for this condition other than to set up another variable to test with scanf, and then assigning another variable of _Bool type to true or false? Any feedback is appreciated. I thank you all :).

HeatfanJohn
  • 7,143
  • 2
  • 35
  • 41
russ0
  • 53
  • 1
  • 6
  • Are you using `_Bool` or `_bool`? See http://stackoverflow.com/questions/8724349/difference-between-bool-and-bool-types-in-c for more info on Boolean types in C – HeatfanJohn Mar 16 '13 at 20:45

1 Answers1

1

You're receiving it because, well, it's expecting an int * and you're passing a _Bool *. They're both integers, but different types - on my system _Bool has a size of 1, and int a size of 4.

There's no scanf specifier for what you want, so you'll need to use a char if you really want the minimum storage. You can always assign the value to a _Bool afterwards.

e.g.

scanf("%c",&microw);
_Bool b = microw - '0';

(Also, your code won't work as you intend. If you enter an invalid character with %d, it'll go into an endless loop.)

teppic
  • 8,039
  • 2
  • 24
  • 37
  • I was hoping for a specifier because otherwise, there is more hassle involved in assigning the _Bool values themselves. But no worries. Thank you for your help. I appreciate it :)! – russ0 Mar 16 '13 at 21:02