Convert int to float: how it is done

Viewed 24297

I'm new to C programming language, and I'd like to ask a question.

Integer i here is casting to float then f (somehow) successfully represents 5.0:

int i = 5;
float f = i;   //Something happened here...

However if we try this approach:

int i = 5;
float f = *(float *)&i;

f would NOT get 5.0 since it interprets the bits stored in i in "float's way". So what magic the complier actually does in the first case? It seems a quite effort-taking job... Can someone specify that? Thanks.

7 Answers
Related