Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > comparison between signed and unsigned int

Reply
Thread Tools

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....?

 
Reply With Quote
 
 
 
 
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.
 
Reply With Quote
 
 
 
 
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;}}}
 
Reply With Quote
 
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
 
Reply With Quote
 
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
'int main(void)', add '#include <stdio.h>', add a terminating newline
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"
 
Reply With Quote
 
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

 
Reply With Quote
 
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
comparison between signed and unsigned evanevankan2@hushmail.com C Programming 4 07-13-2008 02:50 PM
comparison between signed and unsigned integer expressions Gary Wessle C++ 4 07-29-2006 05:56 AM
comparison between signed int and unsigned int Alex C Programming 3 04-26-2006 05:20 AM
comparison between signed and unsigned John Buckley C Programming 2 10-24-2003 11:53 AM



Advertisments