user3514491
user3514491

Reputation: 75

What does it mean to declare variables as static in a function?

I was looking for a function to do some audio effect, and I found one written in C.

Inside this function, some variables are declared as Static. I am confused, I thought Static means these variables are not visibles to other files. But since they are declared inside inside a function, they are already not visible to other files.

What am I missing ?

Upvotes: 2

Views: 161

Answers (1)

merlin2011
merlin2011

Reputation: 75555

static inside a function means that it will hold its value the next time the function is called.

For example,

int foo() {
    static int i = 0;
    printf("%d\n", i);
    i++;
}

int main() {
    foo(); // Prints 0
    foo(); // Prints 1
}

Upvotes: 2

Related Questions