0

what is type cast operator

1st Jul 2016, 1:04 PM
happy
2 Answers
+ 1
It is used to change the type of a given variable. int a = 5; int b = 2; When doing a / b the compiler evaluates that to int(a) / int(b) and int / int -> int. So this expression evaluates to 2, which may not be the answer you want. To get the answer yoy want you need to type cast one of the two variables. (double)a / b or double(a) / b evaluates to double / int -> double so the answer is 2.5, which is the correct answer. Anyways, the default type cast operator doesnt always apply. There is a question here from a couple days ago where I asked what types of type cast techniques there are. Stefan gives a really good and understandable answer.
1st Jul 2016, 1:25 PM
Garme Kain
Garme Kain - avatar
0
it refers to the process of changing the data type of the value stored in a variable
4th Jul 2016, 8:07 AM
Varshini
Varshini - avatar