
在这里,我们将看到如果我们使用负数来获取模数会得到什么结果。让我们看一下以下程序及其输出,以了解这个概念。
#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.
#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
同样,如果两者都是负数,那么结果也将是负数。
立即学习“C语言免费学习笔记(深入)”;
#include<stdio.h>
int main() {
int a = -7, b = -10;
printf("Result: %d", a % b);
}Result: -7
以上就是在C语言中,负数的绝对值为正数的详细内容,更多请关注php中文网其它相关文章!
C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号