logo codesdope

doubt in array article


Do all of the uninitialised elements of the array are assigned to be zero if some elements are initialised?

In the following code , i have declared array of size 3

#include <iostream>
int main(){
  using namespace std;
  int a[3];
  a[2] = 2;
  cout << a[0] << '\n' << a[1] << endl;
  return 0;

output that i got was

a[0] = garbage value like 4196688

a[1] = 0


  •    Amit Kumar , I.I.T. Patna

    Yes, it may take some garbage value.

    • but according to array article, both a[0],a[1] should be zero
      - amankh_99
    • Read http://stackoverflow.com/questions/6032638/default-variable-value/#answer-6032889 to understand when the unitialized value of a variable is 0 and when it is not.
      - Amit Kumar

Share what you know
Write your answer

Welcome.please sign up.


Welcome.please login.