This code produces the (incorrect) result of 255
#include <stdio.h>
int main()
{
unsigned char x = 0;
x = (x-1)%16;
printf("%d", x);
return 0;
}
While this code produces the result 15
which I feel is correct
#include <stdio.h>
int main()
{
unsigned char x = 0;
x--;
x %= 16;
printf("%d", x);
return 0;
}
I was trying to use an underflow to my advantage when this unexpected result happened.
Problem does not occur with unsigned int
or any other unsigned integer type.