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

Kosmos picture Kosmos · Aug 21, 2013 · Viewed 8.8k times · Source

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.

Answer

Mats Petersson picture Mats Petersson · Aug 21, 2013

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