Page 23 - 6437
P. 23

In C programming, when static is used on a class data member, it causes only one copy of
                   that member to be shared by all the objects of its class.


                 #include <stdio.h>



                 /* function declaration */

                 void func(void);



                 static int count = 5;             /* global variable */



                 main()
                 {
                    while(count--)

                    {

                        func();
                    }
                    return 0;

                 }

                 /* function definition */
                 void func( void )
                 {

                    static int i = 5;              /* local static variable */

                    i++;
                    printf("i is %d and count is %d\n", i, count);
                 }

                          When the above code is compiled and executed, it produces the following result:

                 i is 6 and count is 4

                 i is 7 and count is 3




















                                                                                                      25
   18   19   20   21   22   23   24   25   26   27   28