This causes the C compiler to know that space is to be reserved for storing the values of the variables. By stating a variable's type, the C compiler knows how much space in memory is to be set aside. Although not explicitly set by the ANSI C standard, the stand implies the minimum number of bits to be used for each variable type. For instance, ANSI C requires that type int be capable of handling a range of -32767 to 32767 for int type variables. This requires 16 bits or 2 bytes of memory. Therefore, declaring the variable month as int indicates that 16 bits or 2 bytes of memory should be reserved for this variable's value. On the other hand, a float type value typically occupies 4 bytes or 32 bits. Therefore, declaring a variable to be a float requires 4 bytes of memory to be reserved.
In addition, C uses different types of binary codes for integers and reals. This means that, for example, the bit pattern for 32 stored as an int is completely different from the bit pattern for storing 32, as a float. It is important that you keep this in mind. Forgetting this fact will lead to errors. For instance, if printf attempts to read a memory cell that contains an int but it expects a float to be in the cell it will misinterpret the cell contents and print something completely wrong. Since we, the programmers, tell printf what to expect in a memory cell, we must tell it correctly.
No comments:
Post a Comment