C++中求各種數據類型最大最小值問題

#include<iostream>
#include<limits>
using namespace std;
int main()
{
    cout << "min:" << numeric_limits<char>::min() << " max:" << numeric_limits<char>::max() << endl;
    cout << "min:" << numeric_limits<short>::min() << " max:" << numeric_limits<short>::max() << endl;
    cout << "min:" << numeric_limits<int>::min() << " max:" << numeric_limits<int>::max() << endl;
    cout << "min:" << numeric_limits<long>::min() << " max:" << numeric_limits<long>::max() << endl;
    cout << "min:" << numeric_limits<float>::min() << " max:" << numeric_limits<float>::max() << endl;
    cout << "min:" << numeric_limits<double>::min() << " max:" << numeric_limits<double>::max() << endl;
    cout << "min:" << numeric_limits<long double>::min() << " max:" << numeric_limits<long double>::max() << endl;
    cout << "min:" << numeric_limits<unsigned>::min() << " max:" << numeric_limits<unsigned>::max() << endl;
    return 0;
}

結果發現char的最大最小值輸出爲奇怪的符號,最後對char輸出強制轉化爲int類型後即可得到結果。

cout << "min:" << (int)numeric_limits<char>::min() << " max:" << (int)numeric_limits<char>::max() << endl;



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章