看一看这个C代码:INT推广在C unsigned int类型和C#
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.*/
我明白为什么输出x是更大,因为当计算机比较他们两个,x被晋升为一个无符号整数类型。 当x被提升为无符号整数时,-2变成65534,肯定大于10.
但是为什么在C#中,等效代码是否会给出相反的结果呢?
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.
因为C#比较好? :D – lukegravitt
我今天刚学到了一些关于C的新东西。我必须承认我不喜欢它。编译器是否至少会发出警告? –
[unsigned int(C++)vs uint(c#)]可能的重复(http://stackoverflow.com/questions/8266089/unsigned-int-c-vs-uint-c) – lukegravitt