Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #75269

Re: Warning: format '%i' expects type 'int *', but argument 2 has type 'enum month *'

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Warning: format '%i' expects type 'int *', but argument 2 has type 'enum month *'
Date 2015-11-07 11:52 -0500
Organization Self
Message-ID <563E2C6B.2030302@verizon.net> (permalink)
References <bce8216e-9802-4629-a3f3-2e91a2e616c5@googlegroups.com>

Show all headers | View raw


On 11/07/2015 11:24 AM, Alla _ wrote:
> Hello!
> 
> Here is a simple program from the book. When I compile it, the compiler
> generates a warning addressing it to the part with scanf(): 
> 
> warning: format '%i' expects type 'int *', but argument 2 has type 
> 'enum month *'
> 
> // Program to print the number of days in a month
> #include <stdio.h>
> 
> int main (void)
> {
>     enum month {January = 1, February, March, April, May, June, July, August, September, October, November, December};
>     enum month a_month;
>     
>     int days;
>     
>     printf("Enter month number:  ");
>     scanf("%i", &a_month);
>     
>     switch (a_month)
>     {
>         case January:
>         case March:
>         case May:
>         case July:
>         case August:
>         case October:
>         case December:
>                 days = 31;
>                 break;
>         case April:
>         case June:
>         case September:
>         case November:
>                 days = 30;
>                 break;
>         case February:
>                 days = 28;
>                 break;
>         default:
>                 printf("bad month nubmer\n");
>                 days = 0;
>                 break;
>     }
>     
>     if (days != 0)
>         printf("Number of days is %i\n", days);
>     
>     if (a_month == February)
>         printf("... or 29 if it's a leap year\n");
>     
>     return 0;
> }
> 
> Obviously, authors of the book didn't get that warning, or ignored it.
> I understand that I am asking for an integer, the number of a month, hence
> type int %i; but asking scanf to put that value into enum month. I know
> that enumeration values (names in curly braces) are treated by compiler
> as integers. But the variable a_month has the 'enum month' type.

Correct. "Each enumerated type shall be compatible with char, a signed
integer type, or an unsigned integer type. The choice of type is
implementation-defined,128) but shall be capable of representing the
values of all the members of the enumeration." (6.7.2.2p4). Note, in
particular, that "enum month" need not be compatible with 'int'. The
compatible type could easily be "unsigned char" in this case.

> Is there a way to avoid such warning without altering the program's
> simplicity?

Yes. Declare a_month to have the type 'int' rather than "enum month".

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Warning: format '%i' expects type 'int *', but argument 2 has type 'enum month *' Alla _ <modelling.data@gmail.com> - 2015-11-07 08:24 -0800
  Re: Warning: format '%i' expects type 'int *', but argument 2 has type 'enum month *' James Kuyper <jameskuyper@verizon.net> - 2015-11-07 11:52 -0500
    Re: Warning: format '%i' expects type 'int *', but argument 2 has type 'enum month *' Keith Thompson <kst-u@mib.org> - 2015-11-07 13:42 -0800
  Re: Warning: format '%i' expects type 'int *', but argument 2 has type 'enum month *' Barry Schwarz <schwarzb@dqel.com> - 2015-11-07 16:45 -0800

csiph-web