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

It is the process in which data type of output of an arithmetic expression is automatically converted into a particular data type.  The output of expression will be of that data type which has highest number of bytes and range.

Rules of implicit conversion are:

Data type of Value1 Data type of  Value2 Data type of Output
short short short
short int int
short long long
short float float
short double double
short long double long double
short char short
int int int
int long long
int float float
int double double
int long double long double
int char int
long long long
long float float
long double double
long long double long double
long char long
     
float float float
float double double
float long double long double
float char float
     
double double double
double long double long double
double char double
     
long double long double long double
long double char long double
     
char char char



 Program
#include<stdio.h>
int main()
{
int num1,num2,num3;
float num4, num5;
num1=11;
num2=4;
num3=num1/num2;                                    /*Line 1*/
printf(“\nnum3=%d”,num3);
num4=num1/num2;                                    /*Line 2*/
printf(“\nnum4=%f”,num4);
num5=num1/num4;                                    /*Line 3*/
printf(“\nnum1=%f”,num5);
num3=num1/num4;                                    /*Line 4*/
printf(“\nnum3=%d”,num3);
return(0);
}
Output:
num3=2
num4=2.000000
num5=2.75
num3=2
Description
** In line 1, num1 and num2 are integer variables so result of num1/num2 is of integer type. 

In line 2, num1 and num2 are int variables so result of num1/num2 is of int type but it is assigned to float variable num4. So num4 will contain 2.000000.

In line 3, num1 is int and num4 is float type variable so result of num1/num2 is of float type. It is assigned to float variable num5. So num5 will contain 2.75.

In line 4, num1 is int and num4 is float type variable. Result of num1/num2 is of float type. It is assigned to int variable num3 So num3 will contain int value  ignoring digits after decimal point.

Best Books of Computer Science





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