folat i = 0.1; 警告

今天的用途  float i = 0.1;那么编译器警告实际:

 

#include <iostream>
using namespace std;
int main()
{
	float k = 0.1;//警告 initializing' : truncation from 'const double' to 'float'
	return 0;
}


查阅资料(钱能的C++书籍)中说到 一个实型常数假设没有不论什么说明。表示double类型,要表示float型类型,必须在后面加上f或者F。

 

这似乎非常详细并没有注意到,C++的过多细节。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

这段代码中存在一些类型转换错误和未初始化变量错误。具体来说: 1. `float f1=0.1;` 中的 0.1 是一个 double 类型的字面量,需要强制转换为 float 类型,即 `float f1 = (float)0.1;` 2. `i = i * 0.1;` 中的 0.1 同样是 double 类型的字面量,需要强制转换为 int 类型,即 `i = (int)(i * 0.1);` 3. `float f2=1234;` 中的 1234 是 int 类型的字面量,需要强制转换为 float 类型,即 `float f2 = (float)1234;` 4. `long s2=88888888888;` 中的 88888888888 超出了 int 类型的取值范围,需要在末尾加上 L 表示 long 类型的字面量,即 `long s2 = 88888888888L;` 5. `double d1 = 1e20;` 中的 1e20 是科学计数法表示的 double 类型的字面量,无需转换。 6. `byte b1 = 10, b2 = 12, b3 = 131;` 中的 131 超出了 byte 类型的取值范围,需要强制转换为 byte 类型,即 `byte b3 = (byte)131;` 7. `int i=1, j;` 中的 j 未初始化,需要先给 j 赋一个初始值。 8. `j = j+10;` 中的 j 仍然未初始化,需要先给 j 赋一个初始值。 9. `byte b = b1+b2;` 中的 b1 和 b2 都是 byte 类型,相加之后会自动提升为 int 类型,需要强制转换为 byte 类型,即 `byte b = (byte)(b1 + b2);` 正确的代码应该是这样的: ``` public class TestDataConvert { public static void main(String[] args) { int i = 1, j = 0; float f1 = (float)0.1; i = (int)(i * 0.1); float f2 = (float)1234; long s2 = 88888888888L; double d1 = 1e20, d2 = 125; byte b1 = 10, b2 = 12, b3 = (byte)131; j = j + 10; byte b = (byte)(b1 + b2); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值