为什么“long int”与“int”具有相同的大小?这个修饰符是否有效? [英] Why "long int" has same size as "int"? Does this modifier works at all?
问题描述
Ehm ..虽然像长 / 短这样的修饰符扩展/减少了创建变量时分配的内存量,但是... ...
Ehm.. I kind' of though this modifiers like long / short expands / reduces amount of memory allocated when variable are created, but...
#include <stdio.h>
#define test_int int
#define long_int long int
#define long_long_int long long int
void main()
{
printf("%i\n", sizeof (test_int)); //output 4
printf("%i\n", sizeof (long_int)); //output 4. Why? wasn't I modified it's size?
printf("%i\n", sizeof (long_long_int)); //output 8
}
由于未知原因,它会打印的大小int 和 long int 一样。
我使用的是vc ++ 2010 express版。
很抱歉,很难在谷歌找到答案,它总是将长和 int 显示为单独的类型。
For unknown reasons, it prints the size of int and long int as same. I use vc++ 2010 express edition. Sorry, hard to find answer in google, it always shows long and int as separate types.
推荐答案
即使在64位系统上,MS选择使 long
32位的原因是现有的Windows API,历史原因使用 int
和 long
的混合物来表示类似的事情,并且期望这是32位价值(其中一些可以追溯到Windows是16位系统的时候)。因此,为了将旧代码转换为新的64位体系结构,他们选择将 long
保持为32位,以便混合 int <的应用程序/ code>和
long
在各个地方仍然会编译。
The reason that MS choose to makelong
32 bits even on a 64-bit system is that the existing Windows API, for historical reasons use a mixture of int
and long
for similar things, and the expectation is that this is s 32-bit value (some of this goes back to times when Windows was a 16-bit system). So to make the conversion of old code to the new 64-bit architecture, they choose to keep long
at 32 bits, so that applications mixing int
and long
in various places would still compile.
C ++标准中没有任何内容这要求 long
应该大于 int
(当然不是在大多数32位系统上)。所有标准都说大小为短
< = int
< = long
- 并且 short
至少为16位,如果内存服务[不一定表示为应该至少为16位,我认为提到价值范围]。
There is nothing in the C++ standard that dictates that a long
should be bigger than int
(it certainly isn't on most 32-bit systems). All the standard says is that the size of short
<= int
<= long
- and that short
is at least 16 bits, if memory serves [not necessarily expressed as "should be at least 16 bits", I think it mentions the range of values].
这篇关于为什么“long int”与“int”具有相同的大小?这个修饰符是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!