Saturday 15 December 2012

Typecasting in C | Miscelleneous Features |Definition with example and with comments


What is typecasting?
Converting one datatype into another data type is known as typecasting.
for example conversion of int to float and float to  int , int to char and so on..
Basically there is two types of typecasting ..
1->Implicit typecasting
2->Explicit type casting


1:-Implicit typecasting -type casting done by system.
int a=10.8 float

now here float values is  typecasted to int now a=10 and 0.8 is ignored
float b=25
integer value is typecasted to  float b=15.0
Now let see invalid example of  typecasting.
int a ="25"
char a[]=75;
there is no typecasting when string is involved.



2;>Explicit type casting .
type casting  is done by programmer.
int a=9
int b=2

a/b=9/2=4
(float)a/b=9.0/2=4.5
a/(float)b=9/2.0=4.5 variable a and b are type casted to float . they  become 9.0 and 2.0 .
Their values continued to be 9 & 2 in rest of program.



/*How to write Program to illustrate use of typecasting*/
#include<stdio.h>
#include<conio.h>

//main function
void main()
{
float a;
int x=6,y=4;
clrscr();

//Without typecasting x/y
a=x/y;
printf("\n Value of a without typecasting =%f",a);


////After typecasting x/y to float
a=(float) x/y;
printf("\n Value of a after typecasting =%f",a);
getch();
}

Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: