MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Variables that survive within a function

The lifetime of a static local variable.
We have discussed local variables, global variables, and variables with different lifetimes.
Actually, there exists a variable with unusual characteristics that lies between these two.

When declaring a variable within a function, adding static before the type name
You can declare a static local variable.
The following program demonstrates an example of declaring a static local variable.

Source code
#include <stdio.h>

int countfunc(void);

int main(void)
{
    countfunc();
    countfunc();
    countfunc();
    return 0;
}

int countfunc(void)
{
    static int count; /* Static Local Variable */

    count++;
    printf("%d\n", count);

    return count;
}

The results of this program's execution are as follows:

Results
1
2
3

Despite being declared within the function, its value increases by one with each call.
The initial value is 0 even though it hasn't been initialized.
It looks like a global variable, plain and simple.

However, the variable count is declared within the function, making it essentially a local variable.
In fact, using the variable `count` within the `main` function will result in an error.

This is a characteristic of a static local variable.
Since it's declared within a function, it's only accessible within that function.
That value persists until the program terminates.
Furthermore, they are automatically initialized to zero, especially without explicit initialization.

Moreover, initialization will only be performed once at the beginning.
For example, it can also be counted if initialized like this.

Source code
static int count = 0; /* Static Local Variable */

This variable is used when you want the function to remember a value from a previous call.
While its uses are limited, it can be helpful for counting function calls.
This could be the case when a search function needs to remember previously found character positions.


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. line break
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19