Why is this printing 0 instead of 5? Doesn't the modulus operator
result in the remainder after a division?
thanksCode:int main(void) { int c = 0; int remainder = c % 5; printf("%d\n", remainder); return 0; }
This is a discussion on simple modulus question within the C Programming forums, part of the General Programming Boards category; Why is this printing 0 instead of 5? Doesn't the modulus operator result in the remainder after a division? Code: ...
Why is this printing 0 instead of 5? Doesn't the modulus operator
result in the remainder after a division?
thanksCode:int main(void) { int c = 0; int remainder = c % 5; printf("%d\n", remainder); return 0; }
Remember that all that code you write turns into this:
0100100100110010010011100100111001001
0010100100100001001111100010010010010 ....
Suppose that the remainder of dividing 0 by 5 is 5.
If this is true, then here's a simple get rich quick scheme: let n be the number of people in the world. Divide $0 by n. You get to keep the remainder, $n. You now have about 6 billion dollars in your hand. Repeat until you are satisfied.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
thanks laserlight.
I think I got it (maybe).
Remember that all that code you write turns into this:
0100100100110010010011100100111001001
0010100100100001001111100010010010010 ....
the first operand is divided by the second operand to produce the remainder (eg, in this case, 0 is divided by 5, which results in "0 R 0")
if you switch the two operands around (ie, 5 % 0) you will get a divide-by-zero error, but theoretically I guess 5 should be returned
> if you switch the two operands around (ie, 5 % 0) you will get a divide-by-zero error, but theoretically I guess
> 5 should be returned
If you / or % by 0, the result is undefined (anything can happen).
Yes, I stand corrected =)If you / or % by 0, the result is undefined (anything can happen).
sorry, I was thinking in java.