spirito-buono
4/26/2018 - 11:48 AM

Static

static is a keyword in the C programming language. It can be used with variables and functions.

By default, variables are local to the scope in which they are defined. Variables can be declared as static to increase their scope up to file containing them. As a result, these variables can be accessed anywhere inside a file.

While static variables have scope over the file containing them making them accessible only inside a given file, global variables can be accessed outside the file too.

By default, functions are global in C. If we declare a function with static, the scope of that function is reduced to the file containing it.

#include<stdio.h>
int runner() {
    int count = 0;
    count++;
    return count;
}

int main()
{
    printf("%d ", runner());
    printf("%d ", runner());
    return 0;
}


#include<stdio.h>
int runner()
{
    static int count = 0;
    count++;
    return count;
}

int main()
{
    printf("%d ", runner());
    printf("%d ", runner());
    return 0;
}

static void fun(void) {
   printf("I am a static function.");
}