-7

When I tried to find the behavior of increment operators I got this result which I ended up wit two questions. Here is the code and output.

#include <iostream>
using namespace std;
int main(){

    int arr[4] ;
    int x =0;
    cout << arr[0] <<"_"<< arr[1]<<"_"<<arr[2]<<"_"<<arr[3]<<"**"<< x<< endl;
    arr[x] = 10;
    cout << arr[0] <<"_"<< arr[1]<<"_"<<arr[2]<<"_"<<arr[3]<<"**"<< x<< endl;
    arr[x++] = 20;
    cout << arr[0] <<"_"<< arr[1]<<"_"<<arr[2]<<"_"<<arr[3]<<"**"<< x<< endl;
    arr[++x] = 30;
    cout << arr[0] <<"_"<< arr[1]<<"_"<<arr[2]<<"_"<<arr[3]<<"**"<< x<< endl;
    arr[x] = arr[x--] + arr[--x];
    cout << arr[0] <<"_"<< arr[1]<<"_"<<arr[2]<<"_"<<arr[3]<<"**"<< x<< endl; 

    return 0;
}

the result was

-1_-1_4254245_0**0
10_-1_4254245_0**0
20_-1_4254245_0**1
20_-1_30_0**2
50_-1_30_0**0
  • Q:1 what are those default values of the array?
  • Q:2 how 50 came as the vale of the zeroth position of the array ?
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
captain_999
  • 45
  • 1
  • 1
  • 10

1 Answers1

0

Garbage values. These can either be preinitialized values that were just laying around from previous data that was in memory and now allocated to your array, or simply garbage. In C you must initialize all your values if you want something there unlike Java, which initializes to zeros.

mks
  • 199
  • 1
  • 15