I have this simple line of code:
int x;
x
automatically has the value of 1. I don't set it to anything but when I debug, it shows that x
is 1.
Does an int
have a default value of 1?!
Variables of type "int" have a default value of 0.
It is initialized to garbage. However, for a BOOL ivar, it will be initialized to NO , as the whole instance is filled with 0 on initialization. (Note: When ARC is enabled, local object pointers will always be have a default value nil , but local variables of non-object types like BOOL are still initialized to garbage.
Well, int's are value types in both c++ and C#, they use X bytes of ram, and all 0 bits, they are 0. So whether you initialize them or not, in memory they are still 0.
No. int
has an undefined default value. It just happens to be 1
in this case. It could just as easily be -18382
or 22
or 0xBAADF00D
.
Always initialize your variables in C.
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