Home  >  Article  >  Backend Development  >  In C language, the absolute value of a negative number is a positive number

In C language, the absolute value of a negative number is a positive number

WBOY
WBOYforward
2023-08-30 10:41:05969browse

In C language, the absolute value of a negative number is a positive number

Here we will see what we get if we use negative numbers to get the modulus. Let us look at the following program and its output to understand this concept.

Example

#include<stdio.h>
int main() {
   int a = 7, b = -10, c = 2;
   printf("Result: %d", a % b / c);
}

Output

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);
}

Output

Result: 7

If we swap the signs of a and b, then it will become the following.

Example

#include<stdio.h>
int main() {
   int a = -7, b = 10;
   printf("Result: %d", a % b);
}

Output

Result: -7

Similarly, if both are negative, the result will also be negative.

Example

#include<stdio.h>
int main() {
   int a = -7, b = -10;
   printf("Result: %d", a % b);
}

Output

Result: -7

The above is the detailed content of In C language, the absolute value of a negative number is a positive number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete