Loading, please wait...

Static and Automatic Arrays

A static local variable exists for the duration of the program but is visible only in the function body. We can apply static to a local array definition so the array is not created and initialized each time the function is called and the array is not destroyed each time the function is exited in the program.

 

This reduces program execution time, particularly for programs with frequently called functions that contain large arrays. In functions that contain automatic arrays where the function is in and out of scope frequently, make the array static so it’s not created each time the function is called.

 

 Arrays that are static are initialized once at compile time. If you do not explicitly initialize a static array, that array’s elements are initialized to zero by the compiler.

 

Example

Static arrays are initialized to zero */
#include <stdio.h>
void staticArrayInit( void ); /* function prototype */
void automaticArrayInit( void ); /* function prototype */
/* function main begins program execution */
int main( void )
{
printf( "First call to each function:\n" );
staticArrayInit();
automaticArrayInit();
printf( "\n\nSecond call to each function:\n" );
staticArrayInit();
automaticArrayInit();
return 0; /* indicates successful termination */
} /* end main */
/* function to demonstrate a static local array */
void staticArrayInit( void )
{
/* initializes elements to 0 first time function is called */
static int array1[ 3 ];
int i; /* counter */
.
printf( "\nValues on entering staticArrayInit:\n" );
/* output contents of array1 */
for ( i = 0; i <= 2; i++ ) {
printf( "array1[ %d ] = %d ", i, array1[ i ] );
} /* end for */
.
printf( "\nValues on exiting staticArrayInit:\n" );
.
/* modify and output contents of array1 */
for ( i = 0; i <= 2; i++ ) {
printf( "array1[ %d ] = %d ", i, array1[ i ] += 5 );
} /* end for */
} /* end function staticArrayInit */
/* function to demonstrate an automatic local array */
void automaticArrayInit( void )
{
/* initializes elements each time function is called */
int array2[ 3 ] = { 1, 2, 3 };
int i; /* counter */
.
printf( "\n\nValues on entering automaticArrayInit:\n" );
.
/* output contents of array2 */
for ( i = 0; i <= 2; i++ ) {
printf("array2[ %d ] = %d ", i, array2[ i ] );
} /* end for */
printf( "\nValues on exiting automaticArrayInit:\n" );
/* modify and output contents of array2 */
for ( i = 0; i <= 2; i++ ) {
printf( "array2[ %d ] = %d ", i, array2[ i ] += 5 );
} /* end for */
} /* end function automaticArrayInit */

Output:

First call to each function:

Values on entering staticArrayInit:

array1[ 0 ] = 0 array1[ 1 ] = 0 array1[ 2 ] = 0

Values on exiting staticArrayInit:

array1[ 0 ] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5

Values on entering automaticArrayInit:

array2[ 0 ] = 1 array2[ 1 ] = 2 array2[ 2 ] = 3

Values on exiting automaticArrayInit:

array2[ 0 ] = 6 array2[ 1 ] = 7 array2[ 2 ] = 8

Second call to each function:

Values on entering staticArrayInit:

array1[ 0 ] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5

Values on exiting staticArrayInit:

array1[ 0 ] = 10 array1[ 1 ] = 10 array1[ 2 ] = 10

Values on entering automaticArrayInit:

array2[ 0 ] = 1 array2[ 1 ] = 2 array2[ 2 ] = 3

Values on exiting automaticArrayInit:

array2[ 0 ] = 6 array2[ 1 ] = 7 array2[ 2 ] = 8