简体   繁体   English

C数据类型:在Short和Int之间

[英]C datatype : Between Short and Int

I read a book talking about C , it's better for me to present the code first and question in the latter. 我读了一本关于C的书,对我来说,最好先展示代码,然后再问代码。

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. 特别说明 :我使用的是基于Intel的PC,因此int大小为32位。

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. 1.)书中提到这两个代码可以正确运行,尽管其中一个使用%hd说明符,而另一个使用%d说明符。

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. 2)书中的原因是因为C机制会自动将short类型转换为int以进行更快的计算,因此为什么使用%d说明符甚至32位的%ld也会产生正确的结果。

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 ?? 3)我的问题是,什么时候发生这种转换?是在我们将其作为参数传递给printf()函数的时间里吗,就像将float变量作为表达式或表达式传递给double时如何将其转换为double一样。参数,或者在我们初始化变量时使用值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. 4)实际上我已经做了一个小实验,即使用sizeof运算符和printf()函数打印出变量num的sizeof ,它显示了2 bytes但我仍然不确定何时进行转换。

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 ) 5)如果转换是在我们将值分配给short变量的时间内发生的,那么创建short变量的意义何在?(** 如果不是这种情况,应忽略此问题

Your help is much appreciated 非常感谢您的帮助

  1. Yes, %d and %hd are equivalent in this case. 是的,在这种情况下, %d%hd是等效的。 printf() is a variadic function, so the rules say that "integer promotions" are applied to the arguments. printf()是一个可变函数,因此规则说“整数提升”应用于参数。 printf() doesn't see a short value at all, it just sees an int . printf()根本看不到short值,而只看到int
  2. %ld is for long int . %ld用于long int This could be bigger in size than a plain int , so here the book is wrong. 它的大小可能比普通int ,所以这本书是错误的。
  3. The conversion occurs in the call to printf() . 转换发生在对printf()的调用中。 Any short int passed to printf() is converted to int by the compiler. 传递给printf()任何short int由编译器转换为int The short int is not changed of course (not sure what that means anyway!) short int当然是不会改变的(无论如何也不知道这是什么意思!)
  4. When you print the size using sizeof , you are printing a number that is the size of the short int (and the number is of type size_t ). 当您使用sizeof打印大小时,您正在打印的数字是short int的大小(该数字的大小为size_t )。 printf() doesn't even see the short int , sizeof operator does, and reports the correct size. printf()甚至看不到short intsizeof运算符没有,并且报告了正确的大小。
  5. The point of creating a short variable is that if you want a short variable, you create one. 创建short变量的关键是,如果需要short变量,则可以创建一个。 This is true for most variables of course :-). 当然,对于大多数变量来说都是这样。 But if you don't think you need a short int specifically, it's okay to just use int . 但是,如果您认为不需要专门的short int ,则可以只使用int

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 . 如果您调用没有原型的函数带有可变参数的函数(例如printf(3)),那么C会应用称为默认参数提升的东西。

These conversions promote float to double and anything smaller than int to int or unsigned int . 这些转换将float提升为double,并将小于int任何值提升为intunsigned int This tends to harmonize most of the types. 这往往会协调大多数类型。

This is an interesting feature that, possibly, C introduced to the world. 这可能是C引入世界的一个有趣功能。 It actually happens to some extent at the instruction set level or ABI level. 实际上,它在某种程度上发生在指令集级别或ABI级别。 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. C如此出色地匹配硬件并运行得如此之快的另一个原因。

这种转换发生在调用printf ,因为可变参数的功能,所有参数传递进来的一部分...得到扩大至int (或double ,如果该参数是一个float )第一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM