Thread: C number operator question

  1. #1

    Question C number operator question

    here it is I am currently learning C by reading "Beginning C" by Ivor Horton and there is an example that is not explained as detailed as I would hope:

    #include <stdio.h>

    void main()
    int number = 0;
    int rebmun = 0;
    int temp = 0;

    printf("\nEnter a positive integer: ");
    scanf("%d", &number);

    temp = number;

    rebmun = 10 * rebmun + temp % 10;
    temp = temp/10;
    } while(temp);

    printf("\nThe number %d reversed is %d rebmun ehT\n", number, rebmun);


    that which is bolded is what I have a question on, why does C claculate the modulus before multiplying. I have a chart that explains it but it has only:

    * / % From left to right

    now from what I learned back in Algebra that meant that they were all equal and would be performed in order they appeared except that the modulus operator is located after the multiplication. Some one please explain the logic behind this.

    Give me a bad reputation!!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Actually, it goes like this:

    multiplication, then division, then addition, then subtraction.

    Thus, % is basicly division, so it is after multiplication.

    In reality, if you want to be _sure_ of what you're doing, it is a good idea to use parenthesis (Just like in algebra) for clarification.


  3. #3

    Arrow yes,but...

    That I do understand but in the book it tells me that first it calculates the modulus and it is equal to 3 and that is before it multiplies rebmun by 10. Why would it not be in reverse order?
    I know that with parenthesis it would make sense but without I am jsut confused about the example.
    Give me a bad reputation!!!

  4. #4
    I don't think that it matters in your example since the plus is the last operation.

  5. #5
    Registered User
    Join Date
    Sep 2001
    It does the modulus after the multiplication.

    This is a three step problem:

    1) rebmun is multiplied by 10.
    2) temp is taken to the modulus of 10.
    3) The results from 1) and 2) are added.

  6. #6


    ok i think i got it tell me if i am wrong:

    let's say we input 43

    -first it multiply 10 by zero(since rembun is 0)

    -then it found the remainder you get while dividing 43 by 10
    which is 3

    -then it took 43(what I inputed) and divided it by 10 and got 4

    -then it went back and took rebmun(which is now equal to 3)
    and multiplied it by ten and added 30 and 4 to equal 34

    then some how temp became 0 and it printed out the opposite of 43 as 34
    Give me a bad reputation!!!

  7. #7
    Registered User
    Join Date
    Sep 2001
    Correct -- the "somehow" of how temp becomes 0 is that in integer math, 4/10 = 0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM