unsigned int类型(C ++)VS UINT(C#) [英] unsigned int (c++) vs uint (c#)

查看:263
本文介绍了unsigned int类型(C ++)VS UINT(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是C#代码:

   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");
    }



这是C ++代码:

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?

推荐答案

C ++和C#是不同的语言。他们有处理,在比较的事件类型推广不同的规则。

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

在C ++和C,它们通常比较,如果它们都是无符号的。这被称为无符号保留。 C ++和C编译器传统上使用无符号保留,并在C中指定使用这种++标准和K&放大器;:R

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.

在C#中,他们都转换以多头签约,然后进行比较。这就是所谓的保值。 C#指定保值。

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

ANSI C也指定保值,但只有短裤和字符的时候。短裤和字符(符号和无符号)上变换成整数的保值方式,然后进行比较。所以,如果一个无符号short进行了比较,一个符号的短,结果会出来,如C#示例。转化为更大的尺寸做的任何时间,它在一个值保留方式做了,但如果两个变量是相同的大小(而不是短裤或字符)和任一个是无符号的,则它们会相比,在无符号的数量ANSI C.还有的上下两个侧面和一商量好了在comp.lang.c常见问题接近

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.

这篇关于unsigned int类型(C ++)VS UINT(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆