0

I ran the following code through gcc based C compiler:

int main() {
    int i = 0;                              // Line 1
    static int a[10];                       // Line 2
    
    a[i] = ++i;                             // Line 3
    
    printf("%d %d", a[0], a[1]);            // Line 4
    
    return 0;                               // Line 5
}

As per the precedence table provided in the textbook "The C Programming Language by Brian Kernighan and Dennis Ritchie" and the website: https://en.cppreference.com/w/c/language/operator_precedence, the precedence of square brackets (array subscript operator) is higher than that of prefix operator ++.

On running the program, the output i receive is: a[1] = 1.

I don't understand why the sequence of operations is not as follows:

  1. a[0]                 (put i = 0)
  2. ++i                 (increment i by 1)
  3. a[0] = 1           (assign value of i to a[0])

and instead works as

  1. ++i                 (increment i by 1)
  2. a[1]                 (put i = 1)
  3. a[1] = 1           (assign value of i to a[1])

breaking the precedence rule. Please help me know where I'm going wrong.

Akash Sen
  • 1
  • 1
  • 4
    Does this answer your question? [Undefined behavior and sequence points](https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) Check "Example 2" in the top answer. –  Jul 15 '21 at 17:53
  • 6
    Precedence is not involved at all here. – Gerhardh Jul 15 '21 at 17:54
  • `a[i] = ++i` is an unsequenced modification according to clang. – jwdonahue Jul 15 '21 at 18:03
  • @StaceyGirl Thanks a lot! I was exactly looking for an explanation like that. The link pretty much explains about the undefined behavior of the language over such situations. Understood now. – Akash Sen Jul 15 '21 at 18:13
  • @Akash Sen - While Gerhardh is right saying _Precedence is not involved at all here_, this is probably too laconic to enlighten you. Precedence matters if operators are "competing" for an operand. E. g. in `++a[i]`, the precedence rule causes this to be equivalent to `++(a[i])`, as you'd expect. In your `Line 3` there's no competition of operators, thus _Precedence is not involved_. – Armali Jul 15 '21 at 18:15
  • 2
    @Armali Thanks for clearing my idea about what precedence actually means. I pretty much can see what Gerhardh wished to point out. – Akash Sen Jul 15 '21 at 18:25
  • 1
    The evaluations of `a[i]` and `++i` are *unsequenced* relative to each other - they can be evaluated in any order, even simultaneously (interleaved or in parallel). With a few exceptions, C doesn't force left-to-right evaluation of expressions. Similarly, the side effect of `++i` doesn't have to be applied immediately after evaluation. Thus, the result can vary from compiler to compiler, even from run to run. The language definition explicitly calls this out as *undefined behavior* - neither the compiler nor the runtime environment are required to produce a specific result or action. – John Bode Jul 15 '21 at 18:27

0 Answers0