Home Segments Index Top Previous Next

73: Mainline

If you want to tell C to convert an expression from one type to another explicitly, rather than relying on automatic conversion, possibly avoiding a quarrelsome compiler warning, you can cast the expression. To do casting, you prefix the expression with the name of the desired type in parentheses.

If, for example, i is an integer and d is a double, you can cast i to a double and d to an integer as follows:

(double) i         /* A double expression */ 
(int) d            /* An int expression   */ 

Note that the original types of the i and d variables remain undisturbed: i remains an int variable, and d remains a double variable.