2020-03-22 C++ basic exercises

1. Display the number of bytes, the maximum value and the minimum value of each C++ data type

[Sample code]

#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;
}

[Reference results]
Insert picture description here
[Description]

Find the maximum value: cout << (numeric_limits<数据类型>::max)();
Find the minimum value:cout << (numeric_limits<数据类型>::min)();

Two, the declaration of global variables

[Sample code]

#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;
}

Three, the usage of static

[Code example 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;

}

[Reference results]
Insert picture description here
[Sample code 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;

}

[Reference results]
Insert picture description here
[Explanation] Pay attention to whether there is static in the function

Guess you like

Origin blog.csdn.net/qq_42616280/article/details/115088236