Search code examples
objective-cintlocal-variablesvariable-initialization

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


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


Solution

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