cfor-loopcharinteger-promotionrelational-operators

comparison between signed and unsigned char


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?


Solution

  • Your question is not stupid at all. You were close to the solution: i is assigned the value -3 but the implicit conversion to the type of i, unsigned char, changes the value to 253.

    For a more precise explanation, there are multiple issues in your test code:

    You can force char to be unsigned by default by giving the compiler the appropriate flag (eg: gcc -funsigned-char) and test how the behavior changes. Using Godbolt's compiler explorer, you can see that gcc generates just 2 instructions to return 0 in the signed (default) case and the expected output in the unsigned case.