c++ - c 中的模运算符

标签 c++ c math modulus

我需要检查 c 中数字的整除性。如何使用 C 中的模数 operatpr 来检查一个数是否可​​以被另一个数整除?我试过这样做:

if (file_int % 3) {
    printf("Hoppity \n");
}

它没有用,尽管 file_int 等于 9。

我做错了什么?

谢谢

最佳答案

它不起作用,因为该操作将返回 0,这将被视为 false。

你实际上需要:

if(!(file_int % 3)) {
    printf("Hoppity \n");
}

关于c++ - c 中的模运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8673278/

相关文章:

jquery - mouseMove 上的水平滚动 - 较小 div 中的宽 div 并溢出 :hidden (Can't get the math to work)

c# - 小数点后四舍五入

algorithm - 计算窗框(修剪)的有效使用

c++ - 列表迭代器不可取消引用

c++ - 从 std::cin 读取输入两次

c++ - 作为参数传递的字符指针转换为字符串

c - 当可以使用指针时,使用 malloc 有何意义?

c++ - 编译器提示类型冲突

c++ - 这个makefile有什么问题

c - 使用信号量交换两个 fork 进程