小编gom*_*teo的帖子

带有unsigned char的Mod运算符

我发现使用模运算符的一个非常奇怪的行为.

给出以下代码:

#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");
}
Run Code Online (Sandbox Code Playgroud)

我希望结果1 0 3 4 2,但我得到了1 0 255 4 3.

我认为它与整体推广有关,但我不明白转换是如何完成的.

c modulo

5
推荐指数
2
解决办法
1841
查看次数

标签 统计

c ×1

modulo ×1