What is Type Casting Explain with example ?

What is Type Casting Explain with Example ?
Type Casting
-Implicit Type Casting
-Explicit Type Casting

Type Casting
Type casting is a method to convert a variable from one data type to another data type.
For example, if you want to change a Float into a Integer then you can type cast Float to Int. 

Implicit Type Casting
When the type casting is performed automatically  by the compiler, it is known as implicit type conversion
Example:
#include <stdio.h>
void main ()
{
    float x;
    x = 5/2;
    printf("%f",x);
}
Output:
2.000000


Explicit Type Casting
When the type casting is performed by the programmer using the cast operator , it is known as explicit type conversion
(data_type) Expression;

(int)b;
(float)a;

Example:
#include <stdio.h>
void main ()
{
    float x;
    x = (float)5/2;
    printf("%f",x);
}
Output:
2.500000





->Conversion from big data type to small data type may cause data truncation. For Example float to int causes loss of fraction part

 

0 comments:

Post a Comment