简体   繁体   中英

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 ?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM