問題:(numeric_limits<char>::min)()等函式無法顯示后續文字。
原始碼:
#include <iostream>
using namespace std;
// 變數宣告
extern int a, b;
extern int c;
extern float f;
int main ()
{
// 變數定義
int a, b;
int c;
float f;
// 實際初始化
a = 10;
b = 20;
c = a + b;
cout << c << endl ;
f = 70.0/3.0;
cout << f << endl ;
return 0;
}
結果:

明顯不對。
我注釋掉以下代碼卻能夠顯示后面的結果。
// cout << "char: \t\t" << "所占位元組數:" << sizeof(char);
// cout << "\t最大值:" << (numeric_limits<char>::max)(); //(numeric_limits<char>::min)()無法使用
// cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
// cout << "signed char: \t" << "所占位元組數:" << sizeof(signed char);
// cout << "\t最大值:" << (numeric_limits<signed char>::max)();
// cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
// cout << "unsigned char: \t" << "所占位元組數:" << sizeof(unsigned char);
// cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
// cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl; //本程式無法識別unsigned char大小,會出錯
查找了<limits>頭檔案,里面關于char函式有定義,請問這是怎么回事?
uj5u.com熱心網友回復:
沒看懂你要問什么????第一段代碼和后面有何聯系????
unsigned char 最大 肯定列印不出來啊。
uj5u.com熱心網友回復:
糟糕,頭腦暈了,多謝”真相重于對錯“提醒。代碼復制錯了,第一段代碼應該是下圖代碼。#include<iostream>
#include <limits>
using namespace std;
int main()
{
cout << "type: \t\t" << "************size**************"<< endl;
cout << "bool: \t\t" << "所占位元組數:" << sizeof(bool);
cout << "\t最大值:" << (numeric_limits<bool>::max)();
cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
cout << "char: \t\t" << "所占位元組數:" << sizeof(char);
cout << "\t最大值:" << (numeric_limits<char>::max)(); //(numeric_limits<char>::min)()無法使用
cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
cout << "signed char: \t" << "所占位元組數:" << sizeof(signed char);
cout << "\t最大值:" << (numeric_limits<signed char>::max)();
cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
cout << "unsigned char: \t" << "所占位元組數:" << sizeof(unsigned char);
cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl; //本程式無法識別unsigned char大小,會出錯
cout << "wchar_t: \t" << "所占位元組數:" << sizeof(wchar_t);
cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
cout << "short: \t\t" << "所占位元組數:" << sizeof(short);
cout << "\t最大值:" << (numeric_limits<short>::max)();
cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
cout << "int: \t\t" << "所占位元組數:" << sizeof(int);
cout << "\t最大值:" << (numeric_limits<int>::max)();
cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
cout << "unsigned: \t" << "所占位元組數:" << sizeof(unsigned);
cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
cout << "long: \t\t" << "所占位元組數:" << sizeof(long);
cout << "\t最大值:" << (numeric_limits<long>::max)();
cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
cout << "unsigned long: \t" << "所占位元組數:" << sizeof(unsigned long);
cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
cout << "double: \t" << "所占位元組數:" << sizeof(double);
cout << "\t最大值:" << (numeric_limits<double>::max)();
cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
cout << "long double: \t" << "所占位元組數:" << sizeof(long double);
cout << "\t最大值:" << (numeric_limits<long double>::max)();
cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
cout << "float: \t\t" << "所占位元組數:" << sizeof(float);
cout << "\t最大值:" << (numeric_limits<float>::max)();
cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
cout << "size_t: \t" << "所占位元組數:" << sizeof(size_t);
cout << "\t最大值:" << (numeric_limits<size_t>::max)();
cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
cout << "string: \t" << "所占位元組數:" << sizeof(string) << endl;
// << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;
cout << "type: \t\t" << "************size**************"<< endl;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/67780.html
標籤:C++ 語言
上一篇:多執行緒資料庫操作例外
下一篇:大一C語言問題,求教!
