Reinventing The Wheel

Unsigned Vs Signed Integers

What will be the output of the following C code?
int main()
{
    unsigned int y = 10;
    int x = -2;
    if (x > y)
        printf("x is greater");
    else
        printf("y is greater");
    return 0;
}
/*Output: x is greater.*/
Output: x is greater
Why? Because x is signed integer, y is unsigned integer. When the computer compares both of them, x is promoted to an unsigned integer type.
When x is promoted to unsigned integer, -2 becomes 65534 which is definitely greater than 10.
Why does it get converted to 65534? Go here to find out.
This interchanging of signed/unsigned numbers can lead to security bugs as described here.

Note that the equivalent code in C# produces the intuitive result of "y is greater":
public static void Main(String[] args)
{
    uint y = 10;
    int x = -2;
    if (x > y)
    {
        Console.WriteLine("x is greater");
    }
    else
    {
        Console.WriteLine("y is greater");
    }
}
//Output: y is greater.

This is because both uint and int get promoted to long before the comparison. You can read more about this and the rules for other binary numeric promotions on the MSDN website.