Note that there are some explanatory texts on larger screens.

plurals
  1. POQuestion about C datatype and constant
    primarykey
    data
    text
    <p>Greetings! I was experimenting with C language till I encountered something very strange. I was not able to explain myself the result shown below.</p> <p><strong>The Code:</strong></p> <pre><code>#include &lt;stdio.h&gt; int main(void) { int num = 4294967295U; printf("%u\n", num); return 0; } </code></pre> <p><strong>The Question:</strong></p> <p>1.) As you see, I created an <code>int</code> which can hold numbers between <em>-2147483648</em> to <em>2147483647</em>.</p> <p>2.) When I assign the value <em>4294967295</em> to this variable, the IDE shows me a warning message during compilation because the variable overflowed.</p> <p>3.) Due to curiosity I added a <em>U (unsigned)</em> behind the number and when I recompiled it, the compiler did not return any warning message.</p> <p>4.) I did further experiments by changing the <em>U (unsigned)</em> to <em>L (long)</em> and <em>LL (long long)</em>. As expected, the warning message still persist for these two but not after I change it to <em>UL (unsigned Long)</em> and <em>ULL (unsigned long long)</em>.</p> <p>5.) Why is this happening?</p> <p><strong>The Warning Message :(For steps 2)</strong></p> <pre><code>warning #2073: Overflow in converting constant expression from 'long long int' to 'int'. </code></pre> <p><strong>The Warning Message:(For steps 4 <em>LL</em> &amp; <em>L</em>)</strong></p> <pre><code>warning #2073: Overflow in converting constant expression from 'long long int' to 'long int'. </code></pre> <p><em>And last, thanks for reading my question, your teachings and advices are much appreciated.</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload