0

Why is b starting at 9 instead of 1?

I have this very simple incremental code that is printing the value of a,b,c. The out put I get is starting like this: A = 1 B = 9 C = 1 A = 2 B = 10 C = 2 So on till a equals 10. Actual code: #include <iostream> using namespace std; int main() { int a,b,c; while(a<10) { a++; cout <<"a = "<<a<<endl; b++; cout <<"b = "<<b<<endl; c++; cout <<"c = "<<c<<endl; } return 0; } So why is b starting at 9? When the code is identical to a & c?

12th Apr 2018, 12:19 PM
James Davis
James Davis - avatar
8 Answers
+ 1
Maybe this clears it up what exactly is happening: You probably already know that different variable types use different amount of bits. I made a small code to compare the garbage values of uninitialized variable for different types: https://code.sololearn.com/cGAnpXg5ECzU/?ref=app
12th Apr 2018, 3:11 PM
Alex
Alex - avatar
+ 3
If you dont initialize variables they are going to get indeterminated values. You should init a, b and c to 0.
12th Apr 2018, 12:26 PM
J Domingo Jiménez J
J Domingo Jiménez J - avatar
+ 3
The location on the stack of variables (a,b,c) remains the same every time you run your program, and the steps performed by the run time are the same each time, so the values left behind on the stack are the same
12th Apr 2018, 2:14 PM
J Domingo Jiménez J
J Domingo Jiménez J - avatar
+ 2
Indeterminated value, NOT random. It will have some value, but not a predictable one.
12th Apr 2018, 2:08 PM
J Domingo Jiménez J
J Domingo Jiménez J - avatar
+ 2
static int is actually initialized with a default 0, but even then you should assign the 0 to it to make it more readable. The value which gets assigned to the variable is determined by the bits which were sitting inside the unused memory location which is now the memory location of your declared variable. Whatever is in there is now assigned to the uninitialized variable. I honestly don't know the reason why the values are always the same for every run, but I can assure you that the behaviour differs a lot with different systems, compilers, other processes running, etc. My guess for these specific values on sololearn is that the process before actually running the main allocates some memory, does some starting stuff and frees a part of it and then, in the main, the variables get assigned on these memory locations first. But that's just a guess. Actually it doesn't really matter. You just need to know that it's bad to do it. The value of unassigned variables has a lot of influences.
12th Apr 2018, 2:12 PM
Alex
Alex - avatar
+ 1
Thanks for the answers. I haven’t initialized them because I was under the impression that if I didn’t they would either assume a default value or not have one at all. Does anyone know why if uninitialized they are assigned a “random” value at all and why it returns the same random value when I run the program?
12th Apr 2018, 1:57 PM
James Davis
James Davis - avatar
0
I usually do initialize every thing as I like to know exactly what the program is doing. But in my original code I had missed out b&c that appear later in the code by accident and ran the code and was confused by the fact a which was initialized matched c but b did not. so I removed all other code to what you see here. As I couldn’t understand why b wasn’t acting the same as a&c. I posted this to find out.
12th Apr 2018, 2:25 PM
James Davis
James Davis - avatar