Monday, June 9, 2008

Casting

One of the ways to manipulate a data type is to change the variable from one type to another. The form of conversion is known as casting. The actual syntax for casting between values is fairly simple. Put the data type you want to cast in front of the variable. as shown below.

int A;
char B;
A = (int)B;

The idea behind casting is very simple. However, there is a tricky part; you need to make sure that you understand all of the ins and outs of casting, because, if you are not careful, you could lose data during the cast. This is possible if the data type you are casting to is smaller than the orginal type.

No comments: