C Tutorial Truncation
Introduction to C language
Integer types
Char constants
Int constants
Type combination and promotion
Int overflow
Floating point types
Comments
Variables
Assignment operator
Truncation
Int vs float arithmatic
Mathematical operators
Unary Increment Operators
Pre and Post Variations
C Programming Cleverness and Ego Issues
Relational Operators
Logical Operators
Bitwise Operators
Other Assignment Operators

 

 
 
 
 

 

HOME
 

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.

 

char ch;

int i;

 

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.

 

double pi;

int i;

 

pi = 3.14159;

i = pi;     // truncation of a double to fit in an int

// i is now 3

 

Want more information and Video

 

 ???