In this article we will be going to cover Type of conversion in expression in C language so if you any doubt related to this topic then stay with this article and clear your doubt.
What is Conversion in C ?
Before we going to discuss implicit and explicit conversion in C we must be know what is conversion ? so, " When we change or convert form of an expression (in term of study) such process are known as conversion.
In C there are two type Conversions in Expressions
- Implicit Type Conversion
- Explicit Type Conversion
Implicit Type Conversion in C:
C permit mixing of constants and variables of different types in expression.C automatically converts any intermediate value to the proper type so that the expression can be evaluated without loosing any significance.This automatic conversion is know as implicit type conversion.
During evaluation it adheres to very strict rules of type conversion.if operands are of different types the 'lower' type is automatically converted to the 'higher' type of before the operation proceeds.The result is of type.
Given below is the sequence of rules that are applied while evaluating expressions
All short and char are automatically converted into int; then
- If one of the operands is long double,the other will be converted to long double and the result will be long double.
- else, if one of the operands is double, the other will be converted to double and the result will be double.
- else,if one of the operands is float, the other will be converted to float and the result will be float.
- else,if one of the operands is unsigned long int, the other will be converted to unsigned long int and result will be unsigned long int.
- else,if one of the operands is long int and the other is unsigned int,then
- if unsigned int can be converted to long int, the unsigned int operand will be converted as such and the result will be long int;
- else,both operands will be converted to unsigned long int and the result will be unsigned long int.
6.else,if one of the operands is long int, the other will be converted to long int and the result will be long int.
7.else,if one other operands is unsigned int,the other will be converted to unsigned int and the result will be unsigned int.
Explicit Type Conversion in C:
When we want to force a type conversion in a way that is different from the automatic conversion.Consider, for example, the calculation of ratio of female to male in town.
ratio=female_number/male_number
Since fenale_number and male_number are declared as integers in the program, the decimal part of the result of the division would be lost and ratio would represent wrong figure.This can be solved by converting locally one of the variables to the floating point as shown below:
ratio=(float) female_number/male_number
The operator (float) converts the female_number to floating point for the purpose of evaluation of the expression. Then using the rule of automatic conversion,the division is performed in floating point mode,thus retaining the fractional part of result.
Note that is no way does the operator (float) affect the value of the variable female number.And also,the type of female number remain as int in the other parts of the program.
The process of such a local conversion is know as explicit conversion or casting a value.The general form of 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.
Conclusion:
In this article we have learnt about implicit and explicit function in detail I hope you have no any doubt remaining after reading this article,but still if you have any doubt then feel free comment below.
