-5

So I think I'm a bit confused. I'm searching information about the limits of the differents types of integers. I've seen that the limit for unsigned long int is 4294967295 but when I do:

cout << numeric_limits<unsigned long int>::max() << endl;

I'm getting:

18446744073709551615

And if I'm not wrong this number is the limit of unsigned long long, isn'it? So what is happening? Thank you

xampla
  • 157
  • 1
  • 1
  • 10
  • 1
    "What is happening?" The code you're executing is giving you the result you asked for. You should check the docs. – code_dredd Sep 05 '16 at 20:56
  • 1
    The limits of basic types are implementation dependent. Meaning it's up to your compiler (or rather the people who made the compiler) what the actual limit is (which may depend on various factors including hardware the code is to run on). – UnholySheep Sep 05 '16 at 20:56
  • @UnholySheep ok, thanks. – xampla Sep 05 '16 at 20:59
  • 2
    "I've seen that the limit for unsigned long int is 4294967295" - anyone that proclaimed that equivalence as fact outside the context of a *specific* implementation did a grave disservice to the readers that absorbed their "knowledge". – WhozCraig Sep 05 '16 at 21:09
  • Does this answer your question? [What does the C++ standard state the size of int, long type to be?](https://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be) – phuclv Dec 01 '19 at 11:55

2 Answers2

2

I've seen that the limit for unsigned long int is 4294967295

Whoever told you that was wrong.

The limit for unsigned long int will usually be that on systems for which the type is 32-bit.

But yours is evidently 64-bit, so you have a different limit.

this number is the limit of unsigned long long, isn'it?

Again, you're making assumptions about type width.

The width of types varies across compilers/platforms.

If you want to use types with a fixed size, then those do exist.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

The standard only defines lower bounds for the limits of integers. For example, the lower bound for the maximum that an unsigned long can represent is 4294967295.

std::numeric_limits<unsigned long>::max() gives the implementation-defined maximum value an unsigned long can represent (i.e. what the current implementation aka compiler/linker/etc actually supports).

This means it is required that std::numeric_limits<unsigned long>::max() gives a value that is 4294967295 or more. There is nothing preventing it giving a larger result. However, an implementation that gives a smaller result is non-compliant with the standard.

Note that, when moving between compilers, the only guarantee is "4294967295 or more". If one implementation gives a larger value, there is no guarantee that another implementation will.

For the most part, the standard actually says nothing whatsoever about the number of actual bits used to represent the basic integral types, like unsigned long.

The value 18446744073709551615 is consistent with a 64-bit unsigned long, in practice.

Similar stories, albeit with different values, for other integral types (int, char, short, long, etc).

Peter
  • 35,646
  • 4
  • 32
  • 74