Type casting (or) Type conversion

                      The process of converting one datatype to another datatype is called as type casting or type conversion.

                    In C Language type conversion is an arithmetic expression will be done automatically. This is called implicit conversion. If we want to store a value of one datatype into a variable of another datatype, we must caste the value to be stored by preceding it with the type name in parenthesis. This is called explicit conversion or type casting.

Eg:
        int a, b;
        float c,d;
        a=5;
        b=2;
        c=a/b;        c=2.00        Automatic (or)  implicit conversion
        d=(float)a/b;    d=2.50    Type casting (or) explicit conversion

Program:


#include<stdio.h>
#include<conio.h>
void main()
{
    int a, b;
    float c, d;
    clrscr();
    printf("Enter any Two Numbers : ");
    scanf("%d%d", &a, &b);
    c=a/b;
    d=(float)a/b;
    printf("c=%.2f", c);
    printf("\nd=%.2f", d);
    getch();
}

Output:


Enter any Two Numbers : 5  2
c=2.00
d=2.50



No comments:

Post a Comment