Why “long int” has same size as “int”? Does this modifier works at all?

為{幸葍}努か 提交于 2019-11-29 18:06:53

问题


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
}

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.


回答1:


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.

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].




回答2:


All that the standard requires is that:

sizeof(char) == 1

and

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

(and that the corresponding unsigned types have the same size as the signed types).

In addition, there are minimum sizes for each type, indirectly specified by limits on the values of INT_MAX, etc.: a char must be at least 8 bits, a short and an int 16, a long 32 and a long long 64.

On 16 bit platforms, it is usual for both short and int to be 16 bits; on 32 bit platforms (and the 36 and 48 bit platforms that still exist), int and long are almost always the same size. On modern 64 bit platforms (with byte addressing), the rational solution would be to make all four types have different sizes (although one could argue that according to the standard, int should be 64 bits, which would mean that int, long and long long all had the same size).




回答3:


C and C++ implementations, a long is larger or equal to an int. Today's most popular desktop platforms, such as Windows and Linux, run primarily on 32 bit processors and most compilers for these platforms use a 32 bit int which has the same size and representation as a long.




回答4:


In 64 bit compiler both int and log int are of 4 bytes.

If you are in windows go thorough the below link: http://msdn.microsoft.com/en-us/library/s3f49ktz%28v=vs.90%29.aspx

You may find some info in one other discussion: What is the bit size of long on 64-bit Windows?




回答5:


Here's the ordering

char
short (int)
int
long (int)
long long (int)

The standard requires that sizeof(char) == 1 is true. The standard also requires that: int is at least 16 bits. long int is at least 32 bits and long long int is at least 64 bits.

With 32 bit processors being maintstream, compilers typically set int to be 4 bytes. The extension hasn't happened again on 64 bit systems (for MSVC anyway) for the sake of compatibility.




回答6:


The point is that you misconceive the meaning of int and long (that's not an adjective).

What the standard defines is that the numer of bits in each type requires this relation to stay in place:

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long).

int, by itself, can be as long as a short up to as long as a long depending on how the implementation is conceived.

Compiler designers tend to to maximize the performance of int arithmetic, making it the natural size for the underlying processor or OS, and setting up the other types accordingly.

But the use of long int, since int can be omitted, it's just the same as long by definition.



来源:https://stackoverflow.com/questions/18353168/why-long-int-has-same-size-as-int-does-this-modifier-works-at-all

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!