Re: comparison between signed and unsigned int
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Fri, 21 Sep 2007 15:17:19 -0400
compcreator@xxxxxxxxx wrote:
.... snip ...
I have tried the following program. The problem is it is printing
False
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
.
- References:
- comparison between signed and unsigned int
- From: compcreator
- comparison between signed and unsigned int
- Prev by Date: Re: x == 0 && (x & -1) != 0 for negative zero?
- Next by Date: Re: Logical XOR
- Previous by thread: Re: comparison between signed and unsigned int
- Next by thread: Re: comparison between signed and unsigned int
- Index(es):
Relevant Pages
|
|