C Program: Data type casting

CWC
1 Min Read

In C program data type casting is a way to convert a variable from one data type to another data type.

#include<stdio.h>
#include<conio.h>
main()  
{
int x=10,y,sum;  
float z=12.34,abc;  
clrscr();
y = x;  
printf("\nValue of x is %d",x);  
printf("\nValue of y is %d",y);  
abc = x;
printf("\nStored integer in float. Value of abc is %.2f",abc);
abc = z;  
printf("\nStored float to float. Value of abc is %.2f",abc);
abc = (int) z;//casting float to ints
printf("\nStoring integer part of float. Value of abc is %.2f",abc);
x = 20000;  
y = 20000;
sum = (x+y)/100;  
printf("\nWithout casting, the sum is %d",sum);
sum = ((long)x+y)/100;  
printf("\nAfter casting, The sum is %d",sum);
getch();  
return 0;
}

 

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version