The perfect place for easy learning...

C Programming Language

×

Topics List

Place your ad here

Type Casting and Conversion in C

In a programming language, the expression contains data values of same datatype or different datatypes. When the expression contains similar datatype values then it is evaluated without any problem. But if the expression contains two or more different datatype values then they must be converted to single datatype of destination datatype. Here, destination is the location where the final result of that expression is stored. For example,the multiplication of an integer data value with float data value and storing the result into a float variable. In this case, the integer value must be converted to float value so that the final result is a float datatype value.
In c programming language, the data conversion is performed in two different methods as follows...

  1. Type Conversion
  2. Type Casting

Type Conversion

The type conversion is the process of converting a data value from one datatype to another datatype automatically by the compiler. Sometimes type conversion is also called as implicit type conversion. The implicit type conversion is automatically performed by the compiler.
For example, in c programming language, when we assign an integer value to a float variable the integer value automically gets converted to float value by adding decimal value 0. And when a float value is assigned to an integer variable the float value automatically gets converted to integer value by removing the decimal value. To understand more about type conversion observe the following...

int i = 10 ;
float x = 15.5 ;
char ch = 'A' ;

i = x ; =======> x value 15.5 is converted as 15 and assigned to variable i

x = i ; =======> Here i value 10 is converted as 10.000000 and assigned to variable x

i = ch ; =======> Here the ASCII value of A (65) is assigned to i

Example Program

#include<stdio.h>
#include<conio.h>

void main(){
   int i = 95 ;
   float x = 90.99 ;
   char ch = 'A' ;
   
   i = x ;
   printf("i value is %d\n",i);
   x = i ;
   printf("x value is %f\n",x);
   i = ch ;
   printf("i value is %d\n",i);   

}

Output:

printf in c programming

In the above program, we assign i = x, i.e., float variable value is assigned to integer variable. Here, the compiler automatically converts the float value (90.99) into integer value (90) by removing the decimal part of the float value (90.99) and then it is assigned to variable i. Similarly when we assign x = i, the integer value (90) gets converted to float value (90.000000) by adding zero as decimal part.

Type Casting

Type casting is also called as explicit type conversion. Compiler converts data from one datatype to another datatype implicitly. When compiler converts implicitly, there may be a data loss.In such case, we convert the data from one datatype to another datatype using explicit type conversion. To perform this we use the unary cast operator. To convert data from one type to another type we specify the target datatype in paranthesis as a prefix to the data value that has to be converted. The general syntax of type casting is as follows...

(TargetDatatype) DataValue

Example

int totalMarks = 450, maxMarks = 600 ;
float average ;

average = (float) totalMarks / maxMarks * 100 ;

In the above example code, both totalMarks and maxMarks are integer data values. When we perform totalMarks / maxMarks the result is a float value, but the destination (average) datatype is float. So we use type casting to convert totalMarks and maxMarks into float datatype.

Example Program

#include<stdio.h>
#include<conio.h>

void main(){
   int a, b, c ;
   float avg ;
   printf("Enter any three integer values : ") ;
   scanf("%d%d%d", &a, &b, &c) ;
   
   avg = (a + b + c) / 3 ;
   printf("avg before casting = %f\n",avg);
   
   avg = (float)(a + b + c) / 3 ;
   printf("avg after casting = %f\n",avg);
}

Output:

printf in c programming

Place your ad here
Place your ad here