Type Conversion and Type Casting in C++(Day 10) – 2

Type Conversion and Type Casting in C++ 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.

C++ facilitates type conversion into 2 forms :

Type Conversion and Type Casting in C++

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 of Then resultant type of other operand Otherwise
1 long double long double Step 2
2 double double Step 3
3 float float Step 4
4 integral promotion takes place followed by step 5
5 unsigned long unsigned long Step 6
6 long 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
7 long long Step 8
8 unsigned unsigned Both 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 : 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.no Conversion Potential Problems
1 Double to float Loss of precision(significant figures)
2 Float to int Loss of fractional part
3 Long to int/short Loss 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-Endian, Let 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;
}
]]>

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top