why overflow happens on calculation depending on the data type when the type where the value is being assigned can hold it
<p>Earlier I came up with something, which I solved, but it got me later
let's take a look at a similar example of what I was on:</p>

<pre><code>int b = 35000000; //35million
int a = 30000000;
unsigned long n = ( 100 * a ) / b;
</code></pre>

<p>Output: 4294967260</p>

<p>I simply changed <code>a</code> to <code>unsigned long</code> and the correct 85% output would come up, because <code>a</code> is a signed 32bit integer. But this got me later. There is no value assignment to <code>a</code> during <code>( 100 * a )</code> there is just simply a calculation and the correct value which is 3billion should come up instead of an overflow. To understand if there wasn't really an assignment to <code>a</code> I removed <code>a</code> from the code and manually write the value instead instead:</p>

<pre><code>int b = 35000000;
unsigned long n = ( 100 * 30000000 ) / b;
</code></pre>

<p>The big surprise was that the output is also: 4294967260<br>
And of course value of 3billion can be assigned to an <code>unsigned long</code>.
My first thought was that <code>( 100 * 30000000 )</code> was causing an overflow, but then I asked "an overflow on what? there is nothing to be overflowed".
Then I changed <code>b</code> to unsigned long, which even most suprisingly the output was correct 85%.</p>

<p>In the first example changing <code>a</code> to <code>unsigned long</code> </p>

<pre><code>int b = 35000000;
unsigned long a = 30000000;
unsigned long n = ( 100 * a ) / b;
</code></pre>

<p>and leaving <code>b</code>as an <code>int</code> as it is works, but on the second example it doesn't, what is occuring?</p>

<p>This might be a little overwhelming to let me re-write all examples with the ones who work and the ones who dont.</p>

<p>Works (Output = 85):</p>

<pre><code>int b = 35000000;
unsigned long a = 30000000;
unsigned long n = ( 100 * a ) / b;
</code></pre>

<p>Works (Output = 85):</p>

<pre><code>unsigned long b= 35000000;
unsigned long n = ( 100 * 30000000 ) / b;
</code></pre>

<p>Doesn't works (Overflow):</p>

<pre><code>int b = 35000000;
int a = 30000000;
unsigned long n = ( 100 * a ) / b;
</code></pre>

<p>Doesn't works (Overflow):</p>

<pre><code>int b = 35000000;
unsigned long n = ( 100 * 30000000 ) / b;
</code></pre>
