Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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.

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?

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top