Type Conversion in C-implicit & Explicit | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Tuesday, June 11, 2019

Type Conversion in C-implicit & Explicit | My CS Tutorial


In this tutorial we read that what is type conversion and its types in C programming language.

Type conversion(casting) is very important in C language.


Type conversion in C | My CS Tutorial


Type Conversion


Type Conversion is use for converting a variable of one data type to another data type.The compiler will change one data to another data automatically.When the type of variable's data type of left side of assaiment operator(=) is not seeme as right side data type then the value of the expression is promoted or demoted according to the left hand side variable.
As example:

int a;
float b;
a=5.17;
b=40;
printf("a=%d",a);
printf("b=%f",b);


In this program we have entered the point value in the integer and the integer value in the float.When we get the output of this program then 5.17 will not be store in a it will store integer value like 5 and b not store 40(integer value).It store floating point vlue.It convert integer value in floating value.So it store 40.000000.

In C language Type Conversion have two types..
Implicit and Explicit.


Implicit type conversion


C provides implicit type conversion.implicit type Conversion is performed automatically by the compiler.When two different data types are present, the lower size data type should be automatically converted into high size data type before the
operation preceeds.

                 Data type

              Long double
                       ^
                  Double
                       ^
                    Float
                       ^
       Unsigned long long
                       ^
              Long long
                       ^
           Unsigned long
                       ^
                   Long
                       ^
            Unsigned int
                       ^
                     Int
                       ^
           Char/short int

The order of these data types are lowest to highest.

Example:

int x=12;
float y=5.7,z;
z=x*y;
printf("z=%f",z);

In this example we take x in integer and y and z in float.Compiler exicute z=x*y;  this opration. In implicit type conversion, lower size data type automatically convert in higer size data type.In this example int(x) is lower size data type so it is convert in float automatically and result will be get in float.The result is
z=68.400000
Which is completely correct


Explicit Type Conversion


In the Explicit Conversion, the user himself tells what to do and in which data type is to be changed.
As the type conversion is automatically done by the compiler, explicit conversion can be done according to the needs of the user.

Syntax:

(Data type)expression

Example:

(Without type casting)

int x;
int y;
float z;
z=x/y;
printf("z=%f",z);

without type casting the result of this example is z=2.000000

In this examle,we take x and y in integer and z in float. If compiler exicute z=x/y this expression the result will be get in float but answer not correct properly. So we use float in that expression as
 z=(float)x/y;
shown below...

(With type Casting)

int x;
int y;
float z;
z=(float)x/y;
printf("z=%f",z);

If we do this the result will be get in float. The answer is z=2.500000
it is properly correct.



Type conversion is a most important feature of c language.Type Conversion is use for converting a variable of one data type to another data type.
        It Convert lower size data type into higher size data type automatically. In C language Type Conversion(Type Casting) is very usefull. It classified in two states.





No comments:

Post a Comment