C program to demonstrate the static and local variables
kw.c
#include <stdio.h>
int main()
{
    for (int i = 0; i < 5; i++)
    {
        static int a = 0;
        int b = 0;
        a++;
        b++;
        printf("Static:a = %d | Local:b = %d\n", a, b);
    }
    return 0;
}
Output
kodingwindow@kw:~$ gcc kw.c
kodingwindow@kw:~$ ./a.out Static:a = 1 | Local:b = 1 Static:a = 2 | Local:b = 1 Static:a = 3 | Local:b = 1 Static:a = 4 | Local:b = 1 Static:a = 5 | Local:b = 1 kodingwindow@kw:~$
Advertisement