【C++】各种数据类型混合运算

#include <iostream>
#include <iomanip>
using namespace std;

int main(void)
{
	cout << "float    : " << sizeof(float) << endl;
	cout << "double   : " << sizeof(double) << endl;
	cout << "int      : " << sizeof(int) << endl;
	cout << "short    : " << sizeof(short) << endl;
	cout << "char     : " << sizeof(char) << endl;
	cout << "unsigned : " << sizeof(unsigned) << endl;
	cout << "long     : " << sizeof(long) << endl;
	cout << endl;

	cout << "float + float = float" << endl;
	float a1 = 1.2f, a2 = 2.3f;
	cout << "sizeof : " << sizeof(a1 + a2) << endl;

	cout << "char + char = int" << endl;
	char b1 = 'a', b2 = '\x1';
	cout << "sizeof : " << sizeof(b1 + b2) << endl;

	cout << "short + short = int" << endl;
	short c1 = 12, c2 = 34;
	cout << "sizeof : " << sizeof(c1 + c2) << endl;

	cout << "char + int = int" << endl;
	char d1 = 'b'; int d2 = -1;
	cout << "sizeof : " << sizeof(d1 + d2) << endl;

	cout << "short + int = int" << endl;
	short e1 = 2; int e2 = 1;
	cout << "sizeof : " << sizeof(e1 + e2) << endl;

	cout << "int + unsigned = unsigned" << endl;
	int f1 = 1; unsigned f2 = -2;
	cout << "1 + -2 = " << f1 + f2 << endl;

	cout << "unsigned + long = unsigned" << endl;
	unsigned g1 = -2; long g2 = 1;
	cout << "-2 + 1 = " << g1 + g2 << endl;

	cout << "long + float = float" << endl;
	long h1 = 1; float h2 = 0.1f;
	cout << "1 + 0.1 = " << h1 + h2 << endl;

	cout << "float + double = double" << endl;
	float i1 = 0.2f; double i2 = 0.4;
	cout << "sizeof : " << sizeof(i1 + i2) << endl;

	return 0;
}

运行结果:
结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值