首页 > 试题广场 >

写一个程序打印出来各种基本类型、几个指针类型和几个你所选择的

[问答题]
写一个程序打印出来各种基本类型、几个指针类型和几个你所选择的枚举类型的大小。使用sizeof运算符。
#include<iostream>
using namespace std;
int main()
{
	cout << "bool: ";
	cout << sizeof(bool);
	cout << "\n";
	cout << "char: ";
	cout << sizeof(char);
	cout << "\n";
	cout << "int: ";
	cout << sizeof(int);
	cout << "\n";
	cout << "float: ";
	cout << sizeof(float);
	cout << "\n";
	cout << "double: ";
	cout << sizeof(double);
	cout << "\n";
	cout << "void: ";
	cout << sizeof(void);
	cout << "\n";
	cout << "wchar_t: ";
	cout << sizeof(wchar_t);
	cout << "\n";
	cout << "int *: ";
	cout << sizeof(int *);
	cout << "\n";
	enum color
	{
		red,
		green,
		black,
	};
	cout << "枚举型color: ";
	cout << sizeof(color);
	cout << "\n";
	return 0;
}

发表于 2020-05-05 21:01:23 回复(0)
#include<iostream>
using namespace std;
int main()
{
cout<<"长度:"<<endl;
cout<<"char:"<<sizeof(char)<<endl;
cout<<"int:"<<sizeof(int)<<endl;
cout<<"double:"<<sizeof(double)<<endl;
cout<<"float:"<<sizeof(float)<<endl;
cout<<"bool:"<<sizeof(bool)<<endl;
struct A {
int a;
char b;
double c;
bool d=true;
}
cout<<"struct A:"<<sizeof(A)<<endl;
union B {
int a;
char b;
double c;
bool d=true;
}
cout<<"union B:"<<sizeof(B)<<endl;
class C {
int a;
char b;
double c;
bool d=true;
}
cout<<"class C:"<<sizeof(C)<<endl;
cout<<"int *"<<sizeof(int *)<<endl;
return 0;
}
发表于 2020-12-03 11:17:04 回复(0)