unsigned int (c++) vs uint (c#)

Samir Lakhani picture Samir Lakhani · Nov 25, 2011 · Viewed 7.8k times · Source

Following is the c# code:

   static void Main(string[] args)
    {
        uint y = 12;
        int x = -2;
        if (x > y)
            Console.WriteLine("x is greater");
        else
            Console.WriteLine("y is greater");
    }

and this is c++ code:

int _tmain(int argc, _TCHAR* argv[])
{
unsigned int y = 12;
int x = -2;
if(x>y)
    printf("x is greater");
else
    printf("y is greater");

return 0;
}

Both are giving different result. Am I missing something basic? Any idea?

Answer

Keith Irwin picture Keith Irwin · Nov 25, 2011

C++ and C# are different languages. They have different rules for handling type promotion in the event of comparisons.

In C++ and C, they're usually compared as if they were both unsigned. This is called "unsigned preserving". C++ and C compilers traditionally use "unsigned preserving" and the use of this is specified in the C++ standard and in K&R.

In C#, they're both converted to signed longs and then compared. This is called "value preserving". C# specifies value preserving.

ANSI C also specifies value preserving, but only when dealing with shorts and chars. Shorts and chars (signed and unsigned) are upconverted to ints in a value-preserving manner and then compared. So if an unsigned short were compared to a signed short, the result would come out like the C# example. Any time a conversion to a larger size is done, it's done in a value-preserving manner, but if the two variables are the same size (and not shorts or chars) and either one is unsigned, then they get compared as unsigned quantities in ANSI C. There's a good discussion of the up and down sides of both approaches in the comp.lang.c FAQ.