Implicit type Conversion in C | Automatic type conversion in C

Preview
You must first complete Data Types in C Language before viewing this Lesson

Implicit type Conversion in C | Automatic type conversion in C

Implicit conversion means automatic conversion. It is the way in which the output of an arithmetic expression is converted automatically into a particular data type.

The basic rule of implicit conversion is that the output of an expression will be of that data type which has the highest number of bytes and range.

For example if one operand is of int type and other of float type, the output will be of float type as float type has larger size and range of values compared to int data type.

There are some rules of implicit conversion which are as follows:

Data type of first operandData type of second operandData type of Output
shortshortshort
shortintint
shortlonglong
shortfloatfloat
shortdoubledouble
shortlong doublelong double
shortcharshort
intintint
intlonglong
intfloatfloat
intdoubledouble
intlong doublelong double
intcharint
longlonglong
longfloatfloat
longdoubledouble
longlong doublelong double
longcharlong
   
floatfloatfloat
floatdoubledouble
floatlong doublelong double
floatcharfloat
   
doubledoubledouble
doublelong doublelong double
doublechardouble
   
long doublelong doublelong double
long doublecharlong double
   
charcharchar



 Program to demonstrate implicit conversion.
#include<stdio.h>
int main()
{
int a,b,c;
float d, e;
a=10;
b=3;
c=a/b;                                    /*Line 10*/
printf(“\nc=%d”,c);
d=a/b;                                    /*Line 12*/
printf(“\nd=%f”,d);
e=a/d;                                    /*Line 14*/
printf(“\ne=%f”,e);
c=a/d;                                    /*Line 16*/
printf(“\nc=%d”,c);
return(0);
}
Output:
c=3
d=3.000000
e=3.333333
c=3
Description

** In line 10 of above program, a and b are of int type so result of a/b will be of int type i.e. 3 which has been assigned to variable c which is also of int type.

So c  will contain 3.

In line 12, a and b are of int type so result of a/b will be of int type i.e. 3. which has been assigned to variable d which is also of float type.

So d will contain 3.000000.

In line 14, a is of int type and d is of float type so result of a/b will be of float type i.e. 3.333333 which has been assigned to variable e which is also of float type.

So e will contain 3.333333.

In line 16, a is of int type and d is of float type so result of a/b will be of float type i.e. 3.333333 which has been assigned to variable c which is of int type.

So c will contain 3 as int variable c can’t contain digits after decimal point.




Lesson tags: automatics type conversion in c, data type conversion in c, implicit conversion in c, type conversion in c
Back to: C Programming Language