The Curse of Typecasting

In the C language, it’s called typecasting: A variable of one type — for an instant — masquerades as another variable type. In Hollywood, however, it’s a curse: The typecast actor can play roles other than the one he’s famous for creating. That sucks.

C language typecasting doesn’t suck. Often it’s necessary to recast a variable as another type to perform an accurate calculation.

For example, you need to divide two integers. To obtain the correct calculation, you cast each as a float. The result can be used immediately or saved in a float variable, but it’s a floating-point result and not an int value that’s calculated.

For this month’s Exercise, your task is to take an int variable apple. Assign it the value 65. Then use three printf() statements to display the value of apple as an int, char, and float.

If you’re an old hand at C, this Exercise should be easy. If you’re new, then this Exercise is a welcome respite from my recent spate of angry, involved Exercises.

Please attempt this Exercise on your own before you take a peek at my solution.

Leave a Reply