Question

Given the following snippet:

- (void)doSomething
{
    NSUInteger count;
}

What is count? Is it guaranteed to be 0?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top