Why is the result of -8%-5 equal to -3 [duplicate] - c

This question already has answers here:
Modulo operation with negative numbers
(12 answers)
Closed 5 years ago.
I have the following code
#include <stdio.h>
int main(void)
{
printf("%d\n", -8%5);
printf("%d\n", 8%-5);
printf("%d\n", -8%-5);
return 0;
}
The output I get is
-3
3
-3
How is it that -8%5 and 8%-5 have different signs, especially when the results of -8/5 and 8/-5 have the same output ?
Also why does -8%-5 give -3 as the output, when -8/-5 gives it's output as 1 ?

If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined.

Related

sizeof() in C conditional operator [duplicate]

This question already has answers here:
How to compare signed and unsigned (and avoiding issues)
(1 answer)
Why is this happening with the sizeof operator when comparing with a negative number? [duplicate]
(2 answers)
Closed 11 months ago.
#include<stdio.h>
int main(){
int i;
// printf("%d",sizeof(i)) ;
printf("%d",(sizeof(i) > (-1))) ;
return 0;}
why does the code print 0 when sizeof(i) gives 4 in 64 bit OS?
why does (sizeof(i) > (-1))) gives false(0) ?
Use a better compiler and enable warnings. Under any sane compiler you should have gotten a warning about comparing an unsigned and a signed value.
This should be closer to what you want:
printf("%d", (int)sizeof(i) > -1);
Or at least this:
printf("%d", sizeof(i) >= 0);
However your code is a no-op anyway, because it's impossible to have a negative size of a type.

What is the outcome for the arithemic in C? [duplicate]

This question already has answers here:
How does the bitwise complement operator (~ tilde) work?
(18 answers)
C Unsigned int providing a negative value?
(3 answers)
Closed 2 years ago.
There is an interview question in C as below.
int main()
{
unsigned int a = 9;
a = ~a;
printf("%d\n", a);
}
I though it was supposed to be 6 but it is -10.
~a is assinged back to an unsigned integer then printed out.
It should not be a negative value.
Isn't it? Hoe come?

What is wrong with this expression on the code? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I have written this piece of code in my computer and the result is 7 instead of 8 (the correct result ... I think).
I don't know why... Can somebody help me?
#include <stdio.h>
int main() {
int num;
num = (68/10.0 - 68/10)*10;
printf("the result %d", num);
return 0;
}
double typically represents exactly about 264 different numbers. 68/10.0 is not one of them,
As a binary64, 68/10.0 is about
6.7999999999999998223643161..., the closest value to 6.8 that is a multiple of a dyadic rational. # AntonH
68/10 is an integer division with a quotient of 6.
(68/10.0 - 68/10)*10 is thus about 7.9999999999999982236431606...
Assigning that to an int is 7 not 8 as the fraction is discarded even though it is very close to 8.
When converting a floating point value consider round to the the closest, rather than truncating.
num = lround((68/10.0 - 68/10)*10);

how - 5%3 is equal to - 2? [duplicate]

This question already has answers here:
Modulo operation with negative numbers
(12 answers)
Closed 7 years ago.
I'm learning C basics right now. I have a question which is confusing me little bit.
My question is how the below program's output is - 2 ?
#include<stdio.h>
int main()
{
printf("%d", -5%3);
return 0 ;
}
The % operator is gives you the remainder left after of integer division.
Then -5/3 = -1 with -2 as remainder of division as 3*(-1)=-3 and -5-(-3)=-5+3=-2.

The sign of the modulo operator result? [duplicate]

This question already has answers here:
Modulo operation with negative numbers
(12 answers)
Problem using modulo with negative numbers in decryption program
(2 answers)
Closed 4 years ago.
I have couple line of C code testing the modulo operator as follows:
// line 1
printf("%d\n", 5 % (-3)); => output: 2
// line 2
printf("%d\n", -5 % 3); => output: -2
I know that the sign of the modulo depends on the sign of the numerator, but I am curious why not otherwise?
5/(-3) = -1;
(-5)/3 = -1;
If that is agreed then let's calculate the remainder
Remainder = Dividend - ( Divisor * Factor)
5 - (-3 * -1) = 2
-5 - (3 * -1) = -2

Resources