Reputation: 1
#include <stdio.h>
int main() {
int a[]={1,-2,3,0,-5};
int i=0;
while(i++<5){
a[i]=a[i]>a[i-1]?-1:1;
}
for(i=0;i<5;i++) printf("%d",a[i]);
return 0;
}
I think it should return an error because of out of bounds i which is 4 before increment but 5 in the loop. Yet it returns 11-1-11 which is correct.
Upvotes: 0
Views: 67
Reputation: 67845
C language does not check the indexes when you access the array. If you access the array out of bounds it invlokes Undefined Behaviour (UB).
UB may or may not express itself in some way. It can segfault or work "OK". As a programmer, you are responsible for ensuring that arrays are correctly accessed.
Upvotes: 1