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;
}
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.
intshould be at least 16 bits (range from –32,768 to 32,767)longshould be at least 32 bits (range from –2,147,483,648 to 2,147,483,647)longlongshould be at least 64 bits (range from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)