Q22. Why do you think type compatibility is required in assigning values?
Ans. When variables of one type are mixed with variables of another type, a type conversion will occur. In an assignment statement, the type conversion rule states:
The value of the right side (expression side) of the assignment is converted to the type of the left side (target variable).
Therefore, the types of right side and left side of an assignment should be compatible so that type conversion can take place. The compatible data types are mathematical data types that is, char, int, float, double.
For example, the following statement
ch = x; (where ch is char and x is int)
converts the value of x that is, integer to bit into ch, a character. Assuming that word size is 16-bit (2 bytes), the left higher order bits of the integer variables x are lopped off, leaving ch with lower 8 bits. Say, if x was having value 1417 (whose binary equivalent is 0000010110001001) then ch will have lower 8-bits that is, 10001001 resulting in loss of information.
When converting from integers to characters and long integers to integers, the appropriate amount of high-orders bits (depending upon target type’s size) will be removed. In many environments, this means that 8 bits will be lost when going from an integer to a character and 16 bits will be lost when going from a long integer to an integer.