Describe about type conversions

Estudies4you
Q22. Describe about type conversions.
Answer:

Type Conversion
Type conversion refers to the process of converting the data of one type into another type. For example, consider two integer variables namely x = 4 and y = 7. When a division operation is performed on them the result would be 0.57 which is a floating point number. Since x and y are declared as integers, the C++ compiler considers their result(x/y) also as integer by discarding the decimal value eventually. Hence, proper type conversion is necessary to ensure accurate results. There are certain basic rules that are to be followed while type conversion. ;
1. Unsigned char and unsigned short can be converted into unsigned integer.
2. Character and short can be converted into integer.
3. Float can be converted into double.
Basically, there are two types of type conversion supported in C++.
(i) Implicit conversion
(ii) Explicit conversion

(i) Implicit Type Conversion
In a binary expression, if two operands have different data types then the conversion of one type into another is implicitly done by the compiler. This conversion is referred as implicit type conversion.
The conversion is based on the priorities assigned to the operands. The operands with the priorities are listed below:
Operands
Priority
long double (real)
1
double (real)
2
float (real)
3
long long (integer)
4
long (integer)
5
int (integer)
6
short (integer)
7
char (character)
8
bool (boolean)
9
A simple assignment expression consists of two operands with an assignment operator placed in between them. A simple assignment expression is given below,
c=b;
Here,
Variable c is of character type and
Variable b is of boolean type.
Since, c and b are of different types, the data type of right variable is converted into the data type of left variable by performing either a promotion or demotion Operation by the compiler.

Promotion
Promotion operation widens the type of right expression to the type of left expression. After this promotion, both left and right expressions will have the same value.
Example
float f = 4.5;
int i = 2;
f = i; //The value of f is 2.0
Since, ‘f’ has the highest priority than i, the type of i is converted to type of f. Thus, the value of f is 2.0, which is the value of i stored in it.
Demotion
Demotion operation narrows the type of right expression to the type of left expression.
Example
int i= 42;
double d = 4.5360;
i=d; //The value of i is 4
Since, i has the lowest priority than d, the type of d is converted to type of i. Thus, the value of: is the truncated value of d.after demotion.

(ii) Explicit Type Conversion
The process in which data of one type is explicitly converted to another type is known as explicit type conversion. This conversion is also known as casting. It uses an operator ' called the unary cast operator. The casting is basically done by defining a new data type enclosed in parenthesis before the value that needs to be converted. For instance, if a character c’ is to be converted to an integer value then casting is done as follows,
(int) c;
In the above expression, the value of c is a character type value only but the value of the entire expression has a converted type value which is of integer type.
When one data is divided by another, the result may be a real number, which can be obtained by using type casting. For instance, if the percentage of a student is found without casting, then the result  obtained is just an integer value. But, if casting is used, then the result is a real number which is as follows,
percentage = (float)totalobtained/numberofsubjects;
In the above statement, the type of  "totalobtained" is explicitly converted to float type and then the type of “numbersofsubjects” is implicitly converted to float type. Thus, after division, a floating value is obtained that is assigned to “percentage”.
In some situations, if a smaller number is divided by a larger number then the result would be 0. For example, consider the expression.
(float) (x/15);
If x has an integer value 10 then no conversion is required for dividing 10 by 15. The result is just an integer value i.e., 0. This value is then explicitly converted to a floating type and thus, the float result is 0.0, which is obtained by casting any of the values in the following way,
(float) x/15;

Program
#include <iostream>
using namespace std;
int main()
int x = 107;
char y = ‘a’;
double p = 1.2;
x = x+y; //y implicitly converted to int. ASCII value of 'a' is 97
float z = x + 1.5; // x is implicitly converted to float
cout <<“x="<<x<<endl
cout <<“y="<<y<<endl
<<“z=”" <<z << endl;
int sum = (int)p + 1; // Explicit conversion from double to int
cout << “Sum = ” << sum;
return 0;
}

Output

To Top