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:
#include <iostream>
using namespace std;
int main()
{
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;
return 0;
}
expected answer:
2
10
10
Actual answer from compiler:
2
-1134330576
-1134330576
Can someone explain to me where these crazy figures came from ??
but those wacky numbers had to come from somewhere right ?? i mean the compiler didnt just decide that -1134330576 was a nice negative number and lets use it??
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 i seee thanks guys :) that helped me understand it a bit better. so if i want to give c,d and e the same value will c=d=e=5; work or what should i use ??
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 . :)