- C++ Course Basics
- C++ Tutorial
- C++ Basic Syntax
- C++ Identifiers
- C++ Character Set
- C++ Input/Output Operator
- C++ Variables
- C++ Data Types
- C++ Formatting Output
- C++ Operators
- C++ Assignment Operator
- C++ Type Conversion
- C++ Program Control
- C++ if and if-else
- C++ switch
- C++ loops
- C++ break and continue
- C++ Functions
- C++ Functions
- C++ Prototype and Definition
- C++ Function Call
- C++ Function Types
- C++ Friend Function
- C++ Function Overloading
- C++ Arrays and Strings
- C++ Arrays
- C++ One-Dimensional Arrays
- C++ Strings
- C++ String Functions
- C++ Structures
- C++ Structures
- C++ Nested Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ Pointers
- C++ Pointers
- C++ Memory Map
- C++ Declare Initialize Pointers
- C++ Pointers and Structures
- C++ Object-Oriented
- C++ Object-Oriented
- C++ Classes and Objects
- C++ Constructors and Destructors
- C++ Objects as Function Arguments
- C++ Pointers and Objects
- C++ Data Structure
- C++ Linked List
- C++ Stack
- C++ Queues
- C++ File Handling
- C++ File Handling
- C++ Opening and Closing Files
- C++ Steps to Process Files
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Misc
- C++ typedef
- C++ #define
- C++ Date and Time
- C++ Examples
- C++ Examples
C++ Type Conversion and Type Casting
This post was published to describe the two topics of the C++ language, which are
So without any further delay, let's start with the "type conversion."
C++ Type Conversion
The process of converting one predefined type into another is called type conversion. When constants and variables of different types are mixed in an expression, they are converted to the same type. When variables of one type are mixed with variables of another type, a type conversion will occur. C++ facilitates type conversion into the following two forms:
- implicit type conversion
- explicit type conversion (type casting)
Implicit Type Conversion in C++
An implicit type conversion is a conversion performed by the compiler without the programmer's intervention. An implicit conversion is applied generally whenever differing data types are intermixed in an expression (mixed mode expression), so as not to lose information. The value of the right side (expression side) of the assignment is converted to the type of the left side (target variable).
Therefore, the types of the right side and left side of an assignment should be compatible so that type conversion can take place. The compatible data types are mathematical data types, i.e., char, int, float, and double. For example, the following statement:
ch = x ; (where ch is a char and x is an int)
Converts the value of x, i.e., an integer, to a bit in ch, a character. Assuming a word size of 16 bits (2 bytes), the integer variable x's leftmost higher-order bits are looped off, leaving ch with lower 8-bits.Say, if x had a value of 1417 (whose binary equivalent is 0000010110001001), the ch would have lower 8-bits, i.e., 10001001, resulting in a loss of information.
When converting from integers to characters and long integers to integers, the appropriate amount of high-order bits (depending on the target type's size) will be removed. In many environments, this means that 8 bits will be lost when going from an integer to a character, and 16 bits will be lost when going from a long integer to an integer.
The following table summarizes the assignment type conversions:
Target Type | Expression Type | Possible Info Loss |
---|---|---|
signed char | char | if value > 127, target is negative |
char | short int | High-order 8 bits |
char | int | High-order 8 bits |
char | long int | High-order 8 bits |
int | long int | High-order 16 bits |
int | float | Fractional part and possibly more |
float | double | Precision, result rounded |
double | long double | Precision, result rounded |
There is no information lost during the process of converting from a shorter type to a longer type, such as when moving from an int to a float or from a float to a double. It does not add any degree of precision or accuracy either. These kinds of conversions do not alter the value itself; all that happens is that the value is represented in a different format.
Explicit Type Conversion in C++ | C++ Type Casting
The process of explicitly converting an operand to a particular type is referred to as "type casting." An explicit type conversion is one that is defined by the user and compels an expression to have a particular type. Casting data from one type to another can be done using this general form in C++.
(type) expression
where type denotes a valid data type that can be used in C++ and to which the conversion will be made. For instance, the following notation can be utilized to guarantee that the expression (x + y/2) evaluates to the float data type:
(float) (x + y/2)
As can be seen from the preceding statement, the types that are produced on the right side are able to undergo an explicit transformation by utilizing the type casting operator (). One good illustration of this would be the fact that the resultant type of the expression "side" is double.
int i, j; float f, result; double d; result = (i/j) + (f/d) - (f+i);
If you want to convert the type of expression side to float in an explicit manner, you should do as follows:
result = (float) ( (i/j) + (f/d) - (f+i));
Many times, casts are referred to as operators. A cast is considered a unary operator, meaning that it follows the same order of precedence as any other unary operator. Here is an illustration of that:
#include<iostream> using namespace std; int main() { float res; float f1 = 15.5, f2 = 2; res = (int)f1 / (int)f2; cout<<res<<endl; res = (int)(f1/f2); cout<<res<<endl; res = f1/f2; cout<<res<<endl; return 0; }
Here is a sample run of the above C++ program:
7 7 7.75
« Previous Tutorial Next Tutorial »