C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers

114 comments

10,705 users

What is explicit casting ?

0 votes
20 views
asked Jan 10, 2014 by smith Geek (15,340 points)

1 Answer

0 votes

Explicit Casting :
                  This type of casting done by force not by the compiler. If the
operands are of diffrent types,the Higher type is  converted to the lower
type before the operation proceds by force.

for example :- if we do like this:

#include<stdio.h>
#include<conio.h>
void main()
{
  clrscr();
  int a=8,b=3;
  float c;
  c=a/b;                     
  printf("%f",c);
  getch();
 }
 
 Output willbe 2.000000 which is Incorrect.

 now we go with Explicitly casting

 #include<stdio.h>
 #include<conio.h>
 void main()
 {
  clrscr();
  int a=8,b=3;
  float c;
  c=(float)a/b;        // Explicitly  here a temporarly  converted or casted to float            
  printf("%f",c);
  getch();
 }

 Output willbe 2.666667 which is Correct.

 When to use :

 1) Type casting is used when you want to convert the value of one data type to another.
 2)One use for typecasting for is when you want to use the ASCII characters. For example, what if you want to create your own chart of all 256 ASCII characters. To do   this, you will need to use to typecast to allow you to print out the integer as its character equivalent.

 Important Points:

 1) Type casting does not change the actual value of variable, but the resultant   value may be put in temprory storage
 2) Type casting is done using a cast operator that is also a unary operator

answered Feb 5, 2014 by Anand Hooda Sr Member (2,730 points)

Related questions

0 votes
1 answer 17 views
17 views asked Jan 1, 2014 by smith Geek (15,340 points)
+1 vote
2 answers 166 views
166 views asked Jan 11, 2014 by sara Guru (24,570 points)
0 votes
0 answers 56 views
56 views asked Jan 11, 2014 by sara Guru (24,570 points)
0 votes
1 answer 15 views
15 views asked Jan 11, 2014 by sara Guru (24,570 points)
0 votes
1 answer 19 views
19 views asked Jan 11, 2014 by sara Guru (24,570 points)
...