Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 238
Kosmo零 Avatar asked Aug 21 '13 09:08

Kosmo零


People also ask

Why is long int and int same size?

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.

Why is long and int same in C++?

"a long in C/C++ is the same length as an int." Not always. The C++ standard specifies that an int be the "natural" size for the processor, which may not always be as big as a long . The standard also guarantees that a long is at least as long as an int , so the fact that they are equal sizes are not always guaranteed.

What is the difference between int and long int?

An int is a 32-bit integer; a long is a 64-bit integer. Which one to use depends on how large the numbers are that you expect to work with. int and long are primitive types, while Integer and Long are objects.

Are all ints the same size?

Modern languages - or at least frameworks - define a size for an int , normally at 32 bits as this allows for a good sized range of -2,147,483,648 to 2,147,483,647 but if it isn;t explicitly defined by the language then it is completely implementation dependant. So "no". It's not the same.


2 Answers

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

like image 91
Mats Petersson Avatar answered Sep 28 '22 01:09

Mats Petersson


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

like image 43
James Kanze Avatar answered Sep 28 '22 00:09

James Kanze