20

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.

9
  • 2
    Why shouldn't the sizes be the same?! The types are different, that's all that matters. Each type can represent a set of values that's prescribed by the standard, but it's free to be able to represent more than that. Commented Aug 21, 2013 at 9:04
  • 1
    Whats long modifier does then? Aren't it supposed to expand amount of allocated memory? Commented Aug 21, 2013 at 9:05
  • 1
    As far as i know standard only says that long is at least as long as integer. It can't be shorter, but doesn't have to be more than int. Everything else is machine dependent. Commented Aug 21, 2013 at 9:07
  • 5
    The mandatory ranges of representable values are provided in this answer. Noteworthy: short is at least 16 bits, long int at least 32, and long long int at least 64 bits. Everything else is unspecified. For example, a platform could very well have every type be 256 bits long, and thus sizeof every type would be 1. Commented Aug 21, 2013 at 9:08
  • 2
    In fact, the entire site if very bad. I found one or two serious errors on every page I opened. Commented Aug 21, 2013 at 9:37

6 Answers 6

29

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

Sign up to request clarification or add additional context in comments.

5 Comments

Indeed, it mentions that short should at least hold from -32767 to +32767. Note that the standard doesn't mandate -32768 (lowest value for a 16-bit number in 2's complement) because specify how negative numbers should be handled.
Thanks alot Mats, but It seems long is used as an another alias for int for the past 30 years! if it is not going to be greater than that, why bother mentioning it in the standard at all! It just makes no sense to have some thing called long, and not in a single time in 30 years, in major compilers and system, not providing more space than int! This is ridiculous, All new languages have meaningful semantics, C++ is a exception in just every thing it seems!
@Hossein: 30 years ago, many compilers had int as a 16-bit value, and long was 32. These days, on non-Windows platforms that have 64-bit processors, long is indeed 64 bits, where int is 32 bits. But the main point is that there is no GUARANTEE that the type is any particular size for long, other than a minimum of 32 bits. It is then up to the compiler if it's larger than that or not.
@Hossein C and C++ are both used on many different systems, there are many non-windows systems, where a long is 64 bit. There are systems where an int is 16 bit, and a long is 32 bit.
Wow, huh, That hell is actual for windows only. At least, on macOS long is as expected 8 bytes.
13

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

Comments

2

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.

3 Comments

That is incorret, long is "as large or larger than int". No requirement to be bigger.
@MatsPetersson sizeof (int) <= sizeof(long). for 32 bit it is equal but on 64 bit it is different.
On 64-bit it MAY be different. It isn't on a Windows machine, for example.
0

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?

1 Comment

In most 64 bit compilers, int is 4 bytes, and long 8.
0

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.

5 Comments

urgh maybe that's why I though long int should me more than int. when I was learning c++, I started from borland c++ 3.1
C also requires that int be at least 16 bits and long be at least 32 bits.
I'm not sure you could say that int was ever "typically" 16 bits. Back when 16 bit int was common, machines varied a lot more, and there were probably just as many which had 32 bit int (and at least a few which had 36 bit int, or other variants which would be considered exotic today).
And except for MSVC, 64 bit systems have an 8 byte long.
"except for MSVC" is a misnomer. For starters, IIRC the same applies to MinGW; then, I doubt Win64 is the only LLP64 (long long and pointers 64-bit) platform.
0

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.

1 Comment

sizeof (long) == sizeof (long long) is possible, and common among 64-bit platforms besides Windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.