Home > Backend Development > C++ > body text

What are implicit type conversions and explicit type conversions in C language?

PHPz
Release: 2023-09-08 22:13:01
forward
1061 people have browsed it

Converting one data type to another is called type conversion.

  • Implicit type conversion
  • Explicit type conversion
  • ul>

    Implicit type conversion

    • When operating When numbers have different data types, the compiler provides implicit type conversion.

    • It is done automatically by the compiler by converting smaller data types to larger data types.

    int i,x;
    float f;
    double d;
    long int l;
    Copy after login

    What are implicit type conversions and explicit type conversions in C language?

    Here, the above expression finally evaluates to a "double" value.

    Example

    The following is an example of implicit type conversion -

    int x;
    for(x=97; x<=122; x++){
       printf("%c", x); /*Implicit casting from int to char %c*/
    }
    Copy after login

    Explicit type conversion

    • Explicit type conversion by User completion using the (type) operator.

    • Before performing the conversion, a runtime check is made to see if the target type can hold the source value.

    int a,c;
    float b;
    c = (int) a + b
    Copy after login

    Here, the result of 'a b' is explicitly converted to 'int' and then assigned to 'c'.

    Example

    The following is an example conversion of explicit type-

    int x;
    for(x=97; x<=122; x++){
       printf("%c", (char)x); /*Explicit casting from int to char*/
    }
    Copy after login

    Let us understand the difference between two type conversions through an example-

    Example (Implicit conversion)

    Real-time demonstration

    #include<stdio.h>
    main(){
       int i=40;
       float a;
       //Implicit conversion
       a=i;
       printf("implicit value:%f</p><p>",a);
    }
    Copy after login

    Output

    Implicit value:40.000000
    Copy after login

    Example (explicit conversion)

    Real-time demonstration

    #include<stdio.h>
    main(){
       int i=40;
       short a;
       //Explicit conversion
       a=(short)i;
       printf("explicit value:%d</p><p>",a);
    }
    Copy after login

    Output

    Explicit value:40
    Copy after login

The above is the detailed content of What are implicit type conversions and explicit type conversions in C language?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!