Introduction to C language
Type combination and promotion
Floating point types
Int vs float arithmatic
Unary Increment Operators
Pre and Post Variations
C Programming Cleverness and Ego Issues
Other Assignment Operators
The opposite of promotion, truncation moves a value from a type to a smaller type. In
that case, the compiler just drops the extra bits. It may or may not generate a compile
time warning of the loss of information. Assigning from an integer to a smaller integer
(e.g.. long to int, or int to char) drops the most significant bits. Assigning from a floating point type to an integer drops the fractional part of the number.
i = 321;
ch = i; // truncation of an int value to fit in a char
// ch is now 65
The assignment will drop the upper bits of the int 321. The lower 8 bits of the number
321 represents the number 65 (321 - 256). So the value of ch will be (char)65 which
happens to be 'A'.
The assignment of a floating point type to an integer type will drop the fractional part of the number. The following code will set i to the value 3. This happens when assigning a floating point number to an integer or passing a floating point number to a function which
takes an integer.
pi = 3.14159;
i = pi; // truncation of a double to fit in an int
// i is now 3
Want more information and Video