Implicit and Explicit

Could not understand properly

Hello @ANCODE,

A typecast is basically a conversion from one type to another. There are two types of type conversion:

  1. Implicit Type Conversion Also known as ‘automatic type conversion’.
  • Done by the compiler on its own, without any external trigger from the user.

  • Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid loss of data.

  • All the data types of the variables are upgraded to the data type of the variable with the largest data type.
    bool -> char -> short int -> int ->
    unsigned int -> long -> unsigned ->
    long long -> float -> double -> long double
    It is possible for implicit conversions to lose i

  • information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long is implicitly converted to float).

    Example:
    int x = 10; // integer x
    char y = ‘a’; // character c

    // y implicitly converted to int. ASCII
    // value of ‘a’ is 97
    x = x + y;

    // x is implicitly converted to float
    float z = x + 1.0;

    cout << "x = " << x << endl
    << "y = " << y << endl
    << "z = " << z << endl;

    Output:
    x = 107
    y = a
    z = 108

  1. Explicit Type Conversion: This process is also called type casting and it is user-defined. Here the user can typecast the result to make it of a particular data type.
    Converting by assignment: This is done by explicitly defining the required type in front of the expression in parenthesis. This can be also considered as forceful casting.
    Syntax:

    (type) expression
    where type indicates the data type to which the final result is converted.rocess is also called type casting and it is user-defined. Here the user can typecast the result to make it of a particular data type.

Hope, this would help.
Give a like, if you are satisfied.