Tuesday, 24 June 2014

What is the difference between Type Casting and Automatic Type Conversion in C++...with Examples.

Difference Between Type Casting and Automatic Type Conversion ...with Examples.



Automatic Type Conversion

IN C++  an automatic type casting  or implicit type conversion is a type of data type conversion which is  performed by the compiler on its own. It basically occurs whenever an expression has different data type .In this type of conversion , the data type which are smaller are converted into larger data type to avoid data loss.  

Example:

int x=3,y=1,c;
float p=11.5,q;
q=p/x;

In the above case the expression p/x, x's type will automatically be promoted to bigger type float  and then the expression will be evaluated .

TYPE CASTING 

The type casting or explicit type conversion  is user - defined forced conversion . It is basically done with the help of operator ( ) called as casting operator . 

Example:

float p=(float) 22/7;

Explanation 

Then 22 will be first converted to float type and this expression will get  evaluated.

No comments:

Post a Comment