Static variables can also be declared at local scope. static
duration means that the object or variable is allocated when the program starts and is deallocated when the program ends. ( from here )
So what this means for your example program is that 2
is correct:
The following example code, coupled with the statement at top illustrates why:
Initial call:
static int x = 0; // unlike non-static variables, initialization is executed
// only once on 1st call to function.
// Also unlike non-static, local variables, locally created static
// variables are initialized to 0 automatically
// making the explicit assignment to 0 unnecessary.
// (although still a good habit. :))
...
x++; //x==1
2nd call:
static x = 0 //skipped
...
x++; //x==2
3rd call:
static x = 0 //skipped
...
x++; //x==3
And so on, until end of program...
(Having replaced x += 1;
with its idiomatic shortcut syntax: x++;
)