#include <bits/stdc++.h>
using namespace std;
int a[100]; // <--
int main() {
for (int i = 0; i < 100; i++) {
cout << a[i] << " ";
}
return 0;
}
After declaring the array globally in the above code, all the indices get the value 0. What is the reason for this?
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[100]; // <--
for (int i = 0; i < 100; i++) {
cout << a[i] << " ";
}
return 0;
}
After declaring the array inside the main function in the above code and printing the values of all the indices of the array, the garbage values at all the indices are found.
In competitive programming I have seen many declare arrays globally in their code. But I don't understand the exact reason