Search code examples
c++typesintegerlong-integer

Is it not guaranteed that long or long long can store greater numbers than int?


In my assignment, it says

Do not add long int or long long private members to accomplish this as there is no guarantee that either can actually store larger numbers than an int.

I know that int has a maximum of 231-1 and long long has a maximum of 263-1. Can someone give me an example to me why the given sentence is true?


Solution

  • It means exactly what it says. There's no guarantee that a long long can store more numbers than an int. It's at least as big, but it can be the same.

    I know that int has a maximum of 2^31-1 and long long has a maximum of 2^63-1

    This can be true for some platform, with some compiler, but it's not always the same. C++ doesn't guarantee either.

    3.9.1 Fundamental types [basic.fundamental]

    2) There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list. [...] (emphasis mine)