2.编写一个程序,定义1个整型变量、1个单精度变量,1个双精度变量,一个字符变量,并赋值,最后将这些变量的值输出;同时编写程序计算各变量在VC++6.0中所占的字节数
时间: 2023-07-16 18:13:52 浏览: 245
下面是实现该程序的代码:
```c++
#include <iostream>
using namespace std;
int main()
{
int x = 123;
float y = 3.14f;
double z = 3.1415926;
char c = 'A';
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "z = " << z << endl;
cout << "c = " << c << endl;
cout << "sizeof(int) = " << sizeof(int) << " bytes" << endl;
cout << "sizeof(float) = " << sizeof(float) << " bytes" << endl;
cout << "sizeof(double) = " << sizeof(double) << " bytes" << endl;
cout << "sizeof(char) = " << sizeof(char) << " byte" << endl;
return 0;
}
```
输出结果如下:
```
x = 123
y = 3.14
z = 3.14159
c = A
sizeof(int) = 4 bytes
sizeof(float) = 4 bytes
sizeof(double) = 8 bytes
sizeof(char) = 1 byte
```
在 VC++6.0 中,int 和 float 都占用 4 个字节,double 占用 8 个字节,char 占用 1 个字节。可以使用 sizeof 运算符来计算变量在内存中所占用的字节数。
阅读全文