I'm studying C++ using The C++ Programming Language from Bjarne Stroustrup, and while doing exercise 4.5 (What, on your system, are the largest and the smallest values of the following types: char, short, int, long , float, double, long double, and unsigned) I've wrote:
#include <iostream>
#include <limits>
using namespace std;
int main(){
// (...)
// Exercise 4.11.4:
cout << "\nExercise 4.11.4:\n";
for (int i = 48; i < 127; i++)
{
cout
<< char(i) << " Hex:" << hex << i << " Dec:" << dec << i << " Oct:" << oct << i << "" << endl;
}
cout << "\nExercise 4.11.5:\n";
cout << "Largest and smallest values for different data types:\n";
// char ???!??!?!?!?!?
cout << "char: " << static_cast<int>(numeric_limits<char>::min()) << " to "
<< static_cast<int>(numeric_limits<char>::max()) << endl;
// unsigned char
cout << "unsigned char: " << static_cast<int>(numeric_limits<unsigned char>::min()) << " to "
<< static_cast<int>(numeric_limits<unsigned char>::max()) << endl;
// short
cout << "short: " << numeric_limits<short>::min() << " to "
<< numeric_limits<short>::max() << endl;
// int
cout << "int: " << numeric_limits<int>::min() << " to "
<< numeric_limits<int>::max() << endl;
// float
cout << "float: " << numeric_limits<float>::lowest() << " to "
<< numeric_limits<float>::max() << endl;
// double
cout << "double: " << numeric_limits<double>::lowest() << " to "
<< numeric_limits<double>::max() << endl;
// long double
cout << "long double: " << numeric_limits<long double>::lowest() << " to "
<< numeric_limits<long double>::max() << endl;
// unsigned
cout << "unsigned: " << 0 << " to "
<< numeric_limits<unsigned>::max() << endl;
}
And the output is REALLY weird:
(...)
} Hex:7d Dec:125 Oct:175
~ Hex:7e Dec:126 Oct:176
Exercise 4.11.5:
Largest and smallest values for different data types:
char: 37777777600 to 177
unsigned char: 0 to 377
short: 100000 to 77777
int: 20000000000 to 17777777777
float: -3.40282e+38 to 3.40282e+38
double: -1.79769e+308 to 1.79769e+308
long double: -1.79769e+308 to 1.79769e+308
unsigned: 0 to 37777777777
Does anybody has a clue why the values are wrong? I was expecting char to be -128 to 127.
EDIT NOTE: Added part of missing code that was causing the issue, so now it is possible to replicate the problem.
As suggested by the wizards (@Iłya Bursov,and @n. m. could be an AI) the problem was that I was displaying the answer in oct (due to another exercise in the same file, not presented in the question, but they nailed it). I had to tell the compiler to display back to dec and no longer oct.
int main(){
// (...)
// Exercise 4.11.4:
cout << "\nExercise 4.11.4:\n";
for (int i = 48; i < 127; i++)
{
cout
<< char(i) << " Hex:" << hex << i << " Dec:" << dec << i << " Oct:" << oct << i << "" << endl;
}
cout << dec << endl; // FIXING TO DEC.
cout << "\nExercise 4.11.5:\n";
cout << "Largest and smallest values for different data types:\n";
// char ???!??!?!?!?!?
cout << "char: " << static_cast<int>(numeric_limits<char>::min()) << " to "
<< static_cast<int>(numeric_limits<char>::max()) << endl;
// unsigned char
cout << "unsigned char: " << static_cast<int>(numeric_limits<unsigned char>::min()) << " to "
<< static_cast<int>(numeric_limits<unsigned char>::max()) << endl;
// short
cout << "short: " << numeric_limits<short>::min() << " to "
<< numeric_limits<short>::max() << endl;
// int
cout << "int: " << numeric_limits<int>::min() << " to "
<< numeric_limits<int>::max() << endl;
// float
cout << "float: " << numeric_limits<float>::lowest() << " to "
<< numeric_limits<float>::max() << endl;
// double
cout << "double: " << numeric_limits<double>::lowest() << " to "
<< numeric_limits<double>::max() << endl;
// long double
cout << "long double: " << numeric_limits<long double>::lowest() << " to "
<< numeric_limits<long double>::max() << endl;
// unsigned
cout << "unsigned: " << 0 << " to "
<< numeric_limits<unsigned>::max() << endl;
}