The first three lines of the program assign values to all nine of the defined variables so we can manipulate some of the data between the different types.
Since, as mentioned above, a "char" data type is in reality an "integer" data type, no special considerations need be taken to promote a "char" to an "int" variable. When going the other way, there is no standard, so you may simply get garbage if the value is within the range of zero to 255. In the second line therefore, when attempting to set x (a char) to -27, you may or may not get a well defined answer, it depends on your particular implementation of C.
The third line illustrates the simplicity of translating an integer into a "float", simply assign it the new value and the system will do the proper conversion. When going the other way however, there is an added complication. Since there may be a fractional part of the floating point number, the system must decide what to do with it. By definition, it will truncate it. This program produces no output, and we haven’t covered a way to print out "char" and "float" type variables, so you can’t really get in to this program and play with the results, but the next
program will cover this for you.
Characteristics of Variable Types
One unfortunate problem with C (and some other languages) is that the actual size of variables is somewhat dependant upon the machine (and compiler) being used. Difficulties are sometimes experienced when moving code from one machine to another due to this.
A general rule of thumb for modern compilers is that char is at least 8 bits, short is at least 16 bits, long is at least 32 bits, int is the same as either short or long (which causes lots of code conversions come unstuck), float is at least 32 bits, and double is at least as wide as float. As a consequence, you should use either short or long in preference to int (despite its heavy use in this tutorial), and should avoid double where possible. Note that using short instead of int may save an extra memory access, and help speed up code, especially in large
loops.
integer 32 bit (assembles as .l, or ±2147483648)
character 8 bit (0 to 255)
float 32 bit (about 1038)
double 64 bit (10308 maximum)
short 16 bit (assembles as .w, or ±32767)
long 32 bit (assembles as .l, or ±2147483648)
Byte ordering is another problem encountered when converting programs from one machine to another. In the 68000, the low byte of a 16 bit word is the second, odd numbered, byte. The bytes in a 32 bit long are laid out most significant byte at the address, with the least significant byte below it. Intel chips use the opposite order. This byte sex problem leads to arguments akin to those in religion and politics, and just as likely to lead to real solutions.
Google Search
Custom Search