Learn C - C Function Scope






Static Variables

A static variable can retain information from one function call to the next.

You could declare a static variable count with this declaration:

static int count = 0;

The word static is a keyword.

If a static variable is defined within a function, a static variable doesn't get destroyed when execution leaves the function.

The initialization of a variable declared as static occurs only once, right at the beginning of the program.

Although a static variable is visible only within the function that contains its declaration, it is essentially a global variable.

The following code shows the difference between Static and automatic variables.


    #include <stdio.h> 
      //  ww  w  .j  ava2 s. com
    // Function prototypes 
    void test1(void); 
    void test2(void); 
      
    int main(void) { 
      for(int i = 0 ; i < 5 ; ++i) { 
        test1(); 
        test2(); 
      } 
      return 0; 
    } 
    // Function test1 with an automatic variable 
    void test1(void) 
    { 
      int count = 0; 
      printf("test1   count = %d\n", ++count ); 
    } 

    // Function test2 with a static variable 
    void test2(void) 
    { 
      static int count = 0; 
      printf("test2   count = %d\n", ++count ); 
    } 

The code above generates the following result.





Sharing Variables Between Functions

To share variables among functions, declare a variable at the beginning of a program file so they're outside the scope of the functions.

These are called global variables because they're accessible anywhere.


    #include <stdio.h> 
      //w w w. jav  a  2  s  .  c  o  m
    int count = 0;                         // Declare a global variable 
      
    // Function prototypes 
    void test1(void); 
    void test2(void); 
      
    int main(void) { 
      int count = 0;                       // This hides the global count 
      
      for( ; count < 5 ; ++count) { 
        test1(); 
        test2(); 
      } 
      return 0; 
    } 
      
    void test1(void) { 
      printf("test1   count = %d\n", ++count); 
    } 
    void test2(void) { 
      printf("test2   count = %d\n", ++count); 
    } 

The code above generates the following result.





Functions Recursion

A function can call itself. This is called recursion.

A function that calls itself must also contain the ways of stopping the process.

The following code shows how to calculate the factorial of an integer.

A factorial of any integer is the product of all the integers from one up to the integer itself.


    #include <stdio.h> 
      /*  w w w. j a  v  a2  s.  c om*/
    unsigned long long factorial(unsigned long long); 
      
    int main(void) { 
      unsigned long long number = 10LL; 
      printf("The factorial of %llu is %llu\n", number, factorial(number)); 
      return 0; 
    } 
    // A recursive factorial function 
    unsigned long long factorial(unsigned long long n) { 
      if(n < 2LL) 
        return n; 
         
      return n*factorial(n - 1LL); 
    } 

The code above generates the following result.

The abort() Function

Calling the abort() function causes an abnormal termination of the program.

It requires no arguments.

You call it like this when you want to end a program:

abort();                          // Abnormal program end

The exit() and atexit() Functions

exit() function causes normal termination of the program.

The function requires an argument of type int that indicates program status at the time of termination.

The argument can be 0 or EXIT_SUCCESS to indicate a successful termination, and this is returned to the host environment.

For example:

exit(EXIT_SUCCESS);               // Normal program end

If the argument is EXIT_FAILURE, an indication of failure termination is returned to the host environment.

You can register your own functions to be called by exit() by calling atexit().

You call the atexit() function to identify a function that is to be executed when the application terminates.

Here's how you might do that:

void CleanUp(void);// Prototype of function

if(atexit(CleanUp)) 
  printf("Registration of function failed!\n"); 
 

The _Exit() Function

The _Exit() function does essentially the same job as exit().

_Exit() will not call any registered functions.

You call _Exit() like this:

_Exit(1);                         // Exit with status code 1