使用运算符sizeof(对象或类),显示各数据类型(char,int,short,long,float,double)所占的内存字节数
2个回答
展开全部
#include
<iostream.h>
int
main
(void)
{
cout
<<
"char
size
=
"
<<
sizeof(char)
<<
"
bytes\n";
cout
<<
"char*
size
=
"
<<
sizeof(char*)
<<
"
bytes\n";
cout
<<
"short
size
=
"
<<
sizeof(short)
<<
"
bytes\n";
cout
<<
"int
size
=
"
<<
sizeof(int)
<<
"
bytes\n";
cout
<<
"long
size
=
"
<<
sizeof(long)
<<
"
bytes\n";
cout
<<
"float
size
=
"
<<
sizeof(float)
<<
"
bytes\n";
cout
<<
"double
size
=
"
<<
sizeof(double)
<<
"
bytes\n";
cout
<<
"1.55
size
=
"
<<
sizeof(1.55)
<<
"
bytes\n";
cout
<<
"1.55l
size
=
"
<<
sizeof(1.55l)
<<
"
bytes\n";
cout
<<
"hello
size
=
"
<<
sizeof("hello")
<<
"
bytes\n";
}
我们运行这个程序,产生下面的结果(在作者的pc机上):
char
size
=
1
bytes
char*
size
=
2
bytes
short
size
=
2
bytes
int
size
=
2
bytes
long
size
=
4
bytes
float
size
=
4
bytes
double
size
=
8
bytes
1.55
size
=
8
bytes
1.55l
size
=
10
bytes
hello
size
=
6
bytes
<iostream.h>
int
main
(void)
{
cout
<<
"char
size
=
"
<<
sizeof(char)
<<
"
bytes\n";
cout
<<
"char*
size
=
"
<<
sizeof(char*)
<<
"
bytes\n";
cout
<<
"short
size
=
"
<<
sizeof(short)
<<
"
bytes\n";
cout
<<
"int
size
=
"
<<
sizeof(int)
<<
"
bytes\n";
cout
<<
"long
size
=
"
<<
sizeof(long)
<<
"
bytes\n";
cout
<<
"float
size
=
"
<<
sizeof(float)
<<
"
bytes\n";
cout
<<
"double
size
=
"
<<
sizeof(double)
<<
"
bytes\n";
cout
<<
"1.55
size
=
"
<<
sizeof(1.55)
<<
"
bytes\n";
cout
<<
"1.55l
size
=
"
<<
sizeof(1.55l)
<<
"
bytes\n";
cout
<<
"hello
size
=
"
<<
sizeof("hello")
<<
"
bytes\n";
}
我们运行这个程序,产生下面的结果(在作者的pc机上):
char
size
=
1
bytes
char*
size
=
2
bytes
short
size
=
2
bytes
int
size
=
2
bytes
long
size
=
4
bytes
float
size
=
4
bytes
double
size
=
8
bytes
1.55
size
=
8
bytes
1.55l
size
=
10
bytes
hello
size
=
6
bytes
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询