1.各C ++データ型のバイト数、最大値、最小値を表示します
【サンプルコード】
#include<iostream>
#include<limits>
using namespace std;
int main()
{
cout << "显示数据类型的长度、最大值以及最小值" << endl;
cout << "bool\t所占字节数:" << sizeof(bool);
cout << " max: " << (numeric_limits<bool>::max)();
cout << " min: " << (numeric_limits<bool>::min)() << endl;
cout << "char\t所占字节数:" << sizeof(char);
cout << " max: " << (numeric_limits<char>::max)();
cout << " min: " << (numeric_limits<char>::min)() << endl;
cout << "unsigned char\t所占字节数:" << sizeof(unsigned char);
cout << " max: " << (numeric_limits<unsigned char>::max)();
cout << " min: " << (numeric_limits<unsigned char>::min)() << endl;
cout << "signed char\t所占字节数:" << sizeof(signed char);
cout << " max: " << (numeric_limits<signed char>::max)();
cout << " min: " << (numeric_limits<signed char>::min)() << endl;
cout << "int\t所占字节数:" << sizeof(int);
cout << " max: " << (numeric_limits<int>::max)();
cout << " min: " << (numeric_limits<int>::min)() << endl;
cout << "unsigned int\t所占字节数:" << sizeof(unsigned int);
cout << " max: " << (numeric_limits<unsigned int>::max)();
cout << " min: " << (numeric_limits<unsigned int>::min)() << endl;
cout << "signed int\t所占字节数:" << sizeof(signed int);
cout << " max: " << (numeric_limits<signed int>::max)();
cout << " min: " << (numeric_limits<signed int>::min)() << endl;
cout << "unsigned short int\t所占字节数:" << sizeof(unsigned short int);
cout << " max: " << (numeric_limits<unsigned short int>::max)();
cout << " min: " << (numeric_limits<unsigned short int>::min)() << endl;
cout << "signed short int\t所占字节数:" << sizeof(signed short int);
cout << " max: " << (numeric_limits<signed short int>::max)();
cout << " min: " << (numeric_limits<signed short int>::min)() << endl;
cout << "long int\t所占字节数:" << sizeof(long int);
cout << " max: " << (numeric_limits<long int>::max)();
cout << " min: " << (numeric_limits<long int>::min)() << endl;
cout << "unsigned long int\t所占字节数:" << sizeof(unsigned long int);
cout << " max: " << (numeric_limits<unsigned long int>::max)();
cout << " min: " << (numeric_limits<unsigned long int>::min)() << endl;
cout << "signed long int\t所占字节数:" << sizeof(signed long int);
cout << " max: " << (numeric_limits<signed long int>::max)();
cout << " min: " << (numeric_limits<signed long int>::min)() << endl;
cout << "float\t所占字节数:" << sizeof(float);
cout << " max: " << (numeric_limits<float>::max)();
cout << " min: " << (numeric_limits<float>::min)() << endl;
cout << "double\t所占字节数:" << sizeof(double);
cout << " max: " << (numeric_limits<double>::max)();
cout << " min: " << (numeric_limits<double>::min)() << endl;
cout << "long double\t所占字节数:" << sizeof(long double);
cout << " max: " << (numeric_limits<long double>::max)();
cout << " min: " << (numeric_limits<long double>::min)() << endl;
cout << "wchar_t\t所占字节数:" << sizeof(wchar_t);
cout << " max: " << (numeric_limits<wchar_t>::max)();
cout << " min: " << (numeric_limits<wchar_t>::min)() << endl;
system("pause");
return 0;
}
【参考結果】
【説明】
最大値cout << (numeric_limits<数据类型>::max)();
を見つける:最小値を見つける:cout << (numeric_limits<数据类型>::min)();
2つ目は、グローバル変数の宣言です。
【サンプルコード】
#include<iostream>
using namespace std;
int a;
int main()
{
int b = 3, c = 4;
a = b + c;
cout << "a = " << a << endl;
system("pause");
return 0;
}
三、静的の使用法
【コード例1】
#include<iostream>
using namespace std;
int coun = 10;
void func(void);
int main()
{
while (coun--)
{
func();
}
system("pause");
return 0;
}
void func(void)
{
static int i = 1;
i++;
cout << "i = " << i;
cout << " coun = " << coun << endl;
}
【参考結果】
【サンプルコード2】
#include<iostream>
using namespace std;
int coun = 10;
void func(void);
int main()
{
while (coun--)
{
func();
}
system("pause");
return 0;
}
void func(void)
{
int i = 1;
i++;
cout << "i = " << i;
cout << " coun = " << coun << endl;
}
【参考結果】
【説明】関数内の静電気の有無に注意してください。