This is one of those things you simply cannot do in C. At least not without a lot of hazzle. You will have to keep track of such things yourself. So when you declare an array, then you will have to store the size and do something like this:
size_t size=1000;
int arr[size];
...
if(i>=size || i<0) {
// Handle error
} else {
// Do something with arr[i]
}
You can make abstractions and make it more Java-like with constructs like this:
struct array {
int *val;
size_t size;
};
int getVal(struct array array, size_t index) {
if(index>=array.size || index<0)
// Handle error
else
return array.val[index];
}
But if you are using constructs like that, chances are high that it might me a good idea to switch to another language instead.
If we look at your particular example:
char a[50];
fgets(a,200,stdin);
I'm sorry to say it, but it is impossible to make fgets
throw an error in this situation.