C Static Variables

Introduction

A static local variable exists for the duration of the program, but is only visible in the function body. Static automatic variables continue to exist even after the block in which they are defined terminates. Thus, the value of a static variable in a function is retained between repeated function calls to the same function.

Examples with static variables

Example 1

Static Arrays

We can apply static to a local array declaration 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.

Example 2
Function staticArrayInit has a local static array and function automaticArrayInit has an automatic local array.

More Examples of C Programming

c programming language

Special Discounts for Students

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.