This is a follow-on from a discussion, which I think deserves a question of its own.
Basically, is the result of this undefined?
int x;
int y = 1 || x;
There are two "common-sense" arguments here:
x
, the value of y
should be 1
.x
is never evaluated anyway.But the counterargument is that we have an expression that involves an uninitialized variable, so all bets are off (in theory).
More generally, if the value of an uninitialized variable can't possibly affect the result of an expression, is it "safe"? e.g.:
int x;
int y = x - x;
Usual disclaimer: Of course, I'm not advocating ever writing code like this.
Initializing a variable means specifying an initial value to assign to it (i.e., before it is used at all). Notice that a variable that is not initialized does not have a defined value, hence it cannot be used until it is assigned such a value.
An uninitialized variable has an undefined value, often corresponding to the data that was already in the particular memory location that the variable is using. This can lead to errors that are very hard to detect since the variable's value is effectively random, different values cause different errors or none at all.
An uninitialized variable is a variable that has not been given a value by the program (generally through initialization or assignment). Using the value stored in an uninitialized variable will result in undefined behavior.
In computing, an uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one. As such, it is a programming error and a common source of bugs in software.
In C, it is undefined behavior to use the value of an object with automatic storage duration while it is indeterminate. (J.2, informative) but it's OK for variables with automatic storage duration to hold an indeterminate value.
An expression can only have its value used if it is evaluated and according to 6.5.12 (Logical OR operator) the second operand is not evaluated (let alone have its value used) if the first operand compares unequal to 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With