here are part of my code.
float a = 12.5;
printf("%d\n", a);
printf("%d\n", (int)a);
printf("%d\n", *(int *)&a);
when I compile in windows, I got:
0
12
1094713344
and then, I compile in linux, I got:
-1437851864
12
1094713344
-1437851864 will be changed every time I excuted it.
my question is: in how does the “printf” function works in linux
It works very well, but why are you passing the wrong sort of data to it? The
%dspecifier expects andint, but you’re passing something else. Bad idea.If
floatandintare differently sized across the varargs barrier, this is undefined behavior. And sincefloatis typically promoted todoublewith varargs calls, if yourintis smaller than yourdoublethis will break.In short, this is really bad and broken code. Don’t do this.