C 数据类型:介于 Short 和 Int 之间

发布于 2024-11-02 20:39:06 字数 972 浏览 6 评论 0原文

我读了一本谈论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[浮城] 2024-11-09 20:39:06
  1. 是的,在这种情况下,%d%hd 是等效的。 printf() 是一个可变参数函数,因此规则规定“整数提升”应用于参数。 printf() 根本看不到 short 值,它只看到一个 int
  2. %ld 代表long int。它的大小可能比普通的 int 更大,所以这本书在这里是错误的。
  3. 转换发生在对 printf() 的调用中。任何传递给 printf()short int 都会被编译器转换为 intshort int 当然不会改变(无论如何也不知道这意味着什么!)
  4. 当您使用 sizeof 打印大小时,您打印的数字就是short int(数字的类型为size_t)。 printf() 甚至看不到 short int,而 sizeof 运算符却能看到,并报告正确的大小。
  5. 创建 short 变量的要点是,如果您想要一个 short 变量,就创建一个。当然,对于大多数变量来说都是如此:-)。但是,如果您认为不需要专门使用 short int,那么只使用 int 就可以了。
  1. Yes, %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 a short value at all, it just sees an int.
  2. %ld is for long int. This could be bigger in size than a plain int, so here the book is wrong.
  3. The conversion occurs in the call to printf(). Any short int passed to printf() is converted to int by the compiler. The short int is not changed of course (not sure what that means anyway!)
  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). printf() doesn't even see the short int, sizeof operator does, and reports the correct size.
  5. The point of creating a short variable is that if you want a short variable, you create one. 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.
无畏 2024-11-09 20:39:06

如果您调用没有原型的函数具有可变参数的函数(例如 printf(3)),那么 C 将应用称为“默认参数提升”的东西。

这些转换将 float 提升为 double,并将任何小于 int 的值提升为 intunsigned 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 to int or unsigned 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.

苦妄 2024-11-09 20:39:06

此转换发生在对 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 to int (or double, if the argument is a float) first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文