Doubt in C program

Why does the compiler not show any error in the given program as i have declared an array having size 2 and using more than 2? Mostly array starts from 0 or 1 but i have started it from 2.
#include <stdio.h>
int main(void) {
int a[2],i;
for(i=2;i<=10;i++)
scanf("%d",a+i);
for(i=2;i<=10;i++)
printf("%d\t",a[i]);
return 0;
}

What output did you get?
It shows runtime error on ideone.

An array access out of bounds is Undefined Behaviour, meaning anything can happen: the program could crash; the program could continue to run to the end without any errors; your harddrive could be formatted; or demons could fly out of your nose.

3 Likes

I am using turbo C. I got the output as I have declared an array of size 10.

You’re only compiling the program. There’s no compile time error. But you are guaranteed to get a runtime error

No I didn’t get any error. I have run the program in turbo C and also in codechef.

Try running your code in ideone.com

Ok!! Now i get the exact answer.

1 Like

Yes you’re right. It will not execute in every compiler. Thanks to all of you!

1 Like