Why is garbage value coming as output instead of 0 when i have already initialized the array:

CODE :

#include
using namespace std;

int main(){
int n;
cin>>n;
int a[n];
int i;
for(i=0;i<n;i++){
cin>>a[i];

}
for (i=0;i<n;i++){
    cout<<a[i]<<endl;
}
return 0;

}

INPUTS:
7
8
6
0
2

OUTPUT:
8
6
0
2
-707072336
32764
30986786

hi @akritimangal_bbde22b2109a898d
its because u have declared input of n as 7 and taking only 4 numbers input… hence it gives garbage value for the last 3 indices…

@akritimangal_bbde22b2109a898d
is there anything else??

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.