C - static                                                                                                      Home : www.sharetechnote.com

 

 

 

 

static

 

 

Example 01 >

 

#include <stdio.h>

#include <string.h>

 

void staticIncrement()

{

    

    int a = 0;

    static int b = 0;

    

    printf("staticIncrement() is called -----------------------------\n");

    printf("   Before Increment : a = %d, b = %d\n",a,b)   ;

    a++;

    b++;

    printf("   After Increment : a = %d, b = %d\n",a,b)    ;

}

 

int main()

{

 

    staticIncrement();

    staticIncrement();

    staticIncrement();

    staticIncrement();

    staticIncrement();

        

    return 1;

}

 

Result :

staticIncrement() is called -----------------------------

   Before Increment : a = 0, b = 0

   After Increment : a = 1, b = 1

staticIncrement() is called -----------------------------

   Before Increment : a = 0, b = 1

   After Increment : a = 1, b = 2

staticIncrement() is called -----------------------------

   Before Increment : a = 0, b = 2

   After Increment : a = 1, b = 3

staticIncrement() is called -----------------------------

   Before Increment : a = 0, b = 3

   After Increment : a = 1, b = 4

staticIncrement() is called -----------------------------

   Before Increment : a = 0, b = 4

   After Increment : a = 1, b = 5