您好,欢迎访问一九零五行业门户网

在C语言中,负数的绝对值为正数

在这里,我们将看到如果我们使用负数来获取模数会得到什么结果。让我们看一下以下程序及其输出,以了解这个概念。
示例#include<stdio.h>int main() { int a = 7, b = -10, c = 2; printf("result: %d", a % b / c);}
输出result: 3
here the precedence of % and / are same. so % is working at first, so a % b is generating 7, now after dividing it by c, it is generating 3. here for a % b, the sign of left operand is appended to the result. let us see it more clearly.
example#include<stdio.h>int main() { int a = 7, b = -10; printf("result: %d", a % b);}
输出result: 7
如果我们交换a和b的符号,那么它将变成以下内容。
示例#include<stdio.h>int main() { int a = -7, b = 10; printf("result: %d", a % b);}
输出result: -7

同样,如果两者都是负数,那么结果也将是负数。
示例#include<stdio.h>int main() { int a = -7, b = -10; printf("result: %d", a % b);}
输出result: -7

以上就是在c语言中,负数的绝对值为正数的详细内容。
其它类似信息

推荐信息