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?
uint isn't a standard type - unsigned int is. and what does this fact implies? That code written with uint won't be inherently portable unless uint is a typedef that you declare actually inside that code.
Since we use number with positive and negative integers more often than positive integers only, the type Int is the signed integers. If we want a value without a sign, then we use the type UInt . UInt creates a integer of the same bit size as the device's processor can handle.
uint means “unsigned integer” while int means “signed integer”. Unsigned integers only contain positive numbers (or zero).
uint is a keyword that is used to declare a variable which can store an integral type of value (unsigned integer) from the range of 0 to 4,294,967,295.
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.
In C++, when you compare an unsigned int
and a signed int
, the signed int
is converted to unsigned int
. Converting a negative signed int
to an unsigned int
is done by adding UINT_MAX + 1
, which is larger than 12
and hence the result.
In C#, if you are getting the opposite result then it means that in C# both the expressions are being converted to signed int
signed long
(long
or System.Int64
)1 and then compared.
In C++, your compiler must have given you the warning:
warning: comparison between signed and unsigned integer expressions
Rule:
Always take warnings emitted by the compiler seriously!
1 As rightly pointed out by svick in comments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With