C/C++ static local variable and initialization

Content:

Original link: C++ reminder: Function-local static variables are initialized only once, even if it looks like they get initialized multiple times / The Old New Thing / Microsoft.

To show it with much simpler code:

#include <stdio.h>

void f()
{
    static int v = 0;
    v++;
    printf("v = %d\n", v);
}

int main()
{
    f();
    f();
    f();
    return 0;
}

and:

#include <iostream>

using namespace std;

void f()
{
    static int v = 0;
    v++;
    cout << "v = " << v << endl;
}

int main()
{
    f();
    f();
    f();
    return 0;
}

both write 1 2 3 not 1 1 1.

Comments: