C Type Casting

  • Type casting is a concept of converting one data type values into another datatype.
  • Type casting can be performed by using type operator.
  • In C programming language type casting is classified into two types

Implicit Type Casting
Explicit Type Casting

Syntax:

datatype1 variable1=value;
datatype2 variable2;
variable2=(datatype)variable;

Example:

float f=21.49;
int i;
i=(int)f;

Implicit Type Casting:

  • It is a concept of converting lower data type values into higher data type.
  • Implicit typecasting is under control of compiler, so has a programmer no need to be considered about implicit type casting process.
  • Example:

    void main()
    {
    int i=12345;
    long int l;
    l=(long)i;
    printf(“%d%ld”,i,l);
    }

    Explicit Type Casting:

    • It is a process of converting higher data type value into lower data type.
    • Whenever the explicit type casting is occurred mandatory to handling or else data overflow will occur.

    Example:

    void main()
    {
    double d=12345.6789;
    long int l;
    l=(long)d;
    printf(“%d%lf”,l,d);
    }

    Powered by k2schools