隐式转化的问题

隐式转化虽然讲起来简单,但在实践过程中却很容易因为疏忽而犯错.比如下面这个程序

#include "stdafx.h"
#include "stdlib.h"
#include<iostream>
using namespace std;
int _tmain()
{
   double a=3/2; //a虽然定义成浮点型,但是结果却是1
   cout<<a<<endl;
   system("pause");
   return 0;
}

所以推荐的做法应该是将3写成3.0,2写成2.0,这样就是浮点数运算了.

猜你喜欢

转载自blog.csdn.net/l93919861/article/details/84645845