Can long long not store an integer?

My assignment says: "Do not add long or long private members to accomplish this, since there is no guarantee that you can actually store larger numbers than int." I know that int has a maximum of 2 ^ 31-1 and a long long maximum of 2 ^ 63-1. So can someone give me an example for me, why is this sentence true?

Thanks in advance!

+4
source share
2 answers

That means exactly what he says. There is no guarantee that long long can store more numbers than int . It is 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 may be true for some platform with some compiler, but it is not always the same. C ++ also does not guarantee.

3.9.1 Basic types [basic.fundamental]

2) There are five standard standard 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 its predecessors . list. [...] (emphasis mine)

+7
source

Standard C specifies two relevant criteria:

  • sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long) ≤ sizeof(uintmax_t)

    This is indirectly indicated in ISU / IEC 9899: 2011, §6.2.5 Types, ¶8: for any two integer types with the same degree of correspondence and another integer conversion (see 6.3.1.1), the range of values ​​of the type with a lower integer conversion rank the subrange of values ​​of another type is equal.

  • Minimum allowed value for maximum types (ISO / IEC 9899: 2011, §5.2.4.2.1 Dimensions of integer types <limits.h> ):

    • SCHAR_MAX ≥ 127 // 2 7 -1
    • SHRT_MAX ≥ 32.767 // 2 15 -1
    • INT_MAX ≥ 32,767 // 2 15 -1
    • LONG_MAX ≥ 2,147,483,647 // 2 31 -1
    • LLONG_MAX ≥ 9,223,372,036,854,775,807 // 2 63 -1

The quote is formally correct; you can develop systems in which long does not store a larger range than int - indeed, this applies to most 32-bit systems (all that I know about), and also applies to Windows 64. This is less likely to be exact wrt long long ; I don’t know the system where sizeof(int) == sizeof(long long) (and due to the given inequality sizeof(int) == sizeof(long) ). On most 64-bit Unix systems, sizeof(int) == 4 , sizeof(long) == 8 and sizeof(long long) == 8 ; on Windows 64, sizeof(long) == 4 and only long long (or __int64 ) is a 64-bit type.

+2
source

Source: https://habr.com/ru/post/1446852/


All Articles