Type Modifiers in C++: short, long, signed, and unsigned
What is Type Modifiers in C++: short, long, signed, and unsigned? When learning C++, understanding data types is one of the first important steps. Every variable in a C++ program needs a data type that tells the compiler what...
How do type modifiers alter integer storage capacity and value ranges in C++?
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">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span>
<span class="js-id">unsigned</span> <span class="js-id">int</span> <span class="js-id">students</span> <span class="js-op">=</span> <span class="js-num">150</span><span class="js-op">;</span>
<span class="js-id">long</span> <span class="js-id">long</span> <span class="js-id">int</span> <span class="js-id">population</span> <span class="js-op">=</span> 8000000000LL<span class="js-op">;</span></code></pre></div><p>In this example:</p><ul><li><p><code>int</code> stores a normal integer.</p></li><li><p><code>unsigned int</code> stores a non-negative integer.</p></li><li><p><code>long long int</code> can store much larger integer values.</p></li></ul><p>Type modifiers help you choose a suitable data type for your program.</p><img src="https://www.operatetools.com/uploads/tutorial-images/c-type-modifiers-infographic-1794495797-DCmarL.png" alt="Tutorial illustration" class="editor-draggable-img" draggable="true" style="max-width: 100%; height: auto; border-radius: 8px; display: block; margin: 14px auto; cursor: grab;"><p><br></p><h2>Types of Type Modifiers in C++</h2><p>C++ has four main integer type modifiers:</p><table><tbody><tr><th>Type modifier</th><th>Main purpose</th></tr><tr><td><code>short</code></td><td>Provides a smaller integer range than <code>int</code></td></tr><tr><td><code>long</code></td><td>Provides a larger range than <code>int</code> on some systems</td></tr><tr><td><code>signed</code></td><td>Allows positive and negative integer values</td></tr><tr><td><code>unsigned</code></td><td>Allows only zero and positive integer values</td></tr></tbody></table><p>You can combine some of these modifiers with basic data types such as <code>int</code> and <code>char</code>.</p><p>Let's understand each one with simple examples.</p><h2>1. short Modifier in C++</h2><p>The <code>short</code> keyword is used to declare a short integer variable.</p><p>It provides an integer type with a minimum width of 16 bits. It is useful when your program needs to store relatively small integer values.</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">short</span> <span class="js-id">int</span> <span class="js-id">variable_name</span><span class="js-op">;</span></code></pre></div><p>You can also write <code>short</code> without specifying <code>int</code>.</p><h3>Example: Using short in C++</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">short</span> <span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span>
<span class="js-id">short</span> <span class="js-id">marks</span> <span class="js-op">=</span> <span class="js-num">450</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Age: "</span> <span class="js-op"><<</span> <span class="js-id">age</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">"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-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">Age</span><span class="js-op">:</span> <span class="js-num">25</span>
<span class="js-id">Marks</span><span class="js-op">:</span> <span class="js-num">450</span></code></pre></div><h3>How does short work?</h3><p>The <code>short</code> modifier provides a type that is at least 16 bits wide.</p><p>On many common platforms, a signed <code>short</code> uses 2 bytes and can store values from -32,768 to 32,767.</p><p>However, the exact size and range depend on the C++ implementation.</p><h3>When should you use short?</h3><p>You can consider using <code>short</code> when:</p><ul><li><p>You are storing relatively small integer values.</p></li><li><p>You are working with compact data structures.</p></li><li><p>You are handling data formats that specifically require a 16-bit integer.</p></li><li><p>You need to match a particular data representation.</p></li></ul><p>Remember that using <code>short</code> does not automatically make a program faster. Modern compilers and processors often handle ordinary <code>int</code> values very efficiently.</p><h2>2. long Modifier in C++</h2><p>The <code>long</code> keyword is used to declare an integer type with a range that is at least as large as the range of <code>int</code>.</p><p>It is useful when you need to store larger integer values.</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">long</span> <span class="js-id">int</span> <span class="js-id">variable_name</span><span class="js-op">;</span></code></pre></div><p>You can also use:</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">long</span> <span class="js-id">variable_name</span><span class="js-op">;</span></code></pre></div><p>Both declarations represent the same type.</p><h3>Example: Using long in C++</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">long</span> <span class="js-id">int</span> <span class="js-id">population</span> <span class="js-op">=</span> <span class="js-num">5000000</span><span class="js-op">;</span>
<span class="js-id">long</span> <span class="js-id">salary</span> <span class="js-op">=</span> <span class="js-num">750000</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Population: "</span> <span class="js-op"><<</span> <span class="js-id">population</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">"Salary: "</span> <span class="js-op"><<</span> <span class="js-id">salary</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">Population</span><span class="js-op">:</span> <span class="js-num">5000000</span>
<span class="js-id">Salary</span><span class="js-op">:</span> <span class="js-num">750000</span></code></pre></div><h3>What is long long in C++?</h3><p>C++ also supports <code>long long int</code>, which guarantees a width of at least 64 bits.</p><p>It is particularly useful when working with very large integer values.</p><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">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">long</span> <span class="js-id">long</span> <span class="js-id">int</span> <span class="js-id">population</span> <span class="js-op">=</span> 8000000000LL<span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Population: "</span> <span class="js-op"><<</span> <span class="js-id">population</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">Population</span><span class="js-op">:</span> <span class="js-num">8000000000</span></code></pre></div><p>The <code>LL</code> suffix tells the compiler that the integer literal has the <code>long long</code> type.</p><h3>When should you use long?</h3><p>Use <code>long</code> or <code>long long</code> when:</p><ul><li><p>You need to store large integer values.</p></li><li><p>You are calculating population figures.</p></li><li><p>You are working with large financial counters or transaction counts.</p></li><li><p>Your program requires a wider integer type.</p></li></ul><p><strong>Important:</strong> The size of <code>long</code> is platform-dependent. It is commonly 4 bytes on Windows and 8 bytes on many 64-bit Linux systems. If you specifically need at least 64 bits, use <code>long long</code> or <code>std::int64_t</code> when available.</p><h2>3. signed Modifier in C++</h2><p>The <code>signed</code> keyword allows an integer variable to store both positive and negative values, including zero.</p><p>In C++, ordinary <code>int</code> is already signed by default. Therefore, <code>signed int</code> and <code>int</code> represent the same type.</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">signed</span> <span class="js-id">int</span> <span class="js-id">variable_name</span><span class="js-op">;</span></code></pre></div><h3>Example: Using signed in C++</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">signed</span> <span class="js-id">int</span> <span class="js-id">temperature</span> <span class="js-op">=</span> <span class="js-op">-</span><span class="js-num">15</span><span class="js-op">;</span>
<span class="js-id">signed</span> <span class="js-id">int</span> <span class="js-id">balance</span> <span class="js-op">=</span> <span class="js-num">5000</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Temperature: "</span> <span class="js-op"><<</span> <span class="js-id">temperature</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">"Balance: "</span> <span class="js-op"><<</span> <span class="js-id">balance</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">Temperature</span><span class="js-op">:</span> <span class="js-op">-</span><span class="js-num">15</span>
<span class="js-id">Balance</span><span class="js-op">:</span> <span class="js-num">5000</span></code></pre></div><h3>How does signed work?</h3><p>A signed integer can represent:</p><ul><li><p>Negative numbers</p></li><li><p>Zero</p></li><li><p>Positive numbers</p></li></ul><p>For example, a signed 32-bit integer typically supports values from -2,147,483,648 to 2,147,483,647.</p><p>The exact range is determined by the type's implementation.</p><h3>When should you use signed?</h3><p>Use signed integer types when your program needs to handle:</p><ul><li><p>Temperature values above and below zero.</p></li><li><p>Profit and loss calculations.</p></li><li><p>Changes in account balances.</p></li><li><p>Differences between two numbers.</p></li><li><p>Positive and negative measurements.</p></li></ul><h2>4. unsigned Modifier in C++</h2><p>The <code>unsigned</code> keyword is used when you want an integer variable to store only non-negative values.</p><p>An unsigned integer cannot represent negative numbers.</p><p>Because it does not need to represent negative values, its available range is used for zero and positive numbers.</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">unsigned</span> <span class="js-id">int</span> <span class="js-id">variable_name</span><span class="js-op">;</span></code></pre></div><p>You can also write:</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">unsigned</span> <span class="js-id">variable_name</span><span class="js-op">;</span></code></pre></div><h3>Example: Using unsigned in C++</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">unsigned</span> <span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span>
<span class="js-id">unsigned</span> <span class="js-id">int</span> <span class="js-id">students</span> <span class="js-op">=</span> <span class="js-num">150</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Age: "</span> <span class="js-op"><<</span> <span class="js-id">age</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">"Students: "</span> <span class="js-op"><<</span> <span class="js-id">students</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">Age</span><span class="js-op">:</span> <span class="js-num">25</span>
<span class="js-id">Students</span><span class="js-op">:</span> <span class="js-num">150</span></code></pre></div><h3>How does unsigned work?</h3><p>An unsigned integer stores values starting from zero.</p><p>For example, a typical 32-bit unsigned integer has a range from 0 to 4,294,967,295.</p><p>Compare the ranges:</p><table><tbody><tr><th>Data type</th><th>Typical range</th></tr><tr><td><code>signed int</code></td><td>-2,147,483,648 to 2,147,483,647</td></tr><tr><td><code>unsigned int</code></td><td>0 to 4,294,967,295</td></tr></tbody></table><p>Both types typically occupy 4 bytes on common systems, but their value ranges are different.</p><h3>When should you use unsigned?</h3><p>Unsigned integers are useful for:</p><ul><li><p>Storing counts that cannot be negative.</p></li><li><p>Representing bit patterns.</p></li><li><p>Working with bitwise operations.</p></li><li><p>Handling values defined as unsigned by an external data format.</p></li></ul><p>Be careful when using unsigned values in calculations. Subtracting a larger unsigned value from a smaller one can produce a very large result because unsigned arithmetic wraps around.</p><h2>Difference Between signed and unsigned in C++</h2><p>The main difference between signed and unsigned is the range of values they can store.</p><table><tbody><tr><th>Feature</th><th>signed</th><th>unsigned</th></tr><tr><td>Negative values</td><td>Yes</td><td>No</td></tr><tr><td>Zero</td><td>Yes</td><td>Yes</td></tr><tr><td>Positive values</td><td>Yes</td><td>Yes</td></tr><tr><td>Typical 32-bit range</td><td>-2,147,483,648 to 2,147,483,647</td><td>0 to 4,294,967,295</td></tr><tr><td>Common use</td><td>Temperature, profit, loss</td><td>Counts, bit patterns</td></tr></tbody></table><h3>Example: Comparing signed and unsigned</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">signed</span> <span class="js-id">int</span> <span class="js-id">number1</span> <span class="js-op">=</span> <span class="js-op">-</span><span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">unsigned</span> <span class="js-id">int</span> <span class="js-id">number2</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Signed value: "</span> <span class="js-op"><<</span> <span class="js-id">number1</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">"Unsigned value: "</span> <span class="js-op"><<</span> <span class="js-id">number2</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">Signed</span> <span class="js-id">value</span><span class="js-op">:</span> <span class="js-op">-</span><span class="js-num">100</span>
<span class="js-id">Unsigned</span> <span class="js-id">value</span><span class="js-op">:</span> <span class="js-num">100</span></code></pre></div><p>Notice that the signed variable accepts a negative value, while the unsigned variable is designed for non-negative values.</p><h2>Difference Between short, int, long and long long</h2><p>These integer types differ in their minimum required widths and the ranges they can represent.</p><table><tbody><tr><th>Data type</th><th>Minimum width</th><th>Typical size</th></tr><tr><td><code>short int</code></td><td>16 bits</td><td>2 bytes</td></tr><tr><td><code>int</code></td><td>16 bits</td><td>4 bytes</td></tr><tr><td><code>long int</code></td><td>32 bits</td><td>4 or 8 bytes</td></tr><tr><td><code>long long int</code></td><td>64 bits</td><td>8 bytes</td></tr></tbody></table><p>The sizes shown as typical are common examples, not universal guarantees. The C++ standard specifies minimum widths and ordering of ranges rather than requiring these exact sizes on every computer.</p><h2>Can We Combine Type Modifiers in C++?</h2><p>Yes. Some type modifiers can be combined to create different integer types.</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">short</span> <span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">unsigned</span> <span class="js-id">short</span> <span class="js-id">int</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">500</span><span class="js-op">;</span>
<span class="js-id">long</span> <span class="js-id">int</span> <span class="js-id">c</span> <span class="js-op">=</span> <span class="js-num">100000</span><span class="js-op">;</span>
<span class="js-id">unsigned</span> <span class="js-id">long</span> <span class="js-id">int</span> <span class="js-id">d</span> <span class="js-op">=</span> <span class="js-num">200000</span><span class="js-op">;</span>
<span class="js-id">long</span> <span class="js-id">long</span> <span class="js-id">int</span> <span class="js-id">e</span> <span class="js-op">=</span> 9000000000LL<span class="js-op">;</span>
<span class="js-id">unsigned</span> <span class="js-id">long</span> <span class="js-id">long</span> <span class="js-id">int</span> <span class="js-id">f</span> <span class="js-op">=</span> 10000000000ULL<span class="js-op">;</span></code></pre></div><p>You can combine <code>signed</code> or <code>unsigned</code> with <code>short</code>, <code>int</code>, <code>long</code>, and <code>long long</code>.</p><p>Some valid declarations include:</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">signed</span> <span class="js-id">short</span> <span class="js-id">int</span> <span class="js-id">a</span><span class="js-op">;</span>
<span class="js-id">unsigned</span> <span class="js-id">short</span> <span class="js-id">int</span> <span class="js-id">b</span><span class="js-op">;</span>
<span class="js-id">signed</span> <span class="js-id">long</span> <span class="js-id">int</span> <span class="js-id">c</span><span class="js-op">;</span>
<span class="js-id">unsigned</span> <span class="js-id">long</span> <span class="js-id">int</span> <span class="js-id">d</span><span class="js-op">;</span>
<span class="js-id">signed</span> <span class="js-id">long</span> <span class="js-id">long</span> <span class="js-id">int</span> <span class="js-id">e</span><span class="js-op">;</span>
<span class="js-id">unsigned</span> <span class="js-id">long</span> <span class="js-id">long</span> <span class="js-id">int</span> <span class="js-id">f</span><span class="js-op">;</span></code></pre></div><p>These combinations allow you to choose an integer type according to the range your program needs.</p><h2>Complete C++ Program Using All Four Type Modifiers</h2><p>Now let's look at a complete example that uses <code>short</code>, <code>long</code>, <code>signed</code>, and <code>unsigned</code> in one program.</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-id">short</span> <span class="js-id">int</span> <span class="js-id">smallNumber</span> <span class="js-op">=</span> <span class="js-num">1200</span><span class="js-op">;</span>
<span class="js-id">long</span> <span class="js-id">int</span> <span class="js-id">largeNumber</span> <span class="js-op">=</span> <span class="js-num">1000000</span><span class="js-op">;</span>
<span class="js-id">signed</span> <span class="js-id">int</span> <span class="js-id">temperature</span> <span class="js-op">=</span> <span class="js-op">-</span><span class="js-num">25</span><span class="js-op">;</span>
<span class="js-id">unsigned</span> <span class="js-id">int</span> <span class="js-id">studentCount</span> <span class="js-op">=</span> <span class="js-num">500</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Short Number: "</span> <span class="js-op"><<</span> <span class="js-id">smallNumber</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">"Long Number: "</span> <span class="js-op"><<</span> <span class="js-id">largeNumber</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">"Signed Number: "</span> <span class="js-op"><<</span> <span class="js-id">temperature</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">"Unsigned Number: "</span> <span class="js-op"><<</span> <span class="js-id">studentCount</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">Short</span> <span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">1200</span>
<span class="js-id">Long</span> <span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">1000000</span>
<span class="js-id">Signed</span> <span class="js-id">Number</span><span class="js-op">:</span> <span class="js-op">-</span><span class="js-num">25</span>
<span class="js-id">Unsigned</span> <span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">500</span></code></pre></div><h3>Explanation</h3><p>In this program:</p><ol start="1"><li><p><code>short int</code> stores a relatively small integer.</p></li><li><p><code>long int</code> stores a larger integer.</p></li><li><p><code>signed int</code> stores a negative number.</p></li><li><p><code>unsigned int</code> stores a non-negative number.</p></li></ol><p>This example demonstrates how different type modifiers can be used in the same C++ program.</p><h2>How to Check the Size of Type Modifiers in C++</h2><p>You can use the <code>sizeof()</code> operator to find how much memory a data type occupies.</p><p>This is useful when learning about data types and checking how your compiler handles them.</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">cout</span> <span class="js-op"><<</span> <span class="js-str">"Size of short: "</span>
<span class="js-op"><<</span> <span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">short</span><span class="js-op">)</span> <span class="js-op"><<</span> <span class="js-str">" bytes"</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">"Size of int: "</span>
<span class="js-op"><<</span> <span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">int</span><span class="js-op">)</span> <span class="js-op"><<</span> <span class="js-str">" bytes"</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">"Size of long: "</span>
<span class="js-op"><<</span> <span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">long</span><span class="js-op">)</span> <span class="js-op"><<</span> <span class="js-str">" bytes"</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">"Size of long long: "</span>
<span class="js-op"><<</span> <span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">long</span> <span class="js-id">long</span><span class="js-op">)</span> <span class="js-op"><<</span> <span class="js-str">" bytes"</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">"Size of unsigned int: "</span>
<span class="js-op"><<</span> <span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">unsigned</span> <span class="js-id">int</span><span class="js-op">)</span> <span class="js-op"><<</span> <span class="js-str">" bytes"</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>Example 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">Size</span> <span class="js-kw">of</span> <span class="js-id">short</span><span class="js-op">:</span> <span class="js-num">2</span> <span class="js-id">bytes</span>
<span class="js-id">Size</span> <span class="js-kw">of</span> <span class="js-id">int</span><span class="js-op">:</span> <span class="js-num">4</span> <span class="js-id">bytes</span>
<span class="js-id">Size</span> <span class="js-kw">of</span> <span class="js-id">long</span><span class="js-op">:</span> <span class="js-num">8</span> <span class="js-id">bytes</span>
<span class="js-id">Size</span> <span class="js-kw">of</span> <span class="js-id">long</span> <span class="js-id">long</span><span class="js-op">:</span> <span class="js-num">8</span> <span class="js-id">bytes</span>
<span class="js-id">Size</span> <span class="js-kw">of</span> <span class="js-id">unsigned</span> <span class="js-id">int</span><span class="js-op">:</span> <span class="js-num">4</span> <span class="js-id">bytes</span></code></pre></div><p>Your output may be different depending on your operating system, compiler, and system architecture.</p><h2>How to Find the Minimum and Maximum Range of a Data Type</h2><p>C++ provides the <code><limits></code> header to check the minimum and maximum values supported by a numeric type.</p><p>Instead of assuming a data type has a particular range, you can use <code>std::numeric_limits</code>.</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">include</span> <span class="js-op"><</span><span class="js-id">limits</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">cout</span> <span class="js-op"><<</span> <span class="js-str">"Short minimum: "</span>
<span class="js-op"><<</span> <span class="js-id">numeric_limits</span><span class="js-op"><</span><span class="js-id">short</span><span class="js-op">>::</span><span class="js-id">min</span><span class="js-op">()</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">"Short maximum: "</span>
<span class="js-op"><<</span> <span class="js-id">numeric_limits</span><span class="js-op"><</span><span class="js-id">short</span><span class="js-op">>::</span><span class="js-id">max</span><span class="js-op">()</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">"Int minimum: "</span>
<span class="js-op"><<</span> <span class="js-id">numeric_limits</span><span class="js-op"><</span><span class="js-id">int</span><span class="js-op">>::</span><span class="js-id">min</span><span class="js-op">()</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">"Int maximum: "</span>
<span class="js-op"><<</span> <span class="js-id">numeric_limits</span><span class="js-op"><</span><span class="js-id">int</span><span class="js-op">>::</span><span class="js-id">max</span><span class="js-op">()</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">"Unsigned int maximum: "</span>
<span class="js-op"><<</span> <span class="js-id">numeric_limits</span><span class="js-op"><</span><span class="js-id">unsigned</span> <span class="js-id">int</span><span class="js-op">>::</span><span class="js-id">max</span><span class="js-op">()</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>Example 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">Short</span> <span class="js-id">minimum</span><span class="js-op">:</span> <span class="js-op">-</span><span class="js-num">32768</span>
<span class="js-id">Short</span> <span class="js-id">maximum</span><span class="js-op">:</span> <span class="js-num">32767</span>
<span class="js-id">Int</span> <span class="js-id">minimum</span><span class="js-op">:</span> <span class="js-op">-</span><span class="js-num">2147483648</span>
<span class="js-id">Int</span> <span class="js-id">maximum</span><span class="js-op">:</span> <span class="js-num">2147483647</span>
<span class="js-id">Unsigned</span> <span class="js-id">int</span> <span class="js-id">maximum</span><span class="js-op">:</span> <span class="js-num">4294967295</span></code></pre></div><p>These are common results on systems with 16-bit <code>short</code> and 32-bit <code>int</code>.</p><p>This approach is useful because it allows your program to check actual limits instead of depending on assumed values.</p><h2>Common Mistakes When Using Type Modifiers in C++</h2><p>Beginners sometimes make mistakes when selecting integer types. Here are some common problems to avoid.</p><h3>1. Storing negative values in unsigned variables</h3><p>Incorrect 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">unsigned</span> <span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-op">-</span><span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">number</span><span class="js-op">;</span></code></pre></div><p>An unsigned integer cannot represent a negative value. The result is converted according to unsigned integer rules and may become a very large positive number.</p><p>Use <code>signed int</code> or ordinary <code>int</code> if negative values are required.</p><h3>2. Assuming long is always 8 bytes</h3><p>The size of <code>long</code> is not the same on every platform.</p><p>Use <code>sizeof(long)</code> to check its size or use <code>long long</code> when you need at least 64 bits.</p><h3>3. Using unsigned without considering subtraction</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">unsigned</span> <span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">5</span><span class="js-op">;</span>
<span class="js-id">unsigned</span> <span class="js-id">int</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">a</span> <span class="js-op">-</span> <span class="js-id">b</span><span class="js-op">;</span></code></pre></div><p>The result wraps around according to unsigned arithmetic rules instead of becoming -5.</p><p>If negative results are possible, consider using a signed type.</p><h3>4. Choosing a data type without checking its range</h3><p>Always consider the largest and smallest values your program may need to store.</p><p>The <code><limits></code> header can help you check the supported range.</p><h2>Best Practices for Using Type Modifiers</h2><p>Follow these simple practices when writing C++ programs:</p><ul><li><p>Use <code>int</code> for ordinary integer calculations.</p></li><li><p>Use <code>short</code> only when a smaller integer representation is genuinely useful or required.</p></li><li><p>Use <code>long long</code> for large integer calculations.</p></li><li><p>Use signed integers when negative values are possible.</p></li><li><p>Use unsigned integers when the data is naturally non-negative and its arithmetic behaviour is understood.</p></li><li><p>Use <code>sizeof()</code> to check memory size.</p></li><li><p>Use <code>std::numeric_limits</code> to check supported value ranges.</p></li><li><p>Avoid assuming that every computer uses the same data type sizes.</p></li></ul><p>Choosing the right data type makes your code easier to understand and helps prevent unexpected results.</p><h2>Frequently Asked Questions About Type Modifiers in C++</h2><h3>1. What are type modifiers in C++?</h3><p>Type modifiers are keywords that modify the properties of certain basic data types. The four commonly used modifiers are <code>short</code>, <code>long</code>, <code>signed</code>, and <code>unsigned</code>.</p><h3>2. How many type modifiers are there in C++?</h3><p>There are four commonly taught fundamental type modifiers: <code>short</code>, <code>long</code>, <code>signed</code>, and <code>unsigned</code>.</p><h3>3. What is the difference between short and long in C++?</h3><p><code>short</code> provides an integer type with a minimum width of 16 bits, while <code>long</code> provides an integer type with a minimum width of 32 bits. Their actual sizes depend on the platform.</p><h3>4. What is the difference between signed and unsigned?</h3><p>A signed integer can store negative and positive values, while an unsigned integer can store zero and positive values only.</p><h3>5. Can we use unsigned with int in C++?</h3><p>Yes. You can declare an unsigned integer using <code>unsigned int</code>.</p><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">unsigned</span> <span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span></code></pre></div><h3>6. What is the size of short int in C++?</h3><p>A <code>short int</code> is at least 16 bits wide. On many common systems, it occupies 2 bytes.</p><h3>7. What is the size of long long int in C++?</h3><p>A <code>long long int</code> is at least 64 bits wide. It commonly occupies 8 bytes.</p><h3>8. Is int signed or unsigned by default?</h3><p>An ordinary <code>int</code> is signed by default. Therefore, <code>int</code> and <code>signed int</code> represent the same type.</p><h3>9. Can unsigned numbers be negative in C++?</h3><p>No. Unsigned integer types cannot represent negative numbers. Assigning a negative value results in conversion to the corresponding unsigned value.</p><h3>10. Which type modifier should beginners use?</h3><p>For ordinary programming, <code>int</code> is a good starting point. Choose <code>short</code>, <code>long</code>, or <code>unsigned</code> when your program's requirements make their different ranges useful.</p>
Code Example
#include <iostream>
#include <limits>
int main() {
short sVal = 32000;
unsigned short usVal = 65000;
long long bigNumber = 9000000000000000000LL;
unsigned int wrapped = 0;
wrapped = wrapped - 1; // Wraps around to UINT_MAX
std::cout << "short: " << sVal << " (bytes: " << sizeof(sVal) << ")";
std::cout << "unsigned short: " << usVal << "";
std::cout << "long long: " << bigNumber << " (bytes: " << sizeof(bigNumber) << ")";
std::cout << "Unsigned underflow wrap (0 - 1): " << wrapped << "";
return 0;
}Interview Question & Tips
A: Signed integer overflow is Undefined Behavior (UB) in C++, allowing the compiler optimizer to assume it never happens. Unsigned integer overflow is well-defined to wrap around modulo 2^N.
Q: Why should mixing signed and unsigned comparisons be avoided?
A: Comparing a negative signed integer with an unsigned integer implicitly promotes the signed value to unsigned, turning -1 into a huge positive number like 4,294,967,295, causing false comparison results.
Best Practices
2. Use `std::numeric_limits<T>::max()` to check boundary limits dynamically.
3. Prefer signed types for arithmetic calculations and `size_t` exclusively for sizes.
This study reference for Type Modifiers in C++: short, long, signed, and unsigned is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation: