Type Conversion in C++13 Dec 2025 | 5 min read In this topic, we will discuss the conversion of one data type into another in the C++ programming language. Type conversion is the process that converts the predefined data type of one variable into an appropriate data type. The main idea behind type conversion is to convert two different data type variables into a single data type to solve mathematical and logical expressions easily without any data loss. ![]() For example, we are adding two numbers, where one variable is of int type and another of float type; we need to convert or typecast the int variable into a float to make them both float data types to add them. Type conversion can be done in two ways in C++, one is implicit type conversion, and the second is explicit type conversion. Those conversions are done by the compiler itself, called the implicit type or automatic type conversion. The conversion, which is done by the user or requires user interferences called the explicit or user define type conversion. Let's discuss the implicit and explicit type conversion in C++. Implicit Type ConversionThe implicit type conversion is the type of conversion done automatically by the compiler without any human effort. It means an implicit conversion automatically converts one data type into another type based on some predefined rules of the C++ compiler. Hence, it is also known as the automatic type conversion. For example: In the above example, there are two different data type variables, x, and y, where x is an int type, and the y is of short int data type. And the resultant variable z is also an integer type that stores x and y variables. But the C++ compiler automatically converts the lower rank data type (short int) value into higher type (int) before resulting the sum of two numbers. Thus, it avoids the data loss, overflow, or sign loss in implicit type conversion of C++. Order of the typecast in implicit conversionThe following is the correct order of data types from lower rank to higher rank: Program to convert int to float type using implicit type conversionLet's create a program to convert smaller rank data types into higher types using implicit type conversion. Program1.cppOutput The value of num1 is: 25 The value of num2 is: 25 Program to convert double to int data type using implicit type conversionLet's create a program to convert the higher data type into lower type using implicit type conversion. Program2.cppOutput The value of the int variable is: 15 The value of the double variable is: 15.25 In the above program, we have declared num as an integer type and num2 as the double data type variable and then assigned num2 as 15.25. After this, we assign num2 value to num variable using the assignment operator. So, a C++ compiler automatically converts the double data value to the integer type before assigning it to the num variable and print the truncate value as 15. Explicit type conversionConversions that require user intervention to change the data type of one variable to another, is called the explicit type conversion. In other words, an explicit conversion allows the programmer to manually changes or typecasts the data type from one variable to another type. Hence, it is also known as typecasting. Generally, we force the explicit type conversion to convert data from one type to another because it does not follow the implicit conversion rule. The explicit type conversion is divided into two ways:
Program to convert float value into int type using the cast operatorCast operator: In C++ language, a cast operator is a unary operator who forcefully converts one type into another type. Let's consider an example to convert the float data type into int type using the cast operator of the explicit conversion in C++ language. Program3.cpp Output The value of x is: 6 Program to convert one data type into another using the assignment operatorLet's consider an example to convert the data type of one variable into another using the assignment operator in the C++ program. Program4.cpp Output The value of int num1 is: 25 The value of float num2 is: 25.0 Next TopicKasais-algorithm-in-cpp |
Different Ways to Initialize an unordered_set in C++
Introduction In C++, std::unordered_set serves as a flexible container that provides a hash-based approach for storing distinct items. Std::unordered_set is incapable of imposing an order on the components it contains, in contrast to std::set, which keeps its elements sorted. Rather, this technique makes use of a...
4 min read
Difference Between DWORD and Unsigned Int in C++
Introduction General knowledge and interpretation of data types of C++ is very important in order to organize the data and create system-level programs. Two frequently observed types include 'DWORD' and 'unsigned int'. 'DWORD' is a Windows API data type that has the meaning of 'double-word' and...
8 min read
std::quoted in C++
Introduction The std::quoted is a flexible and efficient I/O manipulator designed in C++, which makes quoting of strings in input and output streams very easy. It is especially helpful when working with strings containing a space character or other special characters that can interfere with subsequent parsing...
10 min read
Sieve of Sundaram to print all primes smaller than n in C++
Prime numbers play a central role in various fields, from number theory and cryptography to computer science and engineering. Efficiently generating prime numbers up to a given limit is a classic problem that has been tackled using different algorithms. Among these, the Sieve of Sundaram...
13 min read
Jaccard Similarity Coefficient in C++
In this article, we will discuss the with various examples, advantages, and disadvantages. Jaccard Similarity: When two objects such as two documents of text are compared, a popular similarity measure called Jaccard Similarity is used to check their similarity The Jaccard similarity tool can be used to...
4 min read
ChaCha20 Stream Cipher in C++
Introduction Data confidentiality in applications demanding speed and flexibility is assured by stream ciphers which are one of the essential features in modern cryptography. The ChaCha20 stream cipher is one of the most favoured algorithms in this field. The creator of this cipher, Daniel J. Bernstein,...
15 min read
Serial Port Connection in C++
Connecting to a serial port in C++ is a common requirement for applications involving hardware communication, such as interfacing with sensors, modems, or embedded systems. Serial communication allows data to be transmitted one bit at a time over a communication channel, making it ideal for simple,...
10 min read
Delannoy Number in C++
Delannoy number is a mathematical term that refers to the number of paths from the point (0,0) to (m,n) on a grid where there are three types of movements: right, up, and diagonally (right-up). The sequence appears ubiquitously in combinatorial mathematics, lattice path counting, and...
4 min read
Bertrand's Postulate in C++
In this article, we are going to look at Bertrand's postulate with its examples in C++. What is the Bertrand's Postulate? Joseph Bertrand, a French mathematician, Baptized Bertrand's Postulate an important mathematical theory that resulted in the name of Bertrand. Bertrand first stated the theorem- the British mathematician...
6 min read
C++ Program for dot product and cross product of two Vectors
The task is to determine the cross product and dot product of the two provided vector arrays. Let's imagine we have two vectors, vector A and vector B, each comprising x, y, and directions. In this article, we will discuss a C++ program to find the...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
