-1
int main()
{
    int arr[5]= {55}, i=0;
    while (i<5)
    {
        arr[i]= i++;
    }

    for(i=0; i<5; i++)
        printf("%d, ", arr[i]);

    return 0;
}

why is the output ( 55,0,1,2,3) Not ( 0,1,2,3,4)

Ps: I'm using GCC compiler

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Sherif Beshr
  • 23
  • 1
  • 8

1 Answers1

1

This is undefined behavior.

Clang actually warns about it

warning: unsequenced modification and access to 'i' [-Wunsequenced]
arr[i]= i++;
   ~    ^
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445