c modulus operator

后端 未结 5 435
独厮守ぢ
独厮守ぢ 2021-01-25 13:42

what happens when you use negative operators with %. example -3%2 or 3%-2

5条回答
  •  一整个雨季
    2021-01-25 13:48

    In C99 a % b has the sign of a, pretty much like fmod in math.h. This is often what you want :

    unsigned mod10(int a)
    {
        int b = a % 10;
        return b < 0 ? b + 10 : b;
    }
    

提交回复
热议问题