Typecasting and type conversion in C programming language

Typecasting and type conversion  are designed to convert a variable from one datatype to other

Type conversion

Type conversion can be done by implicitly and?it is used, if the two-variable have a different datatype

Implicit

Implicit conversion mean’s the data types are automatically adjusted by a compiler from the hierarchy of datatypes and The data types are adjusted from lower-level order to higher-level order of data types

Rendered by QuickLaTeX.com

If int a=2 and float b=2.5 then find a x b;

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a=2;
    float b=2.5;
        printf("c=%d",a*b);
    return 0;
}

Output

error : warning format '%d' expects argument of 'int' but argument 2 has type 'double'

Rendered by QuickLaTeX.com

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a=2;
    float b=2.5;
        printf("c=%f",a*b);
    return 0;
}

Output

5.000000

Typecasting

typecasting in contrast, achieved by explicitly. that is to say, forcefully conversion

The above problem of type conversion(automatic conversion by the compiler) can be avoided by using Typecasting

Syntax
(data type) expression;
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a=2;
    float b=2.5;
    int c = (int)a*b;
        printf("c=%d",c);
    return 0;
}

Output

c=5

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.