I'm pretty much assuming this is a stupid question... but I can't really find the answer for it. So I'm asking this here.
For the purpose of learning about implicit type casting, I'm running the following code on C.
#include <stdio.h>
int main()
{
unsigned char i;
char cnt = -1;
int a[255];
for (int k = 0; k < 255; k++)
{
a[k] = k;
}
for (i = cnt - 2; i < cnt; i--)
{
a[i] += a[i + 1];
printf("%d\n", a[i]);
}
return 0;
}
When I ran this program, nothing happened.
I was able to found out that the loop condition of for-loop was false at the first iteration, so the program exited the for-loop right away.
However, I don't get the reason why.
As far as I know, C does implicit casting when assigning or comparing variables with different types. So I thought that on i = cnt - 2
, the minus operation makes the value -3, and then implicit casting assigns i with a value 253.
Then, shouldn't the condition i < cnt
be true since (by another implicit casting of cnt because of comparison of signed and unsigned char) 253 is smaller than 255?
If it isn't, why is this false? Is there something that I missed or is there some exception that I don't know?