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 whose life ends within the function

Lifetime of local variables
The previous chapter discussed functions, but not the relationship between functions and variables.
In this section, we will create a counting function and explain the relationship between functions and variables based on it.
A counting function is a function that counts the number of times it is called.
Although there is no specific term for it, we call it that for convenience.

Simply put, it is fairly easy to accomplish.
All you have to do is prepare a variable and add one to it each time.
The following program is an example of the above idea in action.

source code
 #include <stdio.h>

int countfunc(void);

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

int countfunc(void)
{
    int count = 0; /* initialization */
    count++;
    printf("%d\n", count);
    return count;
}

The result of executing this program will be as follows

Execution Result
1
1
1

The function to add 1 to the variable count should have been called 3 times, but the answers are all 1.
Perhaps this is due to the fact that in the initialization section, you are assigning 0.

So, I would dare to ignore the warning and use the variable without initializing it.
In this case, we don't know what number we'll get at the beginning.
Nevertheless, the second call should produce 1 more than the previous one, and the third 2 more.
The following program is an example of not initializing the variable count.

Source code
 #include <stdio.h>

int countfunc(void);

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

int countfunc(void)
{
    int count; /* no initialization (should not really be done) */
    count++;
    printf("%d\n", count);
    return count;
}

The result of executing this program (in the author's environment) is as follows

Execution Result
5369
5369
5369

Both the second and third numbers somehow produced exactly the same result.
Apparently, the result of adding one more is not reflected at all.

This is closely related to the life span of the variable.
In fact, variables declared in a function are discarded when the function terminates.
If the same function is called again, the variable is re-created once more.
At this time, a different memory is used than last time, so the value will also change.

In the previous case, when I had it run in the author's environment, it just happened to come up with the same value.
This is also, after all, entirely coincidental, and we do not know if the same value will appear.

This means that the life of a variable declared in a function is only within the function.
Thus, a variable whose life ends within the function is called a local variable.
The life span and valid range of a variable are sometimes collectively referred to as its scope.

Keywords.
Local Variables

A variable declared within a function. The same applies to a variable with a temporary argument.
When the function finishes, it is discarded.
When the function is called again, it is newly created.
It cannot be used by other functions.



Keywords.
Scope

A generic term for a mechanism that limits the valid range of an identifier.
In general, a mechanism to determine the life and valid range of a variable.


Different variables with the same name
If local variables are destroyed at the end of a function, then the
If you declare it in the main function, it should survive until the end of the main function.
The following program is an example of declaring a count variable in the main function as well.

source code
 #include <stdio.h>

int countfunc(void);

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

int countfunc(void)
{
    int count; /* no initialization (should not really be done) */
    count++;
    printf("%d\n", count);
    return count;
}

The result of executing this program (in the author's environment) is as follows

Execution Result
5369
5369
5369

Even though the count variable is declared in the main function as well, the result is still the same.
Moreover, even though the count variable is set to 0 in the main function
Given that we see a completely different value within the countfunc function
Apparently, variables declared in the main function cannot be used in other functions.

This is deeply related to the valid range of variables.
Local variables are valid only within the function in which they are declared.
If a variable with the same name is declared in another function, it will be a different variable.
Therefore, changing the value of a variable in the main function has no effect on variables in other functions.
Function Independence
So far, we have seen that variables are created completely separately for each function.
The reason why this is the way it works is because
The reason for this is to increase function independence.

In the previous section, we created a sum function to find the sum of min to max.
This function can be used immediately in other programs.
Thus, to make the functions easy to use, each function must be independent.

For example, the sum function used the num variable.
If all functions were designed to share variables, then
If another function used the num variable, the value of that variable would be changed.
Therefore, if the sum function is used in another program, the
All variables with the same name must be checked beforehand.

However, if the variables used in each function are independent, then
There is absolutely no concern about affecting variables in other functions.
It is free from the fear that the value of a variable will suddenly change.

Depending on the programming language
Although C is a fairly old programming language
Some older languages do not have variable lifetimes.

In such languages, the value of a variable must not be changed without permission anywhere.
Care must be taken throughout the program.

Therefore, when writing a large program
It was very troublesome to have so many variable names to distinguish them.

Precisely in the block
So far, we have described the life of a local variable as within a function, but it is precisely within a block.
As previously explained, a block is the area enclosed by {}.
So far, we have used it with if and for statements, but
It is also possible to use only blocks within a function without regard to those statements, as in the following program.

source code
 #include <stdio.h>

int main(void)
{
    int value1 = 10;
    int value2 = 20;

    printf("1:value1 %d\n", value1);
    printf("1:value2 %d\n", value2);

    {
        int value1;
        value1 = 30;
        value2 = 40;
        printf("2:value1 %d\n", value1);
        printf("2:value2 %d\n", value2);
    }

    printf("3:value1 %d\n", value1);
    printf("3:value2 %d\n", value2);

    return 0;
}

The result of executing this program will be as follows

Execution Result
1:value1 10
1:value2 20
2:value1 30
2:value2 40
3:value1 10
3:value2 40

There are two areas of focus.
The first is a second block that was intentionally created so that value1 can be declared again and the second block can be used to declare value2.
Moreover, it is treated as a separate variable.
You can see this from the fact that value1 was 30 the second time, but when you exit the block, it is 10.

The other is that value2 can be used even within an intentionally created block.
The value2 is the result of changes made in the block and remains in the third display.

Since {} in an if or for statement is also a block, it behaves the same way.


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 more complete 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