Friday, March 27, 2009

Warning C4305: 'initializing' : truncation from 'double' to 'float'

int x=10;
float y=10.1;
char z='a';
std::cout << "x = " << x << std::endl;
std::cout << "y = " << y << std::endl;
std::cout << "z = " << z << std::endl;

this code will cause a compiler error in 2nd line where the float is declared.

It’s because the numerical literal 3.14159 is a double. Changing it to 10.1f to make it a float should fix this.

float y=10.1f;

or

double y=10.1;

No comments:

Post a Comment