# Type Conversion

Type Conversion is the important topic of the Computer Programming & technology. Moreover, Freestudy9 has all kind of important topic and information about the subject.

- When an operator has operands of different types, they are converted to a common type, this is known as type casting or type conversion.
- Typecasting is making a variable of one data type to act like another data type such as an int to float.

**Implicit Type Casting**

- C automatically converts any intermediate values to the proper type so that the expression can evaluate without losing any significance.
- This automatic conversion is known as implicit type conversion.
- The lower type automatically converted to the higher type before the operation proceeds. The result the higher type.
- Example:
- If one operand int and other floats then int will be converted to float.
- If one operand the float and other is long double then float will convert to the long double.

- This conversion happens from low data type to high data type so that information not lost. The conversion hierarchy shown below:

**Explicit Type Casting**

- Sometimes we want to force a type conversion in a way that is different from automatic conversion.
- The process of such a local conversion or casting known as explicit casting.
- The general form of the cast is (type-name) expression.
- Where type-name is one of the standard C data types. The expression may be constant, variable, or an expression.

**Example:**

#include<stdio.h>

#include<conio.h>

void main()

{

int sum=47, n=10;

float avg;

avg=sum / n;

printf(“Result of Implicit Type Casting: %f”, avg);

avg=(float)sum / (float)n;

printf(“Result of Explicit Type Casting: %f”, avg);

}

**Operator Precedence and Associativity**

- Precedence of an operator is its priority in an expression for evaluation.
- Operator precedence why the expression 5 + 3 * 2 calculated as 5 + (3 * 2), giving 11, and not as (5 + 3) * 2, giving 16.
- We say that the multiplication operator (*) has higher “precedence” or “priority” than the addition operator (+), so the multiplication must perform first.
- Moreover, Associativity the left-to-right or right-to-left order for grouping operands to operators that have the same precedence.
- Operator associativity is why the expression 8 – 3 – 2 calculated as (8 – 3) – 2, giving 3, and not as 8 – (3 – 2), giving 7.
- Also, We say that the subtraction operator (-) “left associative”, so the left subtraction must perform first. When we can’t decide by operator precedence alone in which order to calculate an expression, we must use associativity.

**Related Terms**

Computer Programming and Utilization, Constant Types, Primary data types, Engineering Study.

## Leave a Reply