# Thread: List of precedence problem

1. ## List of precedence problem

Hi. I have tried an arithmetic question, but I cannot get the same final answer with the computer output. Here is the code.

int p=1;
int r=25;
int g=2;
int t=3;
int k;
k= p++ + --r/g++ - ++t%g++;

1) g++ --> g = 2+1 = 3.
2) t % g = 3%3 = 0
3) ++ t%g++ --> 0 becomes 1
4) g++ --> g = 3+1 = 4;
5) r/g = 25/4 = 6;
6) --r/g++ --> 6 - 1 = 5;
7) p++ --> 1 + 1 = 2

Then the k will be 2 + 5 - 1 = 6.However the C compiler gives me K = 13.Can anyone expert guide me where have I done wrongly? Thanks.

2. Originally Posted by ericjoe
Hi. I have tried an arithmetic question, but I cannot get the same final answer with the computer output. Here is the code.
Code:
```int p=1;
int r=25;
int g=2;
int t=3;
int k;

k= p++ + --r/g++ - ++t%g++;```
Don't have code like this. Lay out the precedence you want followed, with parentheses, and break it up over multiple lines of code.

Think of someone wanting (perhaps you in 5 years) to extend or modify this code. Think of the difficulty of mapping out the precedence inherent in that line, and then multiply it by perhaps a hundred or a thousand lines of code.

Writing code like that is just kicking yourself in the butt.

Code:
```int p=1;
int r=25;
int g=2;
int t=3;
int k;

k= p++ + --r/g++ - ++t%g++;```
Don't have code like this. Lay out the precedence you want followed, with parentheses, and break it up over multiple lines of code.

Think of someone wanting (perhaps you in 5 years) to extend or modify this code. Think of the difficulty of mapping out the precedence inherent in that line, and then multiply it by perhaps a hundred or a thousand lines of code.

Writing code like that is just kicking yourself in the butt.

Or somebody else in the face

4. Yeah! Hey intern, I need you to ...

5. > k= p++ + --r/g++ - ++t%g++;
You should either slap your "teacher" for not having a clue about C programming, or throw away your book.
Both are wrong if they think they can assert an answer from that expression.