Search code examples
carrayspre-incrementoperator-precedence

In C, is { a[i] = a[++i] } equivalent to { a[i] = a[i+1]; i++;}?


In C, is

a[i] = a[++i];

equivalent to

a[i] = a[i+1]; i++;

That is, which side of the assignment is evaluated first and what value of i is used on the left side? Or is this assignment ambiguous?


Solution

  • In the same sequence point you are using and incrementing i

    a[i] = a[i++];
    

    which will lead to undefined behavior.

    a[i] = a[i+1];
    i++;
    

    is good.

    And to answer your question are they same ? No they are not !! One if well defined and another is not.

    Check this answer