Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.
 int i = 2*5/2;
 int j = 2*(5/2);
 printf("%d\n,%d\n",i,j);

Variable Associativity: when I run this program, the first statement gives me i = 4; Shouldn't it give me 2, because according to BODMAS rule / is evaluated before *, so the variable should have been evaluated as (5/2)=2 and then 2*2=4?

I know this is a noob question, but this never fails to surprise me.

share|improve this question
    
Never assume that C behaves according to some math rule, it has its own operator precedence table, which is quite a lot more complex. –  user29079 Aug 8 '12 at 12:44

3 Answers 3

up vote 6 down vote accepted

The BODMAS rule is misleading. D(ivision) has equal rank with M(ultiplication). So because they have equal associativity they evalute from left to right.

Therefore 2*5/2 == 10/2 == 5.

A(ddition) and S(ubtraction) also have equal rank so the rule should more look like (B)(O)(DM)(AS). But that is less mnemonic.

share|improve this answer
1  
To be fair to the mnemonic, it's only an issue because of the integer truncation. (2*5)/2 would be the same as 2*(5/2) in the real world –  3Doubloons Aug 7 '12 at 15:18
    
Yes, you can argue that BODMAS should only be applied to mathematics as learned in school and not as used in computer languages (after all there are lots of other operators to be used, especially in C). –  bhaak Aug 7 '12 at 15:27

In C, multiplication and division have equivalent precedence. Operator Precedence

i in your example should return 5 since the operations will be
2 * 5 = 10
10 / 2 = 5

Consider it a very important lesson to use parenthesis whenever order of evaluation is critical.

Relying upon the implicit truncation of the integer value may (but probably won't) have compiler specific behavior depending upon whether the C99 standard has been implemented. Kevin Cline posted an SO link that goes into further detail.
Some folk (like me) avoid relying on functionality that can change with the compiler. Others are more familiar with the compilers they are using and safely rely upon that type of functionality. YMMV.

share|improve this answer
1  
-1: Division of integers in C is completely defined (stackoverflow.com/questions/3602827/…) Thousands of applications rely on integer division working as defined. A compiler that varied from the standard would be seriously broken. –  kevin cline Aug 7 '12 at 15:14
    
@kevincline - thanks for the clarification on compiler support, and I have updated my answer accordingly. I'll admit that I don't always dig into which level of the standard that the compiler is supporting. It's a level of detail I prefer to avoid, but I can certainly see when / where it would be used and the advantages. –  GlenH7 Aug 7 '12 at 15:30
    
The alternative to integer division is floating-point division. That requires much more care and more complicated coding to obtain correct results. –  kevin cline Aug 7 '12 at 16:34

Always put () around any expressions, even if they "should work". Make more maintainable and stable code. Same with {}.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.