search
Login

Categories


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

3.8k questions

4k answers

117 comments

10.7k users

0 votes
21 views
asked Geek 15.3k points 14 39 58

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 Sr Member 2.7k points 1 4

Related questions

0 votes
1 answer 18 views
18 views
asked smith Geek 15.3k points 14 39 58
1 vote
2 answers 167 views
167 views
asked sara Guru 24.6k points 29 51 75
0 votes
0 answers 57 views
57 views
asked sara Guru 24.6k points 29 51 75
0 votes
1 answer 16 views
16 views
asked sara Guru 24.6k points 29 51 75
0 votes
1 answer 21 views
21 views
asked sara Guru 24.6k points 29 51 75
...