我有以下的程序。
当使用
编辑: 大部分时间,我尝试使用
#include <stdio.h>
int main(void)
{
unsigned short int length = 10;
printf("Enter length : ");
scanf("%u", &length);
printf("value is %u \n", length);
return 0;
}
当使用
gcc filename.c
编译时,出现了以下警告(在scanf()
行)。
warning: format ‘%u’ expects argument of type ‘unsigned int *’, but argument 2 has type ‘short unsigned int *’ [-Wformat]
然后我参考了C99 specification - 7.19.6 Formatted input/output functions
,但是无法理解在使用长度修饰符(如short
、long
等)与unsigned
一起使用时的正确格式说明符。
%u
是正确的说明符吗?如果是,为什么会出现上述警告?编辑: 大部分时间,我尝试使用
%uh
,但仍然出现警告。
sizeof(short) <= sizeof(int)
(但下面的实际答案当然更好)。 - Philip%hu
而不是%uh
。 - undefined