k reeta wrote:Hi,
I am having troubling trying to figure out implicit promotions - since int and float are 32-bit, I assumed it'd be okay to assign one to another. It works for assigning an int to a float e.g.
int y=5;
float f=y;
But not the other way around, means when I try to assign a float to an int I get a compiler error -
float ff=5.0f;
int x=ff;
Why should this be so since both, float and int are 32-bit??
Thanks.
Here is the order in ascending:
BYTE <SHORT <INT <LONG<FLOAT<DOUBLE.....
Smaller value casted implicitly....(check your first example: float is greater so assigning int value to float -OK)
But not the other way around.
If you want to assign float value to int do explicit cast.
float ff=5.0f;
int x = (int)ff; int is lower in the order and you are assigning higher element to lower one