Type casting

·

The value of an expression can be converted to a different data type. If desired to do so the expression must be preceded by the name of the desired data type enclosed in parentheses.

(data type) expression

This type of construction is known as a cast ( It is also called a type cast.)

Suppose that i is an integer variable whose value is 7, and f is floating- point variable whose value is 8.5. The expression (i +f) % 4 is invalid because the first operand (i + f) is floating point rather than integer. However the expression ((int) (i + f)) % 4 forces the first operand to be an integer and is therefore valid, resulting in the integer remainder 3.

About Me

Blog Archive