所以我在 C 中的运算符逻辑上遇到了问题。我不知道编译器如何运行那些(%)/?
#include <stdio.h>
int main (){
int number1=1606,number2,number3,number4;
number2 = number1/5000;
number3 = (number1%5000)/1000;
number4 = (number1%5000)%1000/100;
printf("%d\n%d\n%d\n%d",number1,number2,number3,number4);
return 0;
}
所以我不明白那个数字3? 不是 1606%5000 = 3212 然后/1000 = 3 吗?所以我从中得到 1 它是如何工作的?
最佳答案
在此声明中
number3 = (number1%5000)/1000;
这里使用了整数运算。运算符 % 产生运算/的余数。
因此子表达式 number1%5000
给出值 1606
因为
number1 可以表示为
number1 = 0 * 5000 + 1606.
将余数除以1000
,您将得到1
。
来自 C 标准(6.5.5 乘法运算符)
5 The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.
关于c - 数字,以及 () 的运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58593632/