[英]confusion about short data type format specifier in C
Consider following program: 考虑以下计划:
#include <stdio.h>
int main()
{
short a=9;
//printf("%hi\n",a);
printf("%d",a); // LINE 6
}
According to this the format specifier for short type (signed) is %hi
Is the short type variable always gets promoted automatically to int before performing any operation on it? 根据这个 ,短类型(签名)的格式说明符是
%hi
短行型变量是否总是在对它执行任何操作之前自动提升为int ? Is it undefined behavior , If I use %d format specifier to print the value of variable in this program? 它是未定义的行为,如果我使用%d格式说明符来打印此程序中的变量值? I compiled it using gcc
-Wall -Wextra -WFormat
options but still compiler isn't showing any single warning. 我使用gcc
-Wall -Wextra -WFormat
选项编译它,但仍然编译器没有显示任何单个警告。 Why? 为什么?
printf("%hi\n", a);
a
is promoted to int
as per the rules of default argument promotion of variadic functions. 根据可变参数函数的默认参数提升规则将
a
提升为int
。
Anyway as you use h
specifier the implementation is allowed to expect the int
value is within SHRT_MIN
or SHRT_MAX
limits. 无论如何,当您使用
h
说明符时,允许实现期望int
值在SHRT_MIN
或SHRT_MAX
限制内。 Passing a value outside the bounds is undefined behavior. 将值传递到边界之外是未定义的行为。
Of course printf("%i\\n", a);
当然是
printf("%i\\n", a);
is also valid because of the int
promotion of a
so using %hi
conversion specification is not very usual. 是因为的也是有效的
int
推广的a
所以用%hi
转换规范还不是很平常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.