我正在使用 Bjarne Stroustrup 的《C++ 编程语言》学习 C++,同时做练习 4.5(在您的系统上,以下类型的最大和最小值是什么:char、short、int、long、float、double ,长双精度,无符号)我写道:
#include <iostream>
#include <limits>
using namespace std;
int main(){
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;
}
输出是真的很奇怪:
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
有人知道为什么这些值是错误的吗? 我期望 char 为 -128 到 127。
按照向导(@Iłya Bursov,和@n.m.可能是人工智能)的建议,问题是我在十月显示答案(由于同一文件中的另一个练习,没有在问题中提出,但他们解决了) ).
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;
}