Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #11543

Re: unsigned == signed ?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: unsigned == signed ?
Date 2011-10-11 12:57 -0700
Organization None to speak of
Message-ID <lnpqi3ti64.fsf@nuthaus.mib.org> (permalink)
References <j72687$bcn$1@speranza.aioe.org>

Show all headers | View raw


Raj Pashwar <raj121190@hotmail.NOSPAM.com> writes:
> int main()
> {
> uint x = -1;
> int y = -1;
> if (x == y)
> printf("EQUAL");
> else
> printf("NOT EQUAL");
> }
>
> This code prints "EQUAL". Does this mean that bit comparison is done for 
> the
> equality test? If it doesn't can you give me an example that shows bit
> comparison isn't done? I also wanted to know if we can assign a number to
> uint & int variables such that "unit var != int var". Thanks so much 

There is no predefined type "uint" in C.  I'll assume you meant
"unsigned int".

Here's a corrected version of your program:

#include <stdio.h>

int main(void)
{
    unsigned int x = -1;
    int y = -1;
    if (x == y) {
        printf("EQUAL\n");
    }
    else {
        printf("NOT EQUAL\n");
    }
    return 0;
}

There's actually a fair amount of stuff going on here, some of it
not obvious.

The expression "-1" is of type int.  The initialization applies an
implicit conversion from int to unsigned int.  The rules for such
a conversion (see C99 6.3.1.3p2) mean that the initial value of
x is UINT_MAX.  (This is probably 4294967295 on a typical 32-bit
system, but other values are possible.)

The comparison "x == y" is comparing an unsigned int value to a
(signed) int value.  Before the comparison is performed, the
"usual arithmetic conversions" are applied to the operands (C99
6.5.9p4, 6.3.1.8).  In this case, the int operand is converted to
unsigned int.  Since y's value is -1, the result is the same as
the result of the conversion in x's initialization.

The result is that "x == y" is comparing UINT_MAX to UINT_MAX,
which of course yields a true value (1).

As it happens, the result is the same that you'd get if the program
just performed bitwise comparisons of the operands.  The rules
for conversions and comparisons are defined purely in terms of
the arithmetic *values* of the operands, but the rules are written
in such a way that they can be implemented easily on most systems
(2's-complement representation for signed values).

The latest draft of the C standard is at
    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
(that's a 3.7-megabyte PDF document).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

unsigned == signed ? Raj Pashwar <raj121190@hotmail.NOSPAM.com> - 2011-10-11 19:43 +0000
  Re: unsigned == signed ? Keith Thompson <kst-u@mib.org> - 2011-10-11 12:57 -0700
  Re: unsigned == signed ? James Kuyper <jameskuyper@verizon.net> - 2011-10-11 16:23 -0400
  Re: unsigned == signed ? Quentin Pope <qp19433@hotmail.NOSPAM.com> - 2011-10-11 20:33 +0000
    Re: unsigned == signed ? James Kuyper <jameskuyper@verizon.net> - 2011-10-11 16:40 -0400
    Re: unsigned == signed ? Keith Thompson <kst-u@mib.org> - 2011-10-11 14:34 -0700
      Re: unsigned == signed ? Quentin Pope <qp19433@hotmail.NOSPAM.com> - 2011-10-12 16:04 +0000
        Re: unsigned == signed ? Keith Thompson <kst-u@mib.org> - 2011-10-12 10:49 -0700
        Re: unsigned == signed ? pete <pfiland@mindspring.com> - 2011-10-12 21:12 -0400
    Re: unsigned == signed ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-11 22:35 +0100
      Re: unsigned == signed ? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-13 14:54 +0300
        Re: unsigned == signed ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-13 15:26 +0100
    Re: unsigned == signed ? "J. J. Farrell" <jjf@bcs.org.uk> - 2011-10-12 03:02 +0100
  Re: unsigned == signed ? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-13 13:56 +0300

csiph-web