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

前端 未结 4 1305
刺人心
刺人心 2020-12-19 13: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 s

相关标签:
4条回答
  • 2020-12-19 13:44

    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.

    0 讨论(0)
  • 2020-12-19 13:45

    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.

    0 讨论(0)
  • 2020-12-19 13:51

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

    ref:

    • Are instance variables set to nil by default in Objective-C?
    0 讨论(0)
  • 2020-12-19 14:06

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

    0 讨论(0)
提交回复
热议问题