我有以下程序
#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;
}
Run Code Online (Sandbox Code Playgroud)
其中编译时使用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用于int数据类型.
是%u正确的说明符unsigned short int吗?如果是这样,为什么我得到上述警告?!
编辑:大多数时候,我在尝试%uh,它仍在发出警告.
cni*_*tar 150
尝试使用"%h"修饰符:
scanf("%hu", &length);
^
Run Code Online (Sandbox Code Playgroud)
ISO/IEC 9899:201x - 7.21.6.1-7
指定以下d,i,o,u,x,X或n转换说明符适用于具有short或unsigned short类型指针的参数.
R..*_*R.. 46
因为scanf,你需要使用,%hu因为你传递指针unsigned short.因为printf,不可能通过unsigned short默认促销(它将被提升为int或unsigned int取决于是否int至少具有与否一样多的值位unsigned short)这样%d或者%u是好的.%hu不过,如果您愿意,可以自由使用.
从Linux手册页:
h A following integer conversion corresponds to a short int or unsigned short int argument, or a fol?
lowing n conversion corresponds to a pointer to a short int argument.
因此要打印无符号的短整数,格式字符串应为"%hu".
| 归档时间: |
|
| 查看次数: |
260049 次 |
| 最近记录: |