c - 数字,以及 () 的运算

标签 c operation integer-arithmetic

所以我在 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/

相关文章:

javascript - 猜数字 – 创建数字范围 JavaScript

c++ - 运算速度

c++ - 将未知大小的整数转换为无符号形式的最短/最干净的方法?

c - pthread 和 UDP

c - 函数调用后释放指针内存

c++ - 在 C/C++(ms) 中将 char[] 转换为 tchar[] 的最简单方法是什么?

c++ - 为什么 int 加上 uint 返回 uint?

c - 如何在二叉搜索树中找到交换节点?

javascript - 如何在 Mozilla Firefox 中使用 javascript 创建文件

c++ - 完美的正方形和完美的立方体