MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
C language learned by suffering

Variables that survive in the function

Lifetime of static local variables
We have covered local variables, global variables, and variables with different lifetimes.
In fact, there is a variable with unusual characteristics that is intermediate between the two.

When declaring a variable in a function, you can prefix the type name with static to make it
You can declare static local variables.
The following program is 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 result of executing this program will be as follows

Execution Result
1
2
3

Even though it is declared in the function, the value is increasing by 1 with each call, and
The beginning value is 0 even though no initialization has been done.
This appears to be a global variable by all accounts.

However, the variable count is essentially a local variable because it is declared within a function.
In fact, using the variable count within the main function results in an error.

This is a characteristic of static local variables.
Since it is declared within a function, it can only be used within the function in which it is declared, but
Its value remains until the program terminates.
It is also automatically initialized to 0 without any particular initialization.

Note that initialization is done only once at the beginning.
For example, the following initialization can be counted

Source Code
 static int count = 0; /* static local variable */

This variable is used when a function wants to remember the value of a previous call.
Its use is limited, but it can be used to count the number of times a function has been called or to
A possible example would be a function that performs a search and stores the position of previously found characters.


About this Site

The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or better than any book on the market.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box