0

Possible Duplicate:
Arithmetic operations on unsigned and signed integers

unsigned int b=2;
int a=-2;

if(a>b)
    printf("a>b");
else
    printf("b>a");

OUTPUT: a>b

int b=2;
int a=-2;

if(a>b)
    printf("a>b");
else
    printf("b>a");

OUTPUT: b>a

PLEASE, someone explain the output

Community
  • 1
  • 1

5 Answers5

1

In the first case both operands are converted to unsigned int, the converted a will be UINT_MAX-1, which is much larger than b and hence the output.

Don't compare signed and unsigned integers unless you understand the semantics of arithematic conversions, the results might surprise you.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

The following is taken from The C Programming Language by Kernighan and Ritchie - 2.7 Type Conversions - page 44; the second half of the page explains the same scenario in detail. A small portion is below for your reference.

Conversion rules are complicated when unsigned operands are involved. The problem is that comparison between signed and unsigned values are machine dependent, because they depend on the sizes of the various integer types. For example, suppose that int is 16 bits long and long is 32 bits. Then -1L < 1U, because 1U, which is an int, is promoted to a signed long. But -1L > 1UL, because -1L is promoted to unsigned long and thus appears to be a larger positive number.

Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
  • While true in general, in this case, it is relatively straight-forward; both types are the same size of `int`, albeit one is `signed` and the other `unsigned`. – Jonathan Leffler Apr 20 '12 at 05:06
0

When signed and unsigned values are compared, and when the unsigned values can't all be represented in the signed type, then the signed operand is promoted to unsigned. This is done with a formula that amounts to a reinterpretation of the 2-s complement bit pattern.

Well, negative numbers have lots of high bits set...

Since your operands are all of the same rank, it's just a matter of unsigned bit patterns being compared.

And so -2 is represented with 111111..110, one less than the largest possible, and it easily beats 2 when interpreted as unsigned.

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
0

You need to learn the operation of the operators in C and the C promotion and conversion rules. They are explained in the C standard. Some excerpts from it plus my comments:

6.5.8 Relational operators
Syntax
1 relational-expression:
shift-expression
relational-expression < shift-expression
relational-expression > shift-expression
relational-expression <= shift-expression
relational-expression >= shift-expression

Semantics
3 If both of the operands have arithmetic type, the usual arithmetic conversions are performed.

Most operators include this "usual arithmetic conversions" step before the actual operation (addition, multiplication, comparison, etc etc). - Alex

6.3.1.8 Usual arithmetic conversions

1 Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. For the specified operands, each operand is converted, without change of type domain, to a type whose corresponding real type is the common real type. Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise. This pattern is called the usual arithmetic conversions:

  • First, if the corresponding real type of either operand is long double, the other operand is converted, without change of type domain, to a type whose corresponding real type is long double.

  • Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.

  • Otherwise, if the corresponding real type of either operand is float, the other operand is converted, without change of type domain, to a type whose corresponding real type is float.

  • Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:

  • If both operands have the same type, then no further conversion is needed.

  • Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.

  • Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

  • Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.

  • Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

6.3.1.3 Signed and unsigned integers

  1. When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

  2. Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. (The rules describe arithmetic on the mathematical value, not the value of a given type of expression.)

  3. Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

So, in your a>b (with a being an int and b being an unsigned int), per the above rules you get a converted to unsigned int before the comparison. Since a is negative (-2), the unsigned value becomes UINT_MAX+1+a (this is the repeatedly adding or subtracting one more than the maximum value bit). And UINT_MAX+1+a in your case is UINT_MAX+1-2 = UINT_MAX-1, which is a huge positive number compared to the value of b (2). And so a>b yields "true".

Forget the math you learned at school. Learn how C does it.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
-2

In this first case you get the unsigned a converted into a signed int. Then these two are compared.

Type conversion ranks between signed and unsigned types could have the same rank in C99. This is when a unsigned and a signed type have the corresponding types, when this happens the result is up to the compiler.

Here is a summary of the rules.

Balp
  • 1
  • 1
  • 1
    To be blunt but accurate: that is wrong. – Jonathan Leffler Apr 20 '12 at 04:59
  • [link](https://www.securecoding.cert.org/confluence/display/seccode/INT02-C.+Understand+integer+conversion+rules) The rank of any unsigned integer type is equal to the rank of the corresponding signed integer type, if any. – Balp Apr 20 '12 at 05:21
  • The key (nay, crucial) word 'corresponding' is signally lacking from your answer. 'Convert ions' should probably be 'conversion'. There is no compiler dependency in the situation asked about in the question; it is completely determined by the C standard. (And, for the record, I've not down-voted you; at least, not yet.) You can and should edit your answer to rescue it. For example, you could add your link to the answer. – Jonathan Leffler Apr 20 '12 at 05:33
  • My first try to answer anything here. I hope the changes made it better. – Balp Apr 20 '12 at 05:55
  • 2
    OK - Welcome to StackOverflow. FYI: ISO 9089:1999 (C99) §6.3.1.8 'Usual Arithmetic Conversions' includes the statement: _Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type._ That appears to apply to this question, where the unsigned integer is of the same rank as the signed integer, but it also seems to be the opposite of what you've said in your first edit. – Jonathan Leffler Apr 20 '12 at 06:06