2

So I was doing some testing with the new data type long long when I stumbled upon a little "problem" (example came from C++ Primer 6th edition). I was using the climits library to tell me the maximum number supported by long and long long and both came out to 9223372036854775807. How is that possible?

#include <iostream> 
#include <climits>

int main()
{
    std::cout << "int size is " << sizeof(int) << " bytes." << std::endl;
    std::cout << "short size is " << sizeof(short) << " bytes." << std::endl;
    std::cout << "long size is " << sizeof(long) << " bytes." << std::endl;
    std::cout << "long long size is " << sizeof(long long) << " bytes." << std::endl;

    std::cout << "Maximum values: " << std::endl;
    std::cout << "int: " << INT_MAX << std::endl;
    std::cout << "short: " << SHRT_MAX << std::endl;
    std::cout << "long: " << LONG_MAX << std::endl;
    std::cout << "long long: " << LLONG_MAX << std::endl;

    return 0;
}
Minja
  • 786
  • 1
  • 11
  • 20
  • See this question for the *minimum* size required for each datatype: http://stackoverflow.com/questions/589575/size-of-int-long-etc , note that it is the minimum size required i.e. the datatype can be bigger than the specified value. – Naveen Apr 18 '12 at 06:17

3 Answers3

5

It is possible because the standard mandates that long has to be at least as large as int and long long has to be at least as large at long (and at least 64 bits in C++11). So there's no contradiction.

Concerning long long in C++11, see this related question.

Since you tagged the question C++, bear in mind that the numeric limits for a given type are given in the limits header, and that what is standard in C99 (i.e. what is in <climits>) is not necessarily standard in C++, i.e. it might be implementation defined. The number of bits in long long (or, more explicitly, its value range) is an example of a standard in C that only became standard in C++11.

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
5

Because you are running on a 64-bit machine where the compiler implements 'long' and 'long long' as a 64-bit integer.

If you were to compile this on for a 32-bit machine you'd see a difference. For example:

$ g++ -m32 size.cpp
$ ./a.out 
int size is 4 bytes.
short size is 2 bytes.
long size is 4 bytes.
long long size is 8 bytes.
Maximum values: 
int: 2147483647
short: 32767
long: 2147483647
long long: 9223372036854775807
Rich Drummond
  • 3,439
  • 1
  • 15
  • 16
  • Before C++11 the range of `long long` was not standard, so the results you print in your example would have been "implementation defined" and could well have been different. – juanchopanza Apr 18 '12 at 06:50
  • Sure. It was just meant as an example. You really should be aware of how your compiler implements these. I remember using compilers where short and int were both 16-bit. – Rich Drummond Apr 18 '12 at 06:59
  • I agree completely! But if you want to write portable code it is good to know what is standard and what isn't. – juanchopanza Apr 18 '12 at 07:05
1

The C99 standard specifies ranges of values each type must be able to represent (§5.2.4.2.1). The specified values are minimum magnitude, so nothing prevent from having bigger range. I converted this value to the least number of bits needed to represent number in these ranges on a digital computer.

  • int should be at least 16 bits (range from –32,768 to 32,767)
  • long should be at least 32 bits (range from –2,147,483,648 to 2,147,483,647)
  • long long should be at least 64 bits (range from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
Antoine
  • 5,158
  • 1
  • 24
  • 37
  • The C standard only specifies minimum value range (not bit width), but also this is a C++ tagged question. – Michael Foukarakis Apr 18 '12 at 06:26
  • 1
    @MichaelFoukarakis That's true. The C++11 standard refers to the C standard for that, though. Is there a way to represent an integer between -9223372036854775808 to 9223372036854775807 on less than 64 bits using a digital computer? – Antoine Apr 18 '12 at 06:42
  • Ah, I didn't know that; maybe it should be put in the answer. That range is certainly representable in at least 64 bits, but also in more. ;-) – Michael Foukarakis Apr 18 '12 at 06:45
  • @Antoine the C++ standard tries not to make assumptions about low level concepts such as bits. Not even the byte is defined in terms of bits. – juanchopanza Apr 18 '12 at 06:48
  • @juanchopanza: Though there is `CHAR_BITS`. – GManNickG Apr 18 '12 at 08:01
  • @GManNickG but is the value of `CHAR_BITS` defined in the C++ standard? I was under the impression it wasn't, but my impressions are often wrong. – juanchopanza Apr 18 '12 at 08:04
  • @juanchopanza: It must be greater than eight, but otherwise no. I only meant to demonstrate that the standard does care about bits at some level. – GManNickG Apr 18 '12 at 09:03