一、显示各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)();
二、全局变量的声明
【示例代码】
#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;
}
三、static的用法
【代码示例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;
}
【参考结果】
【说明】 注意函数中static的有无