Velocity Reviews > comparison between signed and unsigned int

# comparison between signed and unsigned int

compcreator@gmail.com
Guest
Posts: n/a

 09-21-2007
I have tried the following program. The problem is it is printing
False

I checked values for a and b but there is something wrong with the
comparison.
I thing it might be because of signed and unsigned conversion.
Either b is upgraded to unsigned and the resulting value during
comparison is > 5 or a is downgraded to signed and the resulting value
is lower than -1.

void main()
{
unsigned int a = 5;
signed int b = -1;

if(b <= a)
printf("True");
else
printf("False");
}

Can somebody explain why is this happening....?

Tim Prince
Guest
Posts: n/a

 09-21-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have tried the following program. The problem is it is printing
> False
>
> I checked values for a and b but there is something wrong with the
> comparison.
> I thing it might be because of signed and unsigned conversion.
> ... b is upgraded to unsigned and the resulting value during
> comparison is > 5

Yes, this is an FAQ and should be explained in all textbooks.

Ben Pfaff
Guest
Posts: n/a

 09-21-2007
(E-Mail Removed) writes:

> unsigned int a = 5;
> signed int b = -1;
>
> if(b <= a)

When you compare an unsigned int and a signed int in this
fashion, the signed int is converted to unsigned int. Converting
a negative signed int to a unsigned int is done by adding
UINT_MAX + 1. Thus, the comparison is effectively:
if (UINT_MAX <= 5)
which is false.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}

Mark Bluemel
Guest
Posts: n/a

 09-21-2007
(E-Mail Removed) wrote:
> I have tried the following program. The problem is it is printing
> False
>
> I checked values for a and b but there is something wrong with the
> comparison.
> I thing it might be because of signed and unsigned conversion.

FAQs 3.18 and 3.19 - look under http://c-faq.com

Keith Thompson
Guest
Posts: n/a

 09-21-2007
(E-Mail Removed) writes:
> I have tried the following program. The problem is it is printing
> False
>
> I checked values for a and b but there is something wrong with the
> comparison.
> I thing it might be because of signed and unsigned conversion.
> Either b is upgraded to unsigned and the resulting value during
> comparison is > 5 or a is downgraded to signed and the resulting value
> is lower than -1.
>
> void main()
> {
> unsigned int a = 5;
> signed int b = -1;
>
> if(b <= a)
> printf("True");
> else
> printf("False");
> }
>
> Can somebody explain why is this happening....?

In addition to reading the FAQ, you need to change 'void main()' to
to each of your output strings (or use puts()), and add a 'return 0;'
at the end of your program.

You should also find out how to persuade your compiler to give you
more warnings; it could have told you about most of these problems.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

CBFalconer
Guest
Posts: n/a

 09-21-2007
(E-Mail Removed) wrote:
>
> I have tried the following program. The problem is it is printing
> False
>

.... snip ...
>
> void main() {
> unsigned int a = 5;
> signed int b = -1;
>
> if (b <= a) printf("True");
> else printf("False");
> }
>
> Can somebody explain why is this happening....?

a is signed and negative, and cannot fit into the range covered by
b. Therefore it is converted to an unsigned value before
comparing. The conversion results in UINT_MAX, which is
considerably larger than 5. Signed can always be converted to
unsigned, but not the reverse.

Get rid of the 'void main()', which marks you as unknowing. main
returns an int, say and do so. The satisfactory return values are
0, EXIT_SUCCESS, and EXIT_FAILURE. The latter two require #include
<stdlib.h>. Also specify void in the parameter list, unless you
are using argc and argv.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Army1987
Guest
Posts: n/a

 09-21-2007
On Fri, 21 Sep 2007 06:16:07 -0700, compcreator wrote:

> I have tried the following program. The problem is it is printing
> False
>
> I checked values for a and b but there is something wrong with the
> comparison.
> I thing it might be because of signed and unsigned conversion.
> Either b is upgraded to unsigned and the resulting value during
> comparison is > 5 or a is downgraded to signed and the resulting value
> is lower than -1.

Last time I checked 5 wasn't lower than -1.
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen