<p>Let's take the code</p>

<pre><code>int a, b, c;
...
if ((a + b) &gt; C)
</code></pre>

<p>If we add the values of a and b and the sum exceeds the maximum value of an int, will the integrity of the comparison be compromised?  I was thinking that there might be an implicit up cast or overflow bit check and that will be factored into the evaluation of this expression.</p>

[{'answer_id': 50530, 'author': 'hazzen', 'author_id': 5066, 'author_profile': 'https://Stackoverflow.com/users/5066', 'pm_score': 4, 'selected': True, 'text': '<p>C will do no such thing. It will silently overflow and lead to a possibly incorrect comparison. You can up-cast yourself, but it will not be done automatically.</p>\n'}, {'answer_id': 50531, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 0, 'selected': False, 'text': '<p>I believe this might be platform specific. Check the C documentation on how overflows are handled...</p>\n\n<p>Ah, yes, and the upcast will not happen automatically...</p>\n'}, {'answer_id': 50611, 'author': 'cwick', 'author_id': 4828, 'author_profile': 'https://Stackoverflow.com/users/4828', 'pm_score': 0, 'selected': False, 'text': '<p>See section 2.7, <em>Type Conversions</em> in the K&amp;R book</p>\n'}, {'answer_id': 51038, 'author': 'Rob Pilkington', 'author_id': 1623, 'author_profile': 'https://Stackoverflow.com/users/1623', 'pm_score': 2, 'selected': False, 'text': '<p>A test confirms that GCC 4.2.3 will simply compare with the overflowed result:</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\nint main()\n{\n    int a, b, c;\n\n    a = 2000000000;\n    b = 2000000000;\n    c = 2100000000;\n\n    printf("%d + %d = %d\\n", a, b, a+b);\n    if ((a + b) &gt; c)\n    {\n        printf("%d + %d &gt; %d\\n", a, b, c);\n    }\n    else\n    {\n        printf("%d + %d &lt; %d\\n", a, b, c);\n    }\n    return 0;\n}\n</code></pre>\n\n<p>Displays the following:</p>\n\n<pre><code>2000000000 + 2000000000 = -294967296\n2000000000 + 2000000000 &lt; 2100000000\n</code></pre>\n'}, {'answer_id': 992633, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>If upcasting doesn\'t gain you any bits (there\'s no guarantee that sizeof(long)>sizeof(int) in C), you can use conditions like the ones below to compare and check for overflow—upcasting is almost certainly faster if you can use it, though.</p>\n\n<pre><code>#if !defined(__GNUC__) || __GNUC__&lt;2 || (__GNUC__==2 &amp;&amp; __GNUC_MINOR__&lt;96)\n#   define unlikely(x) (x)\n#else\n#   define unlikely(x)     (__extension__ (__builtin_expect(!!(x), 0)))\n#endif\n\n/* ----------\n * Signed comparison (signed char, short, int, long, long long)\n * Checks for overflow off the top end of the range, in which case a+b must\n * be &gt;c.  If it overflows off the bottom, a+b &lt; everything in the range. */\nif(a+b&gt;c || unlikely(a&gt;=0 &amp;&amp; b&gt;=0 &amp;&amp; unlikely(a+b&lt;0)))\n    ...\n\n/* ----------\n * Unsigned comparison (unsigned char, unsigned short, unsigned, etc.)\n * Checks to see if the sum wrapped around, since the sum of any two natural\n * numbers must be &gt;= both numbers. */\nif(a+b&gt;c || unlikely(a+b&lt;a))\n    ...\n\n/* ----------\n * To generate code for the above only when necessary: */\nif(sizeof(long)&gt;sizeof(int) ? ((long)a+b&gt;c)\n       : (a+b&gt;c || unlikely(a&gt;=0 &amp;&amp; b&gt;=0 &amp;&amp; unlikely(a+b&lt;0)))\n    ...\n</code></pre>\n\n<p>Great candidates for macros or inline functions.  You can pull the "unlikely"s if you want, but they can help shrink and speed up the code GCC generates.</p>\n'}]
['https://Stackoverflow.com/questions/50525', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2064/']