-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
  • 181,601
  • 45
  • 354
  • 430
Sherif Beshr
  • 13
  • 1
  • 4

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
  • 181,601
  • 45
  • 354
  • 430