Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #75268 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-11-07 08:24 -0800 |
| Last post | 2015-11-07 16:45 -0800 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.c
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
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-11-07 08:24 -0800 |
| Subject | Warning: format '%i' expects type 'int *', but argument 2 has type 'enum month *' |
| Message-ID | <bce8216e-9802-4629-a3f3-2e91a2e616c5@googlegroups.com> |
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.
Is there a way to avoid such warning without altering the program's
simplicity?
Thank you!
[toc] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-11-07 11:52 -0500 |
| Message-ID | <563E2C6B.2030302@verizon.net> |
| In reply to | #75268 |
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".
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-11-07 13:42 -0800 |
| Message-ID | <ln7flt1o6s.fsf@kst-u.example.com> |
| In reply to | #75269 |
James Kuyper <jameskuyper@verizon.net> writes:
> On 11/07/2015 11:24 AM, Alla _ wrote:
>> 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 *'
[...]
>> 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);
[...]
>> 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".
Or you can read into a temporary int variable, and copy its value to
a_month:
int month_number;
int result = scanf("%d", &month_number);
if (result == 1) {
a_month = month_number;
}
else {
/* error handing */
}
Note that I've changed the format from "%i" (which accepts decimal,
octal, or hexadecimal input) to "%d" (which accepts decimal input). If
the user enters "09", presumably you want to treat it as 9, not as an
error.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-11-07 16:45 -0800 |
| Message-ID | <9u5t3bhlkbk6rkl8pinqslb4i4unfssea3@4ax.com> |
| In reply to | #75268 |
On Sat, 7 Nov 2015 08:24:21 -0800 (PST), Alla _
<modelling.data@gmail.com> 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};
When you specify an enumerator list, the identifiers in the list
(January, etc) are declared as constants with type int. Thus,
everywhere you use January as a value is equivalent to using the
decimal constant 1.
> enum month a_month;
However, when you define a enum object, the compiler is free to
determine which of the allowed types to actually use. In this case,
there is no need to use int; char would work just as well.
If the only thing you did with a_month was assign a value to it and
compare that value with other integers, it would not matter what type
the compiler decided to use since there are implicit conversions
between integer types.
> int days;
>
> printf("Enter month number: ");
> scanf("%i", &a_month);
But here you do something with a_month where the type does matter. You
have told scanf that &a_month is an int*. The diagnostic from the
compiler is telling you that this is not (or at least need not be)
true.
Since all the identifiers in the enumerator list have type int, making
a_month a specific type of integer solves your problem. It is most
common to make it an int but if you want to use a different integer
type you need to change the %i (which really should be %d for reasons
previously discussed) to the appropriate conversion for that type. The
percentage of programs where using short or char would provide any
measurable benefit is extremely small and probably 0 for these types
of training exercises.
--
Remove del for email
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web