Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>the <code>%d</code> format specifier tells <code>printf</code> that you are passing in an <em>int</em> but in the first case you are passing in a <em>double</em> which is also <a href="http://en.wikipedia.org/wiki/Undefined_behavior" rel="nofollow noreferrer">undefined behavior</a> and so the results are not reliable. The result of:</p> <pre><code>money+money*0.4 </code></pre> <p>is <em>double</em> since a <em>floating constant</em> is <em>double</em> unless it has a suffix such as <code>f</code> and the results of the <em>multiplication</em> and <em>addition</em> are <em>double</em> as well due to the <em>usual arithmetic conversions</em> which both operations are subject to and which will cause the value of <code>money</code> to be converted to <em>double</em> for the operation.</p> <p>In the second case you are correctly passing in <em>int</em> and since you are assigning the result to <code>money</code>:</p> <pre><code>money=money+money*0.4 </code></pre> <p>it will truncate the <em>double</em> value. I am not sure what compiler you are using but both <code>clang</code> and <code>gcc</code> without any warning flags both warn about the incorrect format specifier, <code>gcc</code> for example says:</p> <blockquote> <p>warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat]</p> </blockquote> <p>So if you are not seeing any warning for that line you should look into setting your warning levels higher.</p> <p>For completeness sake the <a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf" rel="nofollow noreferrer">draft C99 standard</a> section <code>7.19.6.1</code> <em>The fprintf function</em>, which also covers <code>printf</code> with respect to the format specifiers, In paragraph <em>9</em> says:</p> <blockquote> <p>If a conversion specification is invalid, the behavior is undefined.<sup>248)</sup> If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.</p> </blockquote>
    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. 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.
    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