I found a very strange behavior using the modulo operator.
Given the following code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main()
{
uint8_t x = 2;
uint8_t i;
for(i=0; i<5; i++)
{
x = (x - 1) % 10;
printf("%d ", x);
}
printf("\n");
}
I expect as a result 1 0 3 4 2
, but instead I get 1 0 255 4 3
.
I think it has something to do with the integral promotion, but I don't understand how the conversion is done.