Typecasting / type conversion


typecasting

The conversion of one type of data to another type is called types conversion. When there is some type of data type(int, int) it will produce an answer in int which is auto, but when we need to convert mixed datatype value to another data type(int, float, char, etc.) we use typecasting. typecasting does not change the value but it changes the datatype of a given value or variable. For example, to see the answer of int datatype in float data type, we use typecasting(which if force full). Let's be clear with the given code.  

Syntax   

datatype expression; 

Float b; 

 

Code  

Question:- write a program to multiply any two user input integer values and print them in integer as well as in float.  


#include <stdio.h>
#include <conio.h>
void main()
{
    int ab;
    float c;
    printf("enter a value of a: ");
    scanf("%d", &a);
    printf("enter a value of b: ");
    scanf("%d", &b);
    c = (float)a * b;
    printf("the float answer of given integer is %f\n"c); // this will print float as usal
    printf("the integer answer of given integer is %d\n", (int)c); //  this will give an answer in int because we have typecast c in int
    getch();
}

output  

enter a value of a: 8 

enter a value of b: 5 

the floating answer of a given integer is 40.00000 

the integer answer of a given integer is 40 


Follow our page for regular updates.

0 Comments