Type Conversion

 

Rules for Type Conversion:

 

long mylong;

char mychar:

short int myint;

float myfloat = 7e4;   //Is that an error? Did you mean 0x7e4? No, it’s the exponential format and means 7 times 10 to the 4th power.

mychar =myint = mylong = myfloat;

 

You will get a compiler warning about moving a float into a long.

You will lose information when the value 70000 is moved into the 16 bit width of the short int. [short can hold numbers up to 65536, maximum.

 

mychar gets the value “p”   [01110000] Hex 70

myint gets 4464                   [00010001][01110000]  Hex 1170

mylong gets 70000               [00000000][00000001][00010001][0111000] Hex 11170

 

You can clearly see that the higher order bytes are dropped as the data is placed in ever smaller containers.

 

An explicit typecast can eliminate a compiler warning. See Chapter 20, Typecasts.