Thread: simple modulus question

  1. #1
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72

    simple modulus question

    Why is this printing 0 instead of 5? Doesn't the modulus operator
    result in the remainder after a division?

    Code:
    int main(void)
    {
        int c = 0;
        int remainder = c % 5;
    
        printf("%d\n", remainder);
        return 0;
    }
    thanks
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    thanks laserlight.


    I think I got it (maybe).
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    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

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > 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).

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    If you / or % by 0, the result is undefined (anything can happen).
    Yes, I stand corrected =)
    sorry, I was thinking in java.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM