使用Modulus运算符时,函数返回奇怪的值

时间:2018-05-14 06:33:26

标签: c

它适用于前几次迭代,但之后它开始将最后一位数字改为我知道不正确的数字。这是代码:

long long isbnToArray(long long userNum)
{

int i;
char isbnArray[20];
for (i = 0; i <12; i++)
{
    long long userNumFirst;
    long long userNumRem;
    int j = 11 - i;  // gives us the power of 10 required for the divisor/ modulus operators
    long long int divR = pow(10.0, j);

    userNumFirst = userNum / divR;  // value of the first digit
    userNumRem = userNum % divR;  // remainder after division
    isbnArray[i] = userNumFirst;  // inputs the first ISBN digit value into the array
//    printf("%d.", isbnArray[i]);
    userNum = userNumRem; // sets the isbn number as the remainder for the next iteration of the loop, eg 123451234512 becomes 23451234512.

    printf("userNum = %lli\n", userNum);
}
}

在第二次迭代之后,当它应该保持为'2'时,最后的数字变为'5',然后在它之后螺旋形。我无法弄清楚原因。

运行时,值为:

userNum = 23451234512(预期)

userNum = 3451234512(预期)

userNum = 451234515(预计451234512)

userNum = 51234519(预计51234512)

userNum = 1234524(预计1234512)

userNum = 234524(预计234512)

userNum = 34524(预计34512)

userNum = 4527(预计4512)

userNum = 527(预期512)

userNum = 32(预期12)

userNum = 2(预期2)

userNum = 0(预期为0)

0 个答案:

没有答案