C 数据类型:介于 Short 和 Int 之间
我读了一本谈论 C 的书,对我来说最好先展示代码,然后再提出问题。
第一个代码
#include <stdio.h>
int main(void)
{
short num = 3;
printf("%hd\n" , num );
return 0;
}
第二个代码
#include <stdio.h>
int main(void)
{
short num = 3;
printf("%d\n" , num );
return 0;
}
特别说明:我使用的是基于 intel 的电脑,因此 int 大小是 32 位。
问题:
1.) 书中提到这两个代码可以正确运行,尽管其中一个使用 %hd
说明符,而另一个使用 %d
说明符。
2.)书上的原因是因为C机制会自动将类型short转换为int以加快计算速度,这就是为什么使用%d
说明符甚至 32 位的 %ld
也会产生正确的结果。
3.)我的问题是,这个转换是什么时候发生的?是在我们将它作为参数传递给 printf() 函数期间,就像 float 变量在它转换为 double 时如何转换一样作为表达式或参数传递,或者在我们用值 3
初始化变量时传递?
4.)实际上我做了一个小实验,即使用 sizeof
运算符和 printf()
函数打印出变量 num 的大小,它显示了 2 个字节
。但我仍然不确定转换何时发生。
5.)如果转换发生在我们将值赋给短变量期间,那么创建短变量有什么意义?(**如果不是这种情况,则应忽略此问题)
非常感谢您的帮助
I read a book talking about C , it's better for me to present the code first and question in the latter.
First Code
#include <stdio.h>
int main(void)
{
short num = 3;
printf("%hd\n" , num );
return 0;
}
Second Code
#include <stdio.h>
int main(void)
{
short num = 3;
printf("%d\n" , num );
return 0;
}
Special note: I'm using intel based pc so int size is 32-bit.
Question :
1.) The book mention this two code could run correctly although one of it uses the %hd
specifier while the other uses %d
specifier.
2.)The reason from the book is that because C mechanism would automatically convert the type short to int for faster computation,that is why by using the %d
specifier or even %ld
which is 32-bit would yield the correct result too.
3.)My question is , when does this conversion occurred??Is it during the time we passed it as an argument to the printf()
function , just like how float variable is converted to double when it is passed as an expression or an argument, or by the time we initialize the variable with a value 3
??
4.)Actually I've done a small experiment , that is by printing out the size of the variable num using the sizeof
operator along with printf()
function , and it shows me 2 bytes
.But i still not sure when the conversion happen.
5.)If the conversion occurred during the time we assigned the value to the short variable,what's the point of creating a short variable??(**This question should be ignore if it's not the case)
Your help is much appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
%d
和%hd
是等效的。 printf() 是一个可变参数函数,因此规则规定“整数提升”应用于参数。printf()
根本看不到short
值,它只看到一个int
。%ld
代表long int
。它的大小可能比普通的int
更大,所以这本书在这里是错误的。printf()
的调用中。任何传递给printf()
的short int
都会被编译器转换为int
。short int
当然不会改变(无论如何也不知道这意味着什么!)sizeof
打印大小时,您打印的数字就是short int
(数字的类型为size_t
)。printf()
甚至看不到short int
,而sizeof
运算符却能看到,并报告正确的大小。short
变量的要点是,如果您想要一个short
变量,就创建一个。当然,对于大多数变量来说都是如此:-)。但是,如果您认为不需要专门使用short int
,那么只使用int
就可以了。%d
and%hd
are equivalent in this case.printf()
is a variadic function, so the rules say that "integer promotions" are applied to the arguments.printf()
doesn't see ashort
value at all, it just sees anint
.%ld
is forlong int
. This could be bigger in size than a plainint
, so here the book is wrong.printf()
. Anyshort int
passed toprintf()
is converted toint
by the compiler. Theshort int
is not changed of course (not sure what that means anyway!)sizeof
, you are printing a number that is the size of theshort int
(and the number is of typesize_t
).printf()
doesn't even see theshort int
,sizeof
operator does, and reports the correct size.short
variable is that if you want ashort
variable, you create one. This is true for most variables of course :-). But if you don't think you need ashort int
specifically, it's okay to just useint
.如果您调用没有原型的函数或具有可变参数的函数(例如 printf(3)),那么 C 将应用称为“默认参数提升”的东西。
这些转换将 float 提升为 double,并将任何小于
int
的值提升为int
或unsigned int
。这往往会协调大多数类型。这是一个有趣的功能,可能是 C 向世界介绍的。它实际上在某种程度上发生在指令集级别或 ABI 级别。参数在寄存器或堆栈上传递,通常没有人允许堆栈错位或在高阶位中留下垃圾。
这只是 C 与硬件如此匹配且运行如此之快的又一个原因。
If you call a function without a prototype or a function with variable arguments, like printf(3), then C applies something called the default argument promotions.
These conversions promote float to double and anything smaller than
int
toint
orunsigned int
. This tends to harmonize most of the types.This is an interesting feature that, possibly, C introduced to the world. It actually happens to some extent at the instruction set level or ABI level. Parameters are passed in registers or on the stack, and typically no one allows misaligning the stack or leaving junk in higher-order bits.
Just one more reason why C matches the hardware so well and runs so fast.
此转换发生在对
printf
的调用中,因为对于可变参数函数,作为...
一部分传入的所有参数都会扩展为int
> (或double
,如果参数是float
)。This conversion happens in the call to
printf
, because for variadic functions, all the arguments passed in as part of the...
get widened toint
(ordouble
, if the argument is afloat
) first.