The name of this is initialization. So you initialize the variable:
int sum = 0;
That is, initialization occurs when you declare a variable already assigning it an initial value.
Already this way, you declare it without initializing:
int sum;
In his for
, you have it:
sum = sum + die1[i] + die2[i];
That is, it modifies the value of sum
based on the previous value. But, in the first iteration of this for
, if you stated sum
without initializing, what would be her first value?
The answer is that its initial value is anything. Often this "anything" is zero, but may not be. Often this value is called by the affectionate name of "rubbish".
Okay, but maybe you’re thinking, why does C have this bizarre behavior? The answer is in the way that memory is organized.
In C, each variable is allocated somewhere in memory. When a program is running, a certain part of it can use some portion of the memory to do something and as soon as it finishes, it frees up that portion of memory. It happens that releasing is not cleaning. The old values of the function/procedure/some-other-thing that has ended get dropped there in memory. After some time, another part of the program allocates that memory region (which is dirty) again and starts using it.
That is, what happens is that the original content of the variable sum
is dirty, and contains information that is remnant of some other part of something else that was using that piece of memory for some other purpose.
And then comes the question: Why is it not clean when the variable sum
is allocated? The answer is because this is almost always unnecessary. That’s what booting is for. The initialization is not only to define what is the initial value of the variable, but it is also to delete/overwrite any junk that is there. And then you ask, why isn’t he clean anyway? The answer is because it would mean that it could end up being cleaned twice, one automatically and one by initialization or by first assigning a value to variable, which means that if the compiler generated code to wipe the variable automatically before using, the result would be a waste of performance.
And why didn’t whoever used it last leave it clean? The answer is because this code, whatever it is, didn’t have it cleaned! And cleaning should be unnecessary, since anyone who uses that part of the memory then would probably/should overwrite its content with something else.
In conclusion, using dirty values of memory is a bad programming practice, it is considered a bug. In practice, you can’t know for sure what the old value is, and whatever it is, it probably wouldn’t be any useful value. This value is garbage, dirt, something about which we know nothing and which we cannot control and which is therefore something useless. Never use dirty memory values.
Finally, several code analysis tools will generate alerts warning that the variable sum
may not be properly initialized - Maybe the compiler itself is giving you a warning/warning (Warning) about this problem.