Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>2500000000</code> doesn't fit in an <code>int</code>, so the compiler correctly interprets it as a <code>long</code> (or <code>long long</code>, or a type where it fits). <code>1250000000</code> does, and so does <code>2</code>. The parameter to <code>sizeof</code> <strong>isn't evaluated</strong>, so the compiler can't possibly know that the multiplication doesn't fit in an <code>int</code>, and so returns the size of an <code>int</code>.</p> <p>Also, even if the parameter was evaluated, you'd likely get an overflow (and undefined behavior), but probably still resulting in <code>4</code>.</p> <p>Here:</p> <pre><code>#include &lt;iostream&gt; int main() { long long x = 1250000000 * 2; std::cout &lt;&lt; x; } </code></pre> <p>can you guess the output? If you think it's <code>2500000000</code>, you'd be wrong. The type of the expression <code>1250000000 * 2</code> is <code>int</code>, because the operands are <code>int</code> and <code>int</code> and multiplication isn't automagically promoted to a larger data type if it doesn't fit.</p> <p><a href="http://ideone.com/4Adf97">http://ideone.com/4Adf97</a></p> <p>So here, gcc says it's <code>-1794967296</code>, but it's undefined behavior, so that could be any number. This number does fit into an <code>int</code>.</p> <p>In addition, if you cast one of the operands to the expected type (much like you cast integers when dividing if you're looking for a non-integer result), you'll see this working:</p> <pre><code>#include &lt;iostream&gt; int main() { long long x = (long long)1250000000 * 2; std::cout &lt;&lt; x; } </code></pre> <p>yields the correct <code>2500000000</code>.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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