1.编程实现:用sizeof运算符计算C++中char, short, int, long,

float, double 等基本数据类型所占的字节数,并按以下两列对齐格式输出。

#include<iostream.h>
#include<iomanip.h>
void main()
{
cout<<setw(6)<<"char"<<setw(3)<<sizeof(char)<<endl;
cout<<setw(6)<<"short"<<setw(3)<<sizeof(short)<<endl;
cout<<setw(6)<<"int"<<setw(3)<<sizeof(int)<<endl;
cout<<setw(6)<<"long"<<setw(3)<<sizeof(long)<<endl;
}
/*控制符endl代表换行,要指定输出列数或者对齐,可用控制符setw,其头文件是iomanip.h。如,setw(5)的作用是为后面的输出项预留5列,如输出长度不足5列,自动向右靠齐,如果超过5列,则按实际长度输出。在C中用printf函数输出时,可以指定输出格式。*/

参考资料:东北大学出版社《新概念C语言》

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-05-09
指针所占的字节数都是机器的位数
32位的机器占32/8=4字节
不用计算了吧
cout<<"sizeof(char*)="<<sizeof(char*)<<endl
<<"sizeof(short*)="<<sizeof(short*)<<endl
<<"sizeof(int*)="<<sizeof(int*)<<endl
<<"sizeof(long*)="<<sizeof(long*)<<endl
<<"sizeof(float*)="<<sizeof(float*)<<endl
<<"sizeof(double*)="<<sizeof(double*)<<endl;
第2个回答  2009-10-12
#include <iostream.h>

main()
{
cout<<"char:"<<sizeof(char)<<endl;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网