I have this question, which i thought about earlier, but figured it's not trivial to answer
int x = x + 1; int main() { return x; }
My question is whether the behavior of the program is defined or undefined if it's valid at all. If it's defined, is the value of x
known in main
?
Use the typeof operator to check if a variable is defined or initialized, e.g. if (typeof a !== 'undefined') {} . If the the typeof operator doesn't return a string of "undefined" , then the variable is defined.
What is the difference between initialization and assignment? Initialization gives a variable an initial value at the point when it is created. Assignment gives a variable a value at some point after the variable is created.
Once initialized, a reference cannot be changed to refer to another object.
I'm pretty sure it's defined, and x should have the value 1. §3.6.2/1 says: "Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place."
After that, I think it's all pretty straightforward.
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