0% found this document useful (0 votes)
293 views

Jayashree Type Conversion

Type conversion in C++ refers to converting one predefined type to another. There are two types of conversion: implicit and explicit. Implicit conversion is performed by the compiler without user intervention when different data types are mixed in an expression. Explicit conversion, also called type casting, is user-defined and forces an expression to a specific type. Problems can occur when converting between types if information is lost, like converting a double to a float which causes loss of precision.

Uploaded by

Boobalan R
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
293 views

Jayashree Type Conversion

Type conversion in C++ refers to converting one predefined type to another. There are two types of conversion: implicit and explicit. Implicit conversion is performed by the compiler without user intervention when different data types are mixed in an expression. Explicit conversion, also called type casting, is user-defined and forces an expression to a specific type. Problems can occur when converting between types if information is lost, like converting a double to a float which causes loss of precision.

Uploaded by

Boobalan R
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

TYPE CONVERSION

INTRODUCTION

Both Type conversions in C++ are used to convert one predefined type to anther type.
Type Conversion is the process of converting one predefined type into another typecasting is
the converting one predefined type into another type forcefully.

NEED OF TYPE CONVERSION IN C++

An Expression is composed of one or more operations and operands. Operands


consist 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 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


programmers 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.

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.

1
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.

Usual Arithmetic Conversions are summarized in the following table

StepNo If eitherstype 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 (i) long int (provided long int can Step 7
other is unsigned represent all values of unsigned int)
int
(ii) unsigned long int(if all values of Step 7
unsigned int cant be represented by long
int)

7 long long Step 8

8 unsigned unsigned Both


operandsare int

2
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

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

3
AIM

To cast the data type

ALGORTIHM

Start the program


Declare int variable
Divide 2 values and convert the output into integer format
4.assign the result to x and print it.

SOURCE CODE FOR TYPE CASTING

#include <stdio.h>

int main ()

int x;

x = (int) 7/5;

cout<<"x="<<x;

OUTPUT

CONCLUSION

A type conversion may either be explicit or implicit, depending on whether it is


ordered by the programmer or by the compiler. It is the process of converting one type into
another. In other words converting an expression of a given type into another is called type
casting. There are two ways of achieving the type conversion namely:

4
Automatic Conversion otherwise called as Implicit Conversion

Type casting otherwise called as Explicit Conversion

Explicit type conversions (casts) are used when a programmer want to get around the
compiler's typing system; for success in this endeavour, the programmer must use them
correctly. Problems which the compiler avoids may arise, such as if the processor demands
that data of a given type be located at certain addresses or if data is truncated because a data
type does not have the same size as the original type on a given platform. Explicit type
conversions between objects of different types lead, at best, to code that is difficult to read.

REFERENCES

https://www.doc.ic.ac.uk/lab/cplus/c++.rules/chap13.html
http://www.geeksforgeeks.org/g-fact-35/
http://www.edugrabs.com/type-conversion-and-type-casting-in-c/
http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/strongtyping.html
http://www.pvtuts.com/cpp/cpp-type-conversions

You might also like