关于用C短数据类型格式说明混乱 [英] confusion about short data type format specifier in C
问题描述
考虑下面的程序:
的#include<&stdio.h中GT;
诠释的main()
{
短= 9;
//的printf(%喜\\ N,一);
的printf(%d个,一); // LINE 6
}
根据这的短类型的格式说明符(签字)为 %喜
在短类型的变量总是被自动提升为 INT上执行任何操作之前,?它是不确定的行为,
如果我使用%D格式说明打印在这个程序变量的值?我用gcc的 -Wall -Wextra -Wformat
选项,但仍编译它
编译器没有显示任何单一的警告。为什么呢?
的printf(%喜\\ N,一);
A
提升为 INT
按默认参数推广可变参数函数的规则。
反正你用 ^ h
说明实施允许期望 INT
值在 SHRT_MIN
或 SHRT_MAX
限制。路过的范围以外的值是不确定的行为。
当然,的printf(%I \\ N,一个);
是因为还有效的 INT
推广 A
因此,使用%喜
转换规格也不是很常用的。
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? Is it undefined behavior ,
If I use %d format specifier to print the value of variable in this program? I compiled it using gcc -Wall -Wextra -WFormat
options but still
compiler isn't showing any single warning. Why?
printf("%hi\n", a);
a
is promoted to int
as per the rules of default argument promotion of variadic functions.
Anyway as you use h
specifier the implementation is allowed to expect the int
value is within SHRT_MIN
or SHRT_MAX
limits. Passing a value outside the bounds is undefined behavior.
Of course printf("%i\n", a);
is also valid because of the int
promotion of a
so using %hi
conversion specification is not very usual.
这篇关于关于用C短数据类型格式说明混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!