comparison of an unsigned variable to 0
Posted
by
user2651062
on Programmers
See other posts from Programmers
or by user2651062
Published on 2014-08-17T18:11:24Z
Indexed on
2014/08/20
10:32 UTC
Read the original article
Hit count: 289
When I execute the following loop :
unsigned m;
for( m = 10; m >= 0; --m ){
printf("%d\n",m);
}
the loop doesn't stop at m==0, it keeps executing interminably, so I thought that reason was that an unsigned cannot be compared to 0. But when I did the following test
unsigned m=9;
if(m >= 0)
printf("m is positive\n");
else
printf("m is negative\n");
I got this result:
m is positive
which means that the unsigned variable m was successfully compared to 0.
Why doesn't the comparison of m to 0 work in the for loop and works fine elsewhere?
© Programmers or respective owner