小编SA6*_*610的帖子

C 中负数的模

在 C 语言中,以下代码...

#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<stdlib.h>

int main() {
   int result = (-12) % (10);
   printf("-12 mod 10 = %d\n",result);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

给出这个输出

> gcc modTest.c
> ./a.out
-12 mod 10 = -2
Run Code Online (Sandbox Code Playgroud)

但根据这个 mod 计算器-12 mod 10 = 8

在Python中...

> python
Python 3.3.0 (default, Mar 26 2013, 09:56:30)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> (-12) % (10)
8
Run Code Online (Sandbox Code Playgroud)

C 中发生了什么,产生 -2 而不是 …

c negative-number modulus

5
推荐指数
1
解决办法
3587
查看次数

标签 统计

c ×1

modulus ×1

negative-number ×1