Hey guys so i was practicing some basic c++ and i think i came across a compiler error but im not 100% sure. I typed down my code and expected a result of 2 and then 2 results of 10 but i got bizare results instead.
Heres my code:
using namespace std;
int a , b;
a = 5 - (b = 3);
cout << a << endl;
int c , d , e;
c = 10;
c = d = e;
cout << e << endl;
cout << d << endl;
Actual answer from compiler:
Can someone explain to me where these crazy figures came from ??
Yeah, but it's undefined. There's no way of knowing why it's that number. Maybe it's left over from some previous data that was at the location that's now being used for your uninitialised variable. Maybe it's some strange number your specific compiler writes to unitialised memory.
As far as you're concerned, it might as well be random. That's what you get when you don't initialise variables - undefined behaviour.
It's nothing to do with the compiler 'deciding numbers'.
you ask the compiler for an int called i for example and it'll give you a bit of space for it in memory. it is YOUR responsibility to give it an initial value. if you dont it'll use the value that's already at that memory location.
okay thanks mikey and the rest of you. great help thanks guys. im only new and decided to learn c++ to start making games. i know that im a long way of but i think its a cool idea to understand a coding language to some extent . :)