C++ Type Conversion: Automatic and Explicit Value Transformation
What is C++ Type Conversion: Automatic and Explicit Value Transformation? When writing a C++ program, you may sometimes need to work with different data types together. For example, you might want to add an integer to a decimal number...
What are the differences between implicit type promotion and C++ explicit cast operators?
Explanation & In-Depth Details
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">double</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">number</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Integer: "</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Double: "</span> <span class="js-op"><<</span> <span class="js-id">result</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Integer</span><span class="js-op">:</span> <span class="js-num">10</span>
<span class="js-id">Double</span><span class="js-op">:</span> <span class="js-num">10</span></code></pre></div><p>In this program, an integer value is assigned to a <code>double</code> variable. C++ automatically converts the integer value into a floating-point value.</p><p>Although the output displays <code>10</code>, the value stored in <code>result</code> is <code>10.0</code>.</p><p><br></p><h2>Types of Type Conversion in C++</h2><p>C++ mainly supports two ways of converting data types:</p><ol start="1"><li><p>Implicit Type Conversion (Automatic Conversion)</p></li><li><p>Explicit Type Conversion (Type Casting)</p></li></ol><p>Let's understand both methods with examples.</p><h2>1. Implicit Type Conversion in C++ (Automatic Conversion)</h2><p>Implicit type conversion happens when the compiler automatically converts a value from one data type to another.</p><p>The programmer does not need to write a special conversion operator. The compiler handles the conversion according to C++ language rules.</p><p>This is also known as automatic type conversion.</p><h3>Syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">destination_data_type</span> <span class="js-id">variable</span> <span class="js-op">=</span> <span class="js-id">source_value</span><span class="js-op">;</span></code></pre></div><p>The compiler performs the required conversion when the types are compatible.</p><h3>Example: Converting int to double</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span>
<span class="js-id">double</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">number</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Integer Value: "</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Double Value: "</span> <span class="js-op"><<</span> <span class="js-id">result</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Integer</span> <span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">25</span>
<span class="js-id">Double</span> <span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">25</span></code></pre></div><h3>Explanation</h3><p>In this example:</p><ul><li><p><code>number</code> is an integer variable.</p></li><li><p><code>result</code> is a double variable.</p></li><li><p>The integer value is automatically converted into a double.</p></li><li><p>The original value remains unchanged.</p></li></ul><p>The compiler handles the conversion without requiring an explicit cast.</p><h3>Example: Converting int to float</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">marks</span> <span class="js-op">=</span> <span class="js-num">85</span><span class="js-op">;</span>
<span class="js-id">float</span> <span class="js-id">percentage</span> <span class="js-op">=</span> <span class="js-id">marks</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Marks: "</span> <span class="js-op"><<</span> <span class="js-id">marks</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Percentage: "</span> <span class="js-op"><<</span> <span class="js-id">percentage</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Marks</span><span class="js-op">:</span> <span class="js-num">85</span>
<span class="js-id">Percentage</span><span class="js-op">:</span> <span class="js-num">85</span></code></pre></div><p>Here, the integer value is automatically converted into a floating-point value.</p><h3>Example: Converting char to int</h3><p>A character can be converted into an integer representing its value in the implementation's execution character encoding.</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">char</span> <span class="js-id">letter</span> <span class="js-op">=</span> <span class="js-str">'A'</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-id">letter</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Character: "</span> <span class="js-op"><<</span> <span class="js-id">letter</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Character Code: "</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Typical ASCII Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Character</span><span class="js-op">:</span> <span class="js-id">A</span>
<span class="js-id">Character</span> <span class="js-id">Code</span><span class="js-op">:</span> <span class="js-num">65</span></code></pre></div><p>The numeric result depends on the character encoding used by the implementation. In ASCII-compatible systems, the character <code>A</code> has the value 65.</p><h3>When Does Automatic Type Conversion Happen?</h3><p>Automatic conversion commonly occurs in situations such as:</p><ul><li><p>Assigning one compatible data type to another.</p></li><li><p>Performing arithmetic operations with different numeric types.</p></li><li><p>Passing arguments to functions.</p></li><li><p>Returning values from functions.</p></li><li><p>Evaluating expressions that require compatible operand types.</p></li></ul><h3>Important: Automatic Conversion Can Cause Data Loss</h3><p>Not every automatic conversion preserves the original value.</p><p>For example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">double</span> <span class="js-id">price</span> <span class="js-op">=</span> <span class="js-num">99.75</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">price</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Original Value: "</span> <span class="js-op"><<</span> <span class="js-id">price</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Converted Value: "</span> <span class="js-op"><<</span> <span class="js-id">result</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Original</span> <span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">99.75</span>
<span class="js-id">Converted</span> <span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">99</span></code></pre></div><p>The decimal portion is removed when the floating-point value is converted to an integer. The conversion truncates toward zero; it does not round to the nearest integer.</p><p>Therefore, automatic conversion should not always be considered safe.</p><h2>2. Explicit Type Conversion in C++ (Type Casting)</h2><p>Explicit type conversion happens when the programmer specifically instructs the compiler to convert a value from one data type to another.</p><p>This process is also called type casting.</p><p>It is useful when you want to control how a conversion takes place.</p><h3>Syntax</h3><p>C++ supports several casting methods. A common form is:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">destination_data_type</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">static_cast</span><span class="js-op"><</span><span class="js-id">destination_data_type</span><span class="js-op">>(</span><span class="js-id">value</span><span class="js-op">);</span></code></pre></div><p>For example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">double</span> <span class="js-id">price</span> <span class="js-op">=</span> <span class="js-num">99.75</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">static_cast</span><span class="js-op"><</span><span class="js-id">int</span><span class="js-op">>(</span><span class="js-id">price</span><span class="js-op">);</span></code></pre></div><p>Here, the <code>double</code> value is explicitly converted into an <code>int</code>.</p><h3>Example: Converting double to int</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">double</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">45.89</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">static_cast</span><span class="js-op"><</span><span class="js-id">int</span><span class="js-op">>(</span><span class="js-id">number</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Original Value: "</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Converted Value: "</span> <span class="js-op"><<</span> <span class="js-id">result</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Original</span> <span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">45.89</span>
<span class="js-id">Converted</span> <span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">45</span></code></pre></div><h3>Explanation</h3><p>In this example:</p><ul><li><p><code>number</code> stores a decimal value.</p></li><li><p><code>static_cast<int>(number)</code> converts the value into an integer.</p></li><li><p>The decimal portion is discarded.</p></li><li><p>The converted value is stored in <code>result</code>.</p></li></ul><p>The original variable remains unchanged.</p><h3>Example: Converting int to char</h3><p>You can also convert an integer into a character using an explicit cast.</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">65</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">letter</span> <span class="js-op">=</span> <span class="js-id">static_cast</span><span class="js-op"><</span><span class="js-id">char</span><span class="js-op">>(</span><span class="js-id">number</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Number: "</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Character: "</span> <span class="js-op"><<</span> <span class="js-id">letter</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Typical ASCII Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">65</span>
<span class="js-id">Character</span><span class="js-op">:</span> <span class="js-id">A</span></code></pre></div><p>The character produced depends on the execution character encoding. The example gives <code>A</code> on ASCII-compatible systems.</p><h2>Different Types of Casting in C++</h2><p>C++ provides four named casting operators for different conversion requirements.</p><p>These are:</p><ol start="1"><li><p><code>static_cast</code></p></li><li><p><code>dynamic_cast</code></p></li><li><p><code>const_cast</code></p></li><li><p><code>reinterpret_cast</code></p></li></ol><p>Each casting operator has a different purpose.</p><h2>1. static_cast in C++</h2><p><code>static_cast</code> is the most commonly used named casting operator for ordinary conversions between compatible types.</p><p>It is useful for converting numeric values and performing certain conversions between related class and pointer types.</p><h3>Syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">static_cast</span><span class="js-op"><</span><span class="js-id">destination_type</span><span class="js-op">>(</span><span class="js-id">value</span><span class="js-op">);</span></code></pre></div><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">double</span> <span class="js-id">price</span> <span class="js-op">=</span> <span class="js-num">150.75</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">static_cast</span><span class="js-op"><</span><span class="js-id">int</span><span class="js-op">>(</span><span class="js-id">price</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Original Price: "</span> <span class="js-op"><<</span> <span class="js-id">price</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Converted Price: "</span> <span class="js-op"><<</span> <span class="js-id">result</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Original</span> <span class="js-id">Price</span><span class="js-op">:</span> <span class="js-num">150.75</span>
<span class="js-id">Converted</span> <span class="js-id">Price</span><span class="js-op">:</span> <span class="js-num">150</span></code></pre></div><h3>When should you use static_cast?</h3><p>Use <code>static_cast</code> when:</p><ul><li><p>Converting between compatible numeric types.</p></li><li><p>Performing certain conversions between related class types.</p></li><li><p>Making an intended conversion clear in your code.</p></li><li><p>Replacing traditional C-style casts in ordinary C++ code.</p></li></ul><p>It does not automatically make a conversion safe from data loss or invalid values.</p><h2>2. dynamic_cast in C++</h2><p><code>dynamic_cast</code> is mainly used when working with inheritance and polymorphism.</p><p>It allows you to safely check or convert pointers and references within a class hierarchy at runtime.</p><p>For a downcast from a base class to a derived class, the base class must be polymorphic, usually by having at least one virtual function.</p><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-kw">class</span> <span class="js-id">Animal</span> <span class="js-op">{</span>
<span class="js-id">public</span><span class="js-op">:</span>
<span class="js-id">virtual</span> <span class="js-kw">void</span> <span class="js-id">sound</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Animal sound"</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-id">virtual</span> <span class="js-op">~</span><span class="js-id">Animal</span><span class="js-op">()</span> <span class="js-op">=</span> <span class="js-kw">default</span><span class="js-op">;</span>
<span class="js-op">};</span>
<span class="js-kw">class</span> <span class="js-id">Dog</span> <span class="js-op">:</span> <span class="js-id">public</span> <span class="js-id">Animal</span> <span class="js-op">{</span>
<span class="js-id">public</span><span class="js-op">:</span>
<span class="js-kw">void</span> <span class="js-id">bark</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Dog is barking"</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-op">};</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">Animal</span><span class="js-op">*</span> <span class="js-id">animal</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">Dog</span><span class="js-op">();</span>
<span class="js-id">Dog</span><span class="js-op">*</span> <span class="js-id">dog</span> <span class="js-op">=</span> <span class="js-id">dynamic_cast</span><span class="js-op"><</span><span class="js-id">Dog</span><span class="js-op">*>(</span><span class="js-id">animal</span><span class="js-op">);</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">dog</span> <span class="js-op">!=</span> <span class="js-id">nullptr</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">dog</span><span class="js-op">-></span><span class="js-id">bark</span><span class="js-op">();</span>
<span class="js-op">}</span> <span class="js-kw">else</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Conversion failed"</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-id">delete</span> <span class="js-id">animal</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Dog</span> <span class="js-id">is</span> <span class="js-id">barking</span></code></pre></div><h3>Explanation</h3><p>In this example:</p><ul><li><p><code>Animal</code> is the base class.</p></li><li><p><code>Dog</code> is the derived class.</p></li><li><p>An <code>Animal</code> pointer refers to a <code>Dog</code> object.</p></li><li><p><code>dynamic_cast</code> checks whether the object can be converted to a <code>Dog</code> pointer.</p></li><li><p>The conversion succeeds because the actual object is a <code>Dog</code>.</p></li></ul><p>If a pointer conversion fails, <code>dynamic_cast</code> returns <code>nullptr</code>.</p><p>For a reference conversion that fails, it throws <code>std::bad_cast</code>.</p><h3>When should you use dynamic_cast?</h3><p>Use it when:</p><ul><li><p>Working with inheritance.</p></li><li><p>Checking the actual derived object type at runtime.</p></li><li><p>Performing safe downcasting in polymorphic class hierarchies.</p></li></ul><h2>3. const_cast in C++</h2><p><code>const_cast</code> is used to add or remove <code>const</code> or <code>volatile</code> qualifiers from a pointer or reference type.</p><p>It does not change the underlying data type.</p><p>It is sometimes useful when working with older APIs that do not accept const-qualified arguments.</p><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-kw">const</span> <span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">number</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">newPtr</span> <span class="js-op">=</span> <span class="js-id">const_cast</span><span class="js-op"><</span><span class="js-id">int</span><span class="js-op">*>(</span><span class="js-id">ptr</span><span class="js-op">);</span>
<span class="js-op">*</span><span class="js-id">newPtr</span> <span class="js-op">=</span> <span class="js-num">200</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Updated Value: "</span> <span class="js-op"><<</span> <span class="js-id">number</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Updated</span> <span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">200</span></code></pre></div><h3>Explanation</h3><p>In this example:</p><ul><li><p><code>ptr</code> is a pointer to a const-qualified integer.</p></li><li><p><code>const_cast</code> removes the const qualification from the pointer type.</p></li><li><p>The original object is a non-const integer.</p></li><li><p>Therefore, modifying it through <code>newPtr</code> is valid.</p></li></ul><p><strong>Important:</strong> Removing <code>const</code> from a pointer does not make an originally const object modifiable. Modifying an object that was originally declared const through a cast results in undefined behaviour.</p><h3>When should you use const_cast?</h3><p>Use it only when you have a specific reason to change const qualification, such as interacting with legacy interfaces.</p><p>Avoid using it to bypass const correctness.</p><h2>4. reinterpret_cast in C++</h2><p><code>reinterpret_cast</code> is a low-level casting operator used to reinterpret a value or pointer representation as another type.</p><p>It is commonly associated with low-level programming, system programming, and hardware-related code.</p><h3>Syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">reinterpret_cast</span><span class="js-op"><</span><span class="js-id">destination_type</span><span class="js-op">>(</span><span class="js-id">value</span><span class="js-op">);</span></code></pre></div><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">number</span><span class="js-op">;</span>
<span class="js-kw">void</span><span class="js-op">*</span> <span class="js-id">address</span> <span class="js-op">=</span> <span class="js-id">reinterpret_cast</span><span class="js-op"><</span><span class="js-kw">void</span><span class="js-op">*>(</span><span class="js-id">ptr</span><span class="js-op">);</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">newPtr</span> <span class="js-op">=</span> <span class="js-id">reinterpret_cast</span><span class="js-op"><</span><span class="js-id">int</span><span class="js-op">*>(</span><span class="js-id">address</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Value: "</span> <span class="js-op"><<</span> <span class="js-op">*</span><span class="js-id">newPtr</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">100</span></code></pre></div><h3>Explanation</h3><p>In this example:</p><ul><li><p><code>ptr</code> stores the address of an integer.</p></li><li><p>The address is converted into a <code>void*</code> pointer.</p></li><li><p>The pointer is converted back into an <code>int*</code>.</p></li><li><p>The original integer value can then be accessed.</p></li></ul><h3>When should you use reinterpret_cast?</h3><p>Use it only when low-level representation conversion is genuinely required.</p><p>It should be handled carefully because using a converted pointer incorrectly can result in undefined behaviour.</p><h2>Difference Between Implicit and Explicit Type Conversion</h2><p>Both methods convert values from one data type to another, but they work differently.</p><table><tbody><tr><th>Feature</th><th>Implicit Conversion</th><th>Explicit Conversion</th></tr><tr><td>Also called</td><td>Automatic conversion</td><td>Type casting</td></tr><tr><td>Performed by</td><td>Compiler</td><td>Programmer specifies the cast</td></tr><tr><td>Special syntax</td><td>Not required</td><td>Cast syntax required</td></tr><tr><td>Control</td><td>Less direct control</td><td>Conversion is explicitly requested</td></tr><tr><td>Example</td><td><code>double x = 10;</code></td><td><code>double x = static_cast<double>(10);</code></td></tr><tr><td>Main purpose</td><td>Convenient conversions</td><td>Clear, intentional conversions</td></tr></tbody></table><h3>Which Method Is Better?</h3><p>Neither method is always better.</p><ul><li><p>Use implicit conversion when the conversion is appropriate and its behaviour is understood.</p></li><li><p>Use <code>static_cast</code> when you want to make an ordinary conversion explicit.</p></li><li><p>Use <code>dynamic_cast</code> for runtime-checked conversions in polymorphic class hierarchies.</p></li><li><p>Use <code>const_cast</code> and <code>reinterpret_cast</code> only for specific advanced requirements.</p></li></ul><h2>Common Examples of Type Conversion in C++</h2><p>Here are some common conversions you may encounter while writing C++ programs.</p><table><tbody><tr><th>Conversion</th><th>Example</th><th>Result</th></tr><tr><td>int to double</td><td><code>double x = 10;</code></td><td>10.0</td></tr><tr><td>int to float</td><td><code>float x = 20;</code></td><td>20.0</td></tr><tr><td>double to int</td><td><code>static_cast<int>(10.75)</code></td><td>10</td></tr><tr><td>float to int</td><td><code>static_cast<int>(5.9f)</code></td><td>5</td></tr><tr><td>char to int</td><td><code>static_cast<int>('A')</code></td><td>Character code</td></tr><tr><td>int to char</td><td><code>static_cast<char>(65)</code></td><td>Encoding-dependent character</td></tr><tr><td>int to long</td><td><code>long x = 100;</code></td><td>100</td></tr><tr><td>int to unsigned</td><td><code>unsigned int x = 100;</code></td><td>100</td></tr></tbody></table><p>The results of character conversions depend on the character encoding, and numeric conversions can have range or precision limitations.</p><h2>Type Conversion During Arithmetic Operations</h2><p>C++ also performs type conversion automatically when you use different numeric types in an arithmetic expression.</p><p>For example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number1</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">double</span> <span class="js-id">number2</span> <span class="js-op">=</span> <span class="js-num">5.5</span><span class="js-op">;</span>
<span class="js-id">double</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">number1</span> <span class="js-op">+</span> <span class="js-id">number2</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Result: "</span> <span class="js-op"><<</span> <span class="js-id">result</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Result</span><span class="js-op">:</span> <span class="js-num">15.5</span></code></pre></div><h3>How does it work?</h3><p>In this example:</p><ol start="1"><li><p><code>number1</code> is an integer.</p></li><li><p><code>number2</code> is a double.</p></li><li><p>C++ converts the integer operand to a compatible floating-point type.</p></li><li><p>The addition is performed using floating-point arithmetic.</p></li><li><p>The result is stored in a double variable.</p></li></ol><p>This process is part of the usual arithmetic conversions in C++.</p><h2>Data Loss During Type Conversion</h2><p>One important thing to understand is that converting a value into another data type can sometimes cause data loss.</p><p>This usually happens when converting from a type with a wider range or greater precision into a narrower type.</p><h3>Example: Double to Int</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">double</span> <span class="js-id">value</span> <span class="js-op">=</span> <span class="js-num">125.89</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">static_cast</span><span class="js-op"><</span><span class="js-id">int</span><span class="js-op">>(</span><span class="js-id">value</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Original Value: "</span> <span class="js-op"><<</span> <span class="js-id">value</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Converted Value: "</span> <span class="js-op"><<</span> <span class="js-id">result</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Original</span> <span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">125.89</span>
<span class="js-id">Converted</span> <span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">125</span></code></pre></div><p>The decimal portion is discarded.</p><h3>Example: Large Integer to Small Integer</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">300</span><span class="js-op">;</span>
<span class="js-id">unsigned</span> <span class="js-id">char</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">static_cast</span><span class="js-op"><</span><span class="js-id">unsigned</span> <span class="js-id">char</span><span class="js-op">>(</span><span class="js-id">number</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Converted Value: "</span>
<span class="js-op"><<</span> <span class="js-id">static_cast</span><span class="js-op"><</span><span class="js-id">int</span><span class="js-op">>(</span><span class="js-id">result</span><span class="js-op">);</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Typical Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Converted</span> <span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">44</span></code></pre></div><p>On systems where <code>unsigned char</code> has 8 bits, the conversion wraps the value modulo 256.</p><p>This example demonstrates why you should always check the range of a destination data type before converting a value.</p><h2>Complete C++ Program Using Automatic and Explicit Conversion</h2><p>Now let's look at a complete program that demonstrates both types of conversion.</p><h3>C++ Code</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-cm">// Automatic type conversion</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">50</span><span class="js-op">;</span>
<span class="js-id">double</span> <span class="js-id">automaticResult</span> <span class="js-op">=</span> <span class="js-id">number</span><span class="js-op">;</span>
<span class="js-cm">// Explicit type conversion</span>
<span class="js-id">double</span> <span class="js-id">price</span> <span class="js-op">=</span> <span class="js-num">99.75</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">explicitResult</span> <span class="js-op">=</span> <span class="js-id">static_cast</span><span class="js-op"><</span><span class="js-id">int</span><span class="js-op">>(</span><span class="js-id">price</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Automatic Conversion: "</span>
<span class="js-op"><<</span> <span class="js-id">automaticResult</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Explicit Conversion: "</span>
<span class="js-op"><<</span> <span class="js-id">explicitResult</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><h3>Output</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Automatic</span> <span class="js-id">Conversion</span><span class="js-op">:</span> <span class="js-num">50</span>
<span class="js-id">Explicit</span> <span class="js-id">Conversion</span><span class="js-op">:</span> <span class="js-num">99</span></code></pre></div><h3>Explanation</h3><p>This program demonstrates two different conversion methods.</p><p><strong>Automatic conversion:</strong></p><p>The integer value <code>50</code> is automatically converted into a double.</p><p><strong>Explicit conversion:</strong></p><p>The double value <code>99.75</code> is converted into an integer using <code>static_cast<int>()</code>. Its decimal portion is discarded.</p><p>Both methods are useful in different programming situations.</p><h2>Common Mistakes When Using Type Conversion in C++</h2><p>Beginners sometimes make mistakes when converting between data types. Understanding these mistakes can help you avoid unexpected results.</p><h3>1. Losing Decimal Values</h3><p>Incorrect assumption:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">double</span> <span class="js-id">price</span> <span class="js-op">=</span> <span class="js-num">99.99</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">price</span><span class="js-op">;</span></code></pre></div><p>The result is 99, not 100.</p><p>If you need rounding, use an appropriate function such as <code>std::round()</code> from the <code><cmath></code> header.</p><h3>2. Converting Values Outside the Destination Range</h3><p>A destination type may not be able to represent the converted value.</p><p>Always check the destination type's supported range before converting.</p><h3>3. Confusing Casting with Rounding</h3><p>Casting a floating-point value to an integer truncates the fractional portion toward zero. It does not round the value.</p><p>For example, converting <code>-5.8</code> to <code>int</code> produces <code>-5</code>.</p><h3>4. Using C-Style Casting Everywhere</h3><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-op">(</span><span class="js-id">int</span><span class="js-op">)</span><span class="js-num">10.75</span><span class="js-op">;</span></code></pre></div><p>Although this syntax is supported, modern C++ generally prefers named casts such as:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">static_cast</span><span class="js-op"><</span><span class="js-id">int</span><span class="js-op">>(</span><span class="js-num">10.75</span><span class="js-op">);</span></code></pre></div><p>Named casts make the intention clearer.</p><h3>5. Using reinterpret_cast Without Understanding It</h3><p><code>reinterpret_cast</code> is not a general-purpose conversion tool. Incorrect pointer conversions and memory access can lead to undefined behaviour.</p><p>Use it only when you understand the underlying representation and object lifetime requirements.</p><h2>Best Practices for Type Conversion in C++</h2><p>Follow these practices when working with different data types:</p><ul><li><p>Use implicit conversion when the conversion is appropriate and does not create unexpected results.</p></li><li><p>Prefer <code>static_cast</code> for ordinary explicit conversions.</p></li><li><p>Use <code>dynamic_cast</code> for runtime-checked downcasting in polymorphic class hierarchies.</p></li><li><p>Avoid unnecessary C-style casts.</p></li><li><p>Check destination data type ranges before converting values.</p></li><li><p>Use rounding functions when you need rounded results instead of truncation.</p></li><li><p>Be careful when converting between signed and unsigned integer types.</p></li><li><p>Avoid unnecessary <code>reinterpret_cast</code> operations.</p></li><li><p>Use clear variable names to make conversions easier to understand.</p></li></ul><h2>Frequently Asked Questions About C++ Type Conversion</h2><h3>1. What is type conversion in C++?</h3><p>Type conversion is the process of changing a value from one data type to another. For example, converting an integer into a double is a type conversion.</p><h3>2. What are the two main types of type conversion in C++?</h3><p>The two main types are implicit conversion and explicit conversion. Implicit conversion is performed automatically by the compiler, while explicit conversion is requested by the programmer.</p><h3>3. What is implicit type conversion in C++?</h3><p>Implicit type conversion is an automatic conversion performed by the compiler when a value is used in a context that requires another compatible type.</p><h3>4. What is explicit type conversion in C++?</h3><p>Explicit type conversion happens when a programmer uses a cast to request conversion from one type to another.</p><h3>5. What is type casting in C++?</h3><p>Type casting is the process of explicitly converting a value or expression into another type using a casting operation.</p><h3>6. What is static_cast in C++?</h3><p><code>static_cast</code> is a named casting operator used for many ordinary conversions between compatible types. It is generally the preferred choice for explicit numeric conversions.</p><h3>7. What is dynamic_cast in C++?</h3><p><code>dynamic_cast</code> performs runtime-checked conversions within polymorphic class hierarchies. It is commonly used for safe downcasting.</p><h3>8. What is const_cast in C++?</h3><p><code>const_cast</code> adds or removes const or volatile qualification from a pointer or reference type. It does not make an originally const object safe to modify.</p><h3>9. What is reinterpret_cast in C++?</h3><p><code>reinterpret_cast</code> performs certain low-level conversions involving pointer types and other supported representations. It should be used carefully.</p><h3>10. Does type conversion cause data loss?</h3><p>Sometimes. For example, converting <code>10.75</code> from double to int produces <code>10</code>, so the fractional portion is lost.</p><h3>11. What is the difference between type conversion and type casting?</h3><p>Type conversion is the general process of changing one data type into another. Type casting usually refers to explicitly requesting that conversion through a cast operator.</p><h3>12. Which type casting method is recommended in modern C++?</h3><p>For most ordinary explicit conversions, <code>static_cast</code> is recommended. Use the other named casts only when their specific functionality is required.</p>
Code Example
#include <iostream>
class Base {
public:
virtual ~Base() = default; // Polymorphic base
};
class Derived : public Base {
public:
void printDerived() { std::cout << "Derived instance active!
"; }
};
int main() {
// 1. static_cast (numeric conversion)
double preciseValue = 45.78;
int roundedValue = static_cast<int>(preciseValue);
std::cout << "static_cast result: " << roundedValue << "
";
// 2. dynamic_cast (polymorphic runtime verification)
Base* basePtr = new Derived();
Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);
if (derivedPtr) {
derivedPtr->printDerived();
}
delete basePtr;
return 0;
}Interview Question & Tips
A: `dynamic_cast` returns `nullptr` for failed pointer conversions. When casting reference types (`Derived&`), a failed downcast throws a `std::bad_cast` exception.
Q: When is reinterpret_cast legitimately used?
A: `reinterpret_cast` is used in systems programming, device drivers, network packet parsing, and raw byte serialization where memory buffers must be viewed as specific struct layouts.
Best Practices
2. Prefer `static_cast` for standard numeric and pointer upcasts.
3. Check `dynamic_cast` pointer results against `nullptr` before dereferencing.
This study reference for C++ Type Conversion: Automatic and Explicit Value Transformation is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation: