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

坚强是说给别人听的谎言 提交于 2019-11-29 11:04:50

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.

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.)

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.

kainan

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

ref:

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