Program to Demonstrate Typecasting in C

Here is a program to demonstrate typecasting in C programming language.

#include
#include
void main(){
    float result;
    int num1, num2;
    clrscr();
    num1 = 10;
    num2 = 3;
    result = num1/num2;
    printf("\nBefore Typecasting: %f",result);
    /*
    As num1 and num2 are declared as int, it will return int value
    and it will get stored in the variable "result" as float i.e. 3.000000
    To solve this problem we can use typecasting.
    */
    result = (float)num1/num2;
    printf("\nAfter Typecasting: %f",result);
    getch();
}

Output:

Before Typecasting: 3.000000
After Typecasting: 3.333333

You may also like...

4 Responses

  1. daya says:

    why getch(); is used?? as printf will deliver the output pls explain

  2. Anonymous says:

    getch() is used only so you can see the output on the screen after you run the file on your computer. it has no real use in this question.

  3. in float after decimal point why there are 6 zeros

  1. October 25, 2017

    […] Program to Demonstrate Typecasting in C […]

Leave a Reply

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