Reputation: 27
I gave a char c, value (c='1') as array index to print and it gave me a wrong output
#include<iostream>
using namespace std;
int main()
{
int a[5];
cin>>a[1]; //5
char c ='1';
cout<<c<<" "<<a[c];
}
I expect the output of 5, but the actual output is some random number
Upvotes: 0
Views: 45
Reputation: 6600
Your output code is equivalent to:
cout << c << " " << a[int(c)];
In other words:
cout << '1' << " " << a[49];
That is just an undefined behavior. Enjoy the nasal demons.
Upvotes: 2