3

I have tried the following code and got confused:

#include <stdio.h>

int m=3;
int f(void)
{
    m--;
    return m;
}
int main()
{
    m=f()+m;   //as + operator is executed from left to right, it is okay that m=2+2
    printf("%d\n", m);  //m=4, as expected
    m=m+f();   //as + operator is executed from left to right, it should be like m=4+3
    printf("%d\n", m);  //but m=6
    m+= f();
    printf("%d\n", m);
    return 0;
}

plus (+) operator is commutative, but here it seemed that the order of the summands matters!!! (according to the rule of associativity)

Why m is equal to 6 after the execution of m=m+f();?

It looks like the function call will always get preference!!!!

I am not sure whether it's an undefined behavior. m=m+m-- is an undefined behavior, as far as I know, but here the decrement task has been performed in an indirect manner- by a function.

In the question "subexpressions evaluation order", the answer was that "each of func1 and func2 can, if desired, interact with some shared data without having that data change under it in an unexpected way", but here the value of m has been changed by the function f.

I tried this code later and found another interesting thing. The function evaluation precedence is not the matter here- as this shows

#include <stdio.h>

int m;

int f()
{
    m--;
    return m;
}

int main()
{
    m=4;
    int x;
    x=m+f()+f(); //if f() is evaluated first, then it should be like m=3+2+2
    printf("%d\n",x); // but m=8 
    return 0;
}
Ilya
  • 4,583
  • 4
  • 26
  • 51
Preetom Saha Arko
  • 2,588
  • 4
  • 21
  • 37
  • 6
    `+` is not executed "from left to right". In general, evaluation order is unspecified. – melpomene Aug 24 '15 at 04:41
  • In the step m=m+f() you will get 6 as output. Which is correct. Commutative rule says that "a + b = b + a". – Amit Aug 24 '15 at 04:48
  • 2
    @Amit: no; there's no rule about whether the left or right operand of `+` is evaluated first. It is entirely up to the compiler, which may do as it wishes. The behaviour of the code is indeterminate at best. I'm not sure that it isn't undefined, but that's a strong claim and I'm not making that claim yet. – Jonathan Leffler Aug 24 '15 at 04:56

1 Answers1

-1
 m=m+f();   //as + operator is executed from left to right, it should be like m=4+3
printf("%d\n", m);  //but m=6

This is correct because function call has highest precedence over + operator.

You can have a look at tbis

enter image description here

I hope this would help you.

RajSharma
  • 1,941
  • 3
  • 21
  • 34
  • thanks... I have found the table here: http://en.cppreference.com/w/c/language/operator_precedence – Preetom Saha Arko Aug 24 '15 at 07:10
  • 1
    Precedence is irrelevant here; it is the order of evaluation of the operands of `+` that matters here. And that is unspecified. – joop Aug 24 '15 at 09:44
  • no... it is undefined behaviour... as the side effect in `f()` can happen before of after the `m` evaluation in external expresion. You can evaluate `m`, then `f()` and finally `m + f()`, or you can evaluate `f()`, then `m` and finally `m + f()`. – Luis Colorado Aug 24 '15 at 10:27
  • so u mean that it's an undefined behavior? @joop – Preetom Saha Arko Aug 24 '15 at 10:32
  • 1
    No, the behaviour is defined. The order of evaluation is undefined. – joop Aug 24 '15 at 10:34