GCC编译警告:格式‘%i’需要类型为“int *”的参数,但参数 2 的类型为“enum Month *” [-W格式]
当我尝试编译代码时收到以下警告:
program141.c:13:5: warning: format '%i' Expects argument of type 'int *', but argument 2 has type 'enum Month *' [-Wformat ]
// 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 aMonth;
int days;
printf ("Enter month number: ");
scanf ("%i", &aMonth);
switch (aMonth) {
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 number\n");
days = 0;
break;
}
if ( days != 0 )
printf ("Number of days is %i\n", days);
if ( aMonth == february )
printf ("...or 29 if it's a leap year\n");
return 0;
}
这段代码来自我正在读的一本书。
我该如何解决这个警告?
I get the following warning when I try to compile the code:
program141.c:13:5: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘enum month *’ [-Wformat]
// 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 aMonth;
int days;
printf ("Enter month number: ");
scanf ("%i", &aMonth);
switch (aMonth) {
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 number\n");
days = 0;
break;
}
if ( days != 0 )
printf ("Number of days is %i\n", days);
if ( aMonth == february )
printf ("...or 29 if it's a leap year\n");
return 0;
}
This code is from a book I'm reading.
How do I fix this warning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与
scanf
函数一起使用的%i
转换规范与指向有符号整数的指针匹配。在 C 中,枚举类型是与该类型兼容的实现定义的整数类型。在 gcc 中,枚举通常为 unsigned int 类型,如果枚举常量中有负值,则枚举为 int 类型。
请参阅关于枚举的
gcc
实现定义文档(重点是我的):来源 http://gcc.gnu.org/ onlinedocs/gcc/Structures-unions-enumerations-and-bit_002dfields-implementation.html
要修复系统上的警告,只需使用与指向无符号整数类型的指针匹配的
%u
转换规范:The
%i
conversion specification used with thescanf
function matches a pointer to a signed integer. In C, an enumerated type is an implementation defined integer type compatible with that type.In
gcc
, enums are normally of typeunsigned int
and of typeint
if there is a negative value in the enum constants.See
gcc
implementation defined documentation on enums (emphasis mine):Source http://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit_002dfields-implementation.html
To fix the warning on your system, just use the
%u
conversion specification that matches a pointer to an unsigned integer type:尝试使用:
Try with:
输入一个
int
,然后将其分配给enum Month
对象或者您可以尝试强制转换,但它并不安全(enum 和
int
的表示不必相同) s>注意:格式说明符
"%i"
接受十进制、十六进制和八进制表示法的值。Input an
int
, than assign it to theenum month
objectOr you can try a cast, but it is not safe (the representation of the
enum
andint
s need not be the same)Note: the format specifier
"%i"
accepts values in decimal, hexadecimal, and octal notations.特定枚举的底层类型不一定是
int
- 只要所有值都适合,实现就可以自由选择较小的类型(如果启用-fshort-enums
,gcc 将执行此操作)代码>)。对于可移植的解决方案,请将
aMonth
设为int
(只要使用正确的转换说明符,任何其他整数类型都可以)并转换为enum Month 根据需要。
The type underlying a particular enumeration isn't necessarily
int
- implementations are free to choose a smaller type as long as all values fit (gcc will do this if you enable-fshort-enums
).For a portable solution, make
aMonth
anint
(any other integer type will do as long as you use the correct conversion specifier) and cast toenum month
as necessary.