3

Given the following snippet:

- (void)doSomething
{
    NSUInteger count;
}

What is count? Is it guaranteed to be 0?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
funroll
  • 35,925
  • 7
  • 54
  • 59
  • 1
    Thanks everyone. I actually figured it would be garbage memory but explicitly asked if it was guaranteed to be 0. Because I figured that would be more useful to the next person who found this question. – funroll May 14 '13 at 15:33

1 Answers1

16

No, it isn't guaranteed to be zero, since it's a local automatic variable. Without initialization, its value is indeterminate. If you want it to be zero, either initialize it:

NSUInteger count = 0;

or define it as static:

static NSUInteger count;

since variables with static storage duration are implicitly intialized to zero, but note that this has side effects (namely the value persists between function calls).