For example,
float f = 2.4;
int n = f + 1;
n = 3
variable f is 0x4019999a in memory, So I thought f + 1 = 0x4019999a + 1 but Computer doesn't. How to know f is 'float'? Even if f is just 0x4019999a in memory. Is the type of variable stored somewhere?
The type isn't stored explicitly (in an optimized production executable; debugging outputs have all sorts of extra information in them).
"So if the type isn't stored, how does it know how to handle f = f + 1
differently from n = n + 1
?" I hear you ask. :-) The answer is that the compiler knew the type when it was compiling, and it output different CPU instructions for those operations. In the f
case, it output instructions that work with floating point values, but in the n
case, it output instructions that work with two's complement integers.