Programming in C, Implicit typecasting, Explicit typecasting

 Question 1: What is the use of Implicit typecasting?

Ans: Implicit typecasting is performed automatically by the compiler without programmer’s intervention. In this type of casting the compiler converts all operands into the data type of the largest operand. 

For example in the program given below, compiler converts the sum of two float numbers to an int.

    #include< stdio.h

    void main(void)

    {

            float value 1 = 2.5;

            float value 2 = 5.3;

            int result;

            result = value 1 + value 2;

            printf(“Result: %d”, result);

    }    

Question 2: What is the use of explicit typecasting?

Ans: Explicit typecasting is performed by the programmer. The programmer explicitly defines the data type in parenthesis before the variable or expression, as follows:

(type) expression.

For example in the program given below, the programmer converts the division of two int number to float.

        #include< stdio.h

        void main(void)

        {

                    int value1 = 30;

                    int value2 = 7; Float result;

                    result = (float)value1/value2;

                    printf(“Result: %f”, result);

        }

Comments

Popular posts from this blog

Conditional Control structure. Chapter 4, Lab Activities, Long questions.

Chapter # 5, Loop Control Structure, Lab Activities