# C number operator question

• 10-26-2001
unanimous
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;
do

{
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.

Sean
• 10-26-2001
quzah
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.

Quzah.
• 10-26-2001
unanimous
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.
• 10-26-2001
Unregistered
I don't think that it matters in your example since the plus is the last operation.
• 10-26-2001
The V.
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.
• 10-26-2001
unanimous
thanx
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
• 10-26-2001
The V.
Correct -- the "somehow" of how temp becomes 0 is that in integer math, 4/10 = 0