float to int unexpected behaviour

前端 未结 6 834
情话喂你
情话喂你 2020-11-30 16:05

can you please explain the o/p behavior of this program.

int main()
{
  float a = 12.5;
  printf(\"%d\\n\", a);
  printf(\"%d\\n\", *(int *)&a);
  return         


        
6条回答
  •  心在旅途
    2020-11-30 16:44

    The difference is that in one case the compiler is doing an implicit cast (I assume you get 12 from the first one) while in the second case it is taking the memory containing the floating point value and interpreting it as an int. It would help if you included what the actual output was.

提交回复
热议问题