-1
int main(){
    static int a[20];
    int i = 1;
    a[i] = i++;
    printf("\n %d %d %d \n",a[0],a[1],i);
    return 0;}

Output is 0,0,2 Why a[1] is not 1 as i is 1.

while if I change i with i++

  int main(){
        static int a[20];
        int i = 1;
        a[i] = i;
        printf("\n %d %d %d \n",a[0],a[1],i);
        return 0;}

output is 0,1,1

Gcc version is 6.3.0

yogesh singh
  • 103
  • 1
  • 10

1 Answers1

0

The language does not define whether a[i] is computed before or after i is incremented. Your code therefore has undefined behavior.