2

I want to know that why it only executes the else statement

The code is Given below:

#include<stdio.h>
int main()
{
    unsigned int a = 100;
    int b = -100;
    if(a > b)
    {
        print("Obviously 100 is Bigger than -100!\n");
    }
    else
        print("Something Unexpected has Happened\n");
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Amir Shahzad
  • 73
  • 1
  • 6
  • Why the downvote? Apart from the awful formatting, this is well presented and answerable, and truly bewildering the first time you encounter this. – Bathsheba Dec 02 '19 at 13:54
  • @SanderDeDycker: My instinct suggested to me that this one had a duplicate and there was also nothing else special in the question to warrant a brand-new answer, which is why I didn't answer. Personally I think that it should be possible to upvote and downvote the actions of a duplicate spotter. – Bathsheba Dec 02 '19 at 14:16
  • (note for the record I wasn't the one downvoting - I agree with the sentiment that a downvote wasn't warranted) – Sander De Dycker Dec 02 '19 at 14:18

2 Answers2

6

This statement

  if(a>b)

involves operation (comparison) between a signed and an unsigned integer, and as per the promotion rules, the signed integer will be promoted to unsigned integer and produce a huge unsigned value: for example, in an environment with 32-bit integer, the value would be 4294967196 (232 - 100).

Thereby, the condition will look like

if (100 > 4294967196)

and will evaluate to false, making sure the code in the else block is executed.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

This is happening because the value -100 is being converted to a large positive value.

What you see is a result of the usual arithmetic conversions. When a signed integer value and an unsigned integer value of the same rank (int and unsigned int in this case) are used in an expression, the signed value is converted to the unsigned value.

These conversions are spelled out in section 6.3.1.8p1 of the C standard:

If both operands have the same type, then no further conversion is needed.

Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.

Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.

Otherwise, both operands are converted to the unsigned
integer type corresponding to the type of the operand with signed integer type.

The highlighted paragraph is what applies in this case. As for how the actual conversion occurs, that is spelled out in section 6.3.1.3p2:

Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

So assuming an unsigned int is 32 bits in size with no padding bytes, the maximum value it can hold is 4294967295. That means that the value -100 is converted to the value 4294967196. This means the comparison that is actually performed is 100 > 4294967295 which is false, so the else portion of the statement is executed.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
dbush
  • 205,898
  • 23
  • 218
  • 273