why this output ???

1
2
int a[5]={5};
cout<<a[1]<<" "<<++a[1]<<endl; 


the output is

1 1

why :(
Undefined behaviour is undefined.
And the first element of the array is at 0 index.
int a[5]={5}; namely a[0]=5 and a[1].............a[4]=0

However, the output
Because, Calling C + + from right!!!

from right: ++a[1]: 0+1 output: 1
a[1]:1 output: 1
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main(){
    int a(0);
    cout<< a <<" "<< a++ <<" "<< a++;
return 0;
}

2 1 0

a++ not equal ++a
but refer to http://www.cplusplus.com/doc/tutorial/operators/
Last edited on
@JLBorges
http://en.cppreference.com/w/cpp/language/eval_order would be a more relevant link.

@Alirez your program is undefined as well. The output is going to be completely different in different compiles, or even different versions of the same compiler. For example, on my computer, gcc prints 2 1 0, intel prints 0 0 1, and pathscale prints 2 0 1.
Last edited on
> @JLBorges http://en.cppreference.com/w/cpp/language/eval_order would be a more relevant link.

Yes. Thanks, Cubbi.

That was quite careless on my part; just blindly pasted the link from Google search results.
Topic archived. No new replies allowed.