Why is LONG_MAX Values the same as INT_MAX?

Why is LONG_MAX Values the same as INT_MAX? I'm also getting error when doing something like: long number = 241421; but It can be fixed by doing long long number = 241421;
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/climits/
long and int are not the same thing. sizeof(int) is guaranteed to be <= sizeof(long), but they are not guaranteed to be the same. Deviations between compilers can occur.

You should use std::numeric_limits<type>::max()
On my machine, this is what I get printed out:

numeric_limits<char>::max()  = 127
numeric_limits<short>::max() = 32767
numeric_limits<int>::max() = 2147483647
numeric_limits<long>::max() = 2147483647
numeric_limits<long long>::max() = 9223372036854775807


Using cpp.sh, this is what I get
numeric_limits<char>::max()  = 127
numeric_limits<short>::max() = 32767
numeric_limits<int>::max() = 2147483647
numeric_limits<long>::max() = 9223372036854775807
numeric_limits<long long>::max() = 9223372036854775807

Notice they do not match
Last edited on
So IT really depends on the Compiler you chosed?
Not all your stated sizes are not guaranteed.

char is 8 bits. Short is at least 16 bits, int is at least 16 bits, long is at least 32 bits, long long is at least 64 bits.
But "at least" doesn't mean their sizes cannot be larger.

http://en.cppreference.com/w/cpp/language/types


Besides the minimal bit counts, the C++ Standard guarantees that
1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)


I'm being pedantic, but the distinction is important to explain differences between sizes on different platforms.
Last edited on
char is 1 byte and CHAR_BIT wide. CHAR_BIT is equal to the byte width of the platform. Although now extremely rare, platforms with non-octet bytes do exist.
Last edited on
If you need type with an exact size see this:

http://www.cplusplus.com/reference/cstdint/
So IT really depends on the Compiler you chosed?

On the platform.

Same compiler (e.g. GCC) can exists on many platforms, and won't use the same limits everywhere.

Note that many 64-bit environments support both 32-bit and 64-bit binaries, and the versions have different limits. Same compiler -- with different options -- can create both versions.
Topic archived. No new replies allowed.