Does an int in Objective-C have a default value of 1?

我的未来我决定 提交于 2019-11-28 04:16:29

问题


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?!


回答1:


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.




回答2:


The initial value is undefined, and in this case will be whatever happened to be in that memory location before x started using it.

(Depending on the surrounding code, you might find that in your specific case it's always 1, but you can't be sure of that.)




回答3:


No, on the contrary, x has no default value at all. What you're seeing is the garbage that the variable was placed upon when you created it.




回答4:


Instance variables are initialized to 0 before your initializer runs..

ref:

  • Are instance variables set to nil by default in Objective-C?


来源:https://stackoverflow.com/questions/5980749/does-an-int-in-objective-c-have-a-default-value-of-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!