1. Muestra el número de bytes, el valor máximo y el valor mínimo de cada tipo de datos C ++
[Código de muestra]
#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;
}
[Resultados de la referencia]
[Descripción]
Encuentre el valor máximo: cout << (numeric_limits<数据类型>::max)();
Encuentre el valor mínimo:cout << (numeric_limits<数据类型>::min)();
Dos, la declaración de variables globales.
[Código de muestra]
#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;
}
Tres, el uso de estática
[Ejemplo de código 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;
}
[Resultados de referencia]
[Código de muestra 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;
}
[Resultados de referencia]
[Explicación] Preste atención a la presencia o ausencia de estática en la función