Type Conversion in C:

Type conversion in C can be of following two types:

a) Automatic Type Conversion:

  • When an expression in C is evaluated , the resulting value has a particular data type .
    If all the operands in the expression are of same type, the resulting value also has the same type .

  • When operands in an expression are of different data type , they undergo type conversion or automatic type conversion before the expression takes on its final value .

  • The following rules apply when operands are of different data types and when neither operand is unsigned :
    1. If both operands are floating - point types (e.g. a float and a double) , the lower range operand will be converted to the data type of higher range operand and the result will be expressed in data type of higher range operand. Thus an operation between a float and a double will result in a double ; float and a long double will result in a long double ; a double and a long double will result in a long double.

    2. if one operand is a floating - point type ( e.g. float , double or long double ) and other is a char or an int ( including short int or long int ) , the char or int will be converted to the floating point type of the other operand and the result will be expressed as such . Hence , an operation between an int and a double will result in a double .

    3. If neither operand is floating-point type but one is long int , the other will be converted to long int and the result will be long int . Thus , an operation between a long int and an int will result in a long int.

    4. If neither operand is floating-point type or a long int , then both operands will be converted to int and the result will be int. Thus, an operation between a short int and a char will result in an int.
    NOTE : Automatic type conversion is temporary in nature.



    b) Type Casting or Forced Type Conversion:

    • The value of an expression or operand can be converted to a different data type forcibly if desired . To do so , the expression must be preceded by the name of the desired data type , enclosed in parentheses .

    • Hence , in general we can write ( data type ) expression . This is known as type casting or forced type conversion and ( data type ) is known as type operator or cast operator or simply cast .
    • If 'i' is an integer variable whose value is 7 and 'f' is a floating-point variable whose value is 8.5 , then the expression ( ( int ) ( i+f ) ) % 4 forces the operand ( i + f ) to be an integer and therefore the result of this operation is 3 . The explicit type specification applies only to the first operand , not the entire expression .

    • The data type of the expression ( i + f ) is not changed by a cast . Rather it is value of the expression that undergoes type conversion.

    • Consider the example ( ( int ) f ) % 2 where 'f' is a floating point variable of value 5.5 . This expression is perfectly valid and results in integer remainder 1 . The data type pf 'f' remains to be a float even though its value was converted to integer 5 when carrying out the remainder operation . After the execution of the expression the value of f remains to be 5.5 .

    
    #include<stdio.h>
    void main() {
    int a;
    float b=5.6667;
    a=(int)b;
    printf("%d",a);
    }
    
    

    output:

    
    5
    
    

    generally,It is preferred to cast datatypes from lower to higher to prevent any data loss.

    Device
    • Print Page