comparison between signed and unsigned int

C

compcreator

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

Tim Prince

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

Ben Pfaff

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

Mark Bluemel

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
 
K

Keith Thompson

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

CBFalconer

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

Army1987

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top