4.28编写一段程序,输出每一种内置类型所占空间大小。
#include <iostream>
#include <string>
using namespace std;
int main(){
int a;
short b;
long c;
char d;
wchar_t e;
bool f;
float g;
double h;
long double i;
unsigned int j;
unsigned short k;
unsigned long l;
cout << "int:" << sizeof a << endl;
cout << "short:" << sizeof b << endl;
cout << "long:" << sizeof c << endl;
cout << "char:" << sizeof d << endl;
cout << "wchar_t:" << sizeof e << endl;
cout << "bool:" << sizeof f << endl;
cout << "float:" << sizeof g << endl;
cout << "double:" << sizeof h << endl;
cout << "long double:" << sizeof i << endl;
cout << "unsigned int:" << sizeof j << endl;
cout << "unsigned short:" << sizeof k << endl;
cout << "unsigned long:" << sizeof l << endl;
return 0;
}
结果:
int:4
short:2
long:4
char:1
wchar_t:2
bool:1
float:4
double:8
long double:8
unsigned int:4
unsigned short:2
unsigned long:4
4.29
int x[10]; int *p = x;
cout << sizeof(x) / sizeof(*x) << endl;//10
cout << sizeof(p) / sizeof(*p) << endl;//1
4.30
(a)sizeof x + y -----sizeof(x + y)
(b)sizeof p->mem[i]-----sizeof(p->mem[i])
(a)sizeof a < b -----sizeof(a < b)
(b)sizeof f()-----sizeof(f())