5
int i=-1;
int a=65;
int b=a*i + ++i;

What is the value of b? Here associativity of =,+ is left to right and associativity of *,prefix increment (++) is right to left.

So What order of evaluation should I consider for int b=a*i + ++i;

left to right? right to left? Why?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Abdus Sattar Bhuiyan
  • 3,016
  • 4
  • 38
  • 72

3 Answers3

5

Do not think about assosiativity. Think about order of evaluation and unfortunately it is undefined in this case.
In the expression

int b=a*i + ++i;   

you are modifying i as well as using it in the same expression which invokes undefined behavior.

C99 states that:

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.

Further I would suggest you to read c-faq: Q 3.8.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    A statement doesn't _invoke_ undefined behavior. A statement _has_ undefined behavior. "Undefined behavior" is not a function that gets invoked. – Shahbaz Mar 19 '14 at 17:11
  • yes, _cause_ is also fine. Sorry for nitpicking, but I'd seen people say _invokes undefined behavior_ so many times... – Shahbaz Mar 19 '14 at 17:13
  • 1
    @Shahbaz [invoke](http://www.merriam-webster.com/dictionary/invoke) has a plain English meaning which makes perfect sense in that context. – Shafik Yaghmour Mar 19 '14 at 17:18
  • You _did_ use that term didn't you? ;) Never mind though, I only said it to prevent confusion. With _invokes undefined behavior_ it sounds like (to a noob) that the compiler or whoever deliberately goes and invokes something called undefined behavior when it sees code like that. – Shahbaz Mar 19 '14 at 17:18
  • 1
    @Shahbaz the standards committee uses such language as well, for example this [defect report](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1719.htm) so I would consider this a term of the field and explain it is needed to anyone who does not understand. – Shafik Yaghmour Mar 19 '14 at 17:28
  • 1
    @ShafikYaghmour; Thanks for clearing up the things. – haccks Mar 19 '14 at 17:30
  • @ShafikYaghmour, interesting. I'm not a native speaker, so perhaps I'm not clear on all usages of invoke (I thought it just means _call upon_). Nevertheless I had just asked [a question](http://english.stackexchange.com/q/158556/14369) on english.SE. – Shahbaz Mar 19 '14 at 17:31
  • @Shahbaz; I am waiting for the answers. – haccks Mar 19 '14 at 17:34
  • 2
    @Shahbaz: "Invokes undefined behaviour" is a (even 'the') conventional way to describe what happens when a program does something that the standard specifies as leading to 'undefined behaviour'. I use the term; I am going to continue using the term. It is more convenient than most of the circumlocutions. – Jonathan Leffler Mar 19 '14 at 23:28
2

here you should consider the operator precedence. here the expression is solved based on precedence of the operator so at a time only one associativity is applied.
The associativity comes into picture when the operator has same precedence and in that case associativity will be same.
In your question b=a*i + ++i i used twice results in undefined behavoiur.

otherwise it would be evaluated as,
++ followed by * then + operation according to precedence. if this was followed the the answer would be,

b=a*i + ++i
b=65*i + 0
b=65*0 + 0
b=0 + 0
b=0

but the compiler can use the stored value of i instead of using value after ++i,

b=a*i + ++i
b=a*-1 + 0
b=-65 + 0
b=-65

this results in undefined behaviour since b can be 0 or -65. and also you can see that associativity doesn't create problem while evaluating since operators with same precedence will have same associativity and evaluation is ordered by precedence first and within that ordered by associativity.

LearningC
  • 3,182
  • 1
  • 12
  • 19
-1

There are two things associativity and precedence .

First check precedence and then associativity.

In you expression int b=a*i + ++i;

++has higher precedence than + and * so first ++ would be evaluated. After that + and *, * has higher precedence so * is evluated. In the last + is evaluated.

It's also the case as haccks suggested, you should not modify and use the value in the same expression.(not a good practice)

Jatin Khurana
  • 1,155
  • 8
  • 15
  • So, according to your answer, if the expression would be `b = a*i + ++c;` then `++c` get evaluated first and then `a*i` and the result of both get added? – haccks Mar 19 '14 at 17:27
  • yes .... If you put the expression like that b=a*i + (++c) otherwise it would take the ++ with i..... I means to say only one thing.... first it would take ++(increment) before + and *(binary operator). And also one more thing... unary operator always has higher precedence than binary operator. – Jatin Khurana Mar 19 '14 at 17:36
  • Then this is alarming! You need to revise your basics. – haccks Mar 19 '14 at 17:38
  • it depends on associativity and also on compiler for gcc – asifaftab87 Mar 20 '14 at 11:26