type conversion

Both Type conversion and Type casting in C++ are used to convert one predefined type to anther type.
Type Conversion is the process of converting one predefined type into another type.
and type Casting is the converting one predefined type into another type forcefully.

Need of Type Conversion and Type Casting in C++

An Expression is composed of one or more operations and operands. Operands consists of constants and variables. Constants and expressions of different types are mixed together in an expression. so they are converted to same type or says that a conversion is necessary to convert different types into same type.

Types of Type Conversions in C++

C++ facilitates type conversion into 2 forms :
  • Implicit Type Conversion
  • Explicit Type Conversion

Implicit Type Conversions : 

Implicit Type Conversion is the conversion performed by the compiler without programmer’s intervention.
It is applied, whenever, different data types are intermixed in an expression, so as not to loose information.
The C++ compiler converts all operands upto the type of the largest operand, which is called type promotion. 
Usual Arithmetic Conversions are summarized in the following table – 
StepNo.If either’stype ofThen resultant type of other operandOtherwise
1long doublelong doubleStep 2
2doubledoubleStep 3
3floatfloatStep 4
4integral promotion takes place followed by step 5
5unsigned longunsigned longStep 6
6long int ant the other is unsigned int(i) long int (provided long int can represent all values of unsigned int)Step 7
(ii) unsigned long int(if all values of unsigned int can’t be represented by long int)Step 7
7longlongStep 8
8unsignedunsignedBoth operandsare int
The step 1 and 2 in the above table will be read as –
Step 1: If either operand is of type long double, the other is converted to long double.
Step2 : 
Otherwise, if either is of type double, the other is converted to double.
After applying above arithmetic conversions, each pair f operands is of same type and the result of each operation is the same as the type of both operands.

Example of Implicit Type Conversion :

Type Conversion and Type Casting in C++

Explicit Type Conversion :

Explicit Type conversion is also called type casting. It is the conversion of one operand to a specific type. An explicit conversion is a user defined that forces an expression to be of specific type.
Syntax :    (type) expression
Example :    float(a+b/5) ; This expression evaluates to type float.

Problem in Explicit Type Conversion :

Assigning a value of smaller data type to a larger data type, may not pose any problem.
But, assigning a value of larger data type to smaller type, may poses problems. The problem is that assigning to a smaller data type may loose information, or result in losing some precision.

Conversion Problems – 

S.noConversionPotential Problems
1Double to floatLoss of precision(significant figures)
2Float to intLoss of fractional part
3Long to int/shortLoss of Information as original valuemay be out of range for target type

Type Compatibility –

In an assignment statement, the types of right types and left side of an assignment should be compatible, so that conversion can take place. For example,
ch=x; (where ch is of char data type and x is of integer data type)

How and Why Information is loose ?

what is Big Endian ??   ⇒ refer to the link Click here
since the memory representation in Big-EndianLet
int x=1417;
ch=x;
now, x will be 00000101 10001001 in binary.
type conversion and type casting in c
So, the information i.e. the value stores at address 1001, will be lost after assigning an integer variable value to a character variable

Example of C++ Program for Type Casting –

#include <stdio.h>
int main ()
{
    float x;
    x = (float) 7/5;
    cout<<"x="<<x;
}