I was trying to play with array initializations in a for loop in c.
Here is the program I tried :
#include<stdio.h>
int main(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int br[10]={0};
printf("%d\n", br[-1]);
}
}
return 0;
}
The results of this code when compiled with gcc are:
0 1 2 0 1 2 0 1 2
The same program when compiled with clang ,the results are:
0 0 0 0 0 0 0 0 0
If I tweak the program a bit by making a array initialization before starting of second loop:
#include<stdio.h>
int main(){
for(int i=0;i<3;i++){
int ar[10]={0};
for(int j=0;j<3;j++){
int br[10]={0};
printf("%d\n", br[-1]);
}
}
return 0;
}
I get output as 0 0 0 0 0 0 0 0 0
for both gcc and clang
Can anyone explain what exactly is happening here,when I try to access a negative index why are these results showing up with these two different compilers(gcc and clang) and why is another array initialization before second loop changing everything.