c - 为什么-8%-5的结果等于-3

标签 c

我有以下代码

#include <stdio.h>

int main(void)
{

    printf("%d\n", -8%5);


    printf("%d\n", 8%-5);

    printf("%d\n", -8%-5);


    return 0;
}

我得到的输出是

-3
3
-3

-8%5 和 8%-5 的符号为何不同,特别是当 -8/5 和 8/-5 的结果具有相同的输出时?

另外,为什么 -8%-5 给出 -3 作为输出,而 -8/-5 给出的输出为 1 ?

最佳答案

如果两个操作数均为非负,则余数为非负;如果不是,则余数的符号是​​实现定义的。

关于c - 为什么-8%-5的结果等于-3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42442417/

相关文章:

c - ANSI C : How to convert a float value into a short variable

处理文件时的困惑

c - 与 gd php 扩展交互

c - C中静态函数的正确用法是什么?

c - 如何在 C 中的字符串中存储路径名?

C - 为什么向字符添加数字会重置为 255?

c - 链表中 Next 指针的大小?

c - 为什么 while(true) 是无限循环?

c - fork 和信号 : how to send signals from parent process to specific child process

java - 调用由 SWIG 生成的 JNI 的 UnsatisfiedLinkError?