股息为零时 % 5 的意外行为

发布于 2024-09-12 22:38:58 字数 319 浏览 1 评论 0原文

我有以下代码

#include <iostream>
#include<exception>
#include <cstdlib>
int main(){
  for (int i = 0; i < 100; i++) {
    std::cout << i << " ";
      if (i % 5 == 0) {
        abort();
      }
   }
}

,但它只写 0 并说调用了 abort 为什么?我认为它应该输出 0 1 2 3 4 然后退出程序是吗?

I have following code

#include <iostream>
#include<exception>
#include <cstdlib>
int main(){
  for (int i = 0; i < 100; i++) {
    std::cout << i << " ";
      if (i % 5 == 0) {
        abort();
      }
   }
}

but it only writes 0 and says that abort was called why? i think it should ouput
0 1 2 3 4 and than exit program yes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

童话 2024-09-19 22:38:58

% 视为“除法后的余数”。 0 / 5 等于 0 余数为 0。

Think of % as "remainder after division." 0 / 5 equals 0 with a remainder of 0.

逆光下的微笑 2024-09-19 22:38:58

i为0时,0 % 5等于0

when i is 0, 0 % 5 equals 0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文