Can You Define Type Casting In A Simple Way?

2

2 Answers

raaga Profile
raaga answered
The term type casting is mostly used in computer programming languages. Type casting generally refers to the conversion of values from one data type to another in a programming code. The type casting can be done in two common ways, implicitly and explicitly. Thus, there are two types of type casting as:

1. implicit/automatic type casting
2. explicit type casting
Implicit type casting as its name shows it is done automatically by the compiler. Programming languages provide a facility to convert the data types of values to another data type. When a program code is compiled, the compiler make these allowed type conversions automatically when these are needed for correct computation. As we know that in any mathematical expression, the data types of all the operands must be same but sometimes violations occur due to the programmer's mistake.

Normally in implicit type casting the lower data type can be converted to some higher data type in an expression. But if the programmer wants to convert a value from a lower data type to a higher data type, he must do it explicitly by explicit type casting. The value that we want to convert to a higher data type must be specified explicitly in the program code. Because the compiler can not do that.
ch ch Profile
ch ch answered
We have data types such as int,float ,char,double..
Int*Int gives int only.similarly float*float gives float only.

But in some cases for example (in divison)  4/3 gives float .But 4 and 3 are integers.In such cases we have to change data type of ints temporarily as floats.This temporary conversions of one data type to another is called type casting.

Casting means changing.Type casting gives the meaning of changing the data type.Let look at the simple program which involves type casting.
/*program for divison of two numbers*/
#include
main()
{
int I,j;
float k;
printf("Input two numbers:");
scanf("%d %d",&I,&j);
k=(float)I/j;    /*type casting*/
printf("%f",k);
}

Answer Question

Anonymous