Re: comparison between signed and unsigned int



compcreator@xxxxxxxxx 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) kst-u@xxxxxxx <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"
.



Relevant Pages