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 to the end

Lifetime of global variables
In the previous section, we explained that variables declared within a function have a life span that ends within the function.
It is natural to ask, then, what about outside the function?

So far, all variables have been declared within a function, but in fact they can also be declared outside a function.
A variable declared outside a function is sometimes called a global variable.

Keywords.
Global Variables

Variables declared outside the function.
It survives until the entire program is terminated, and
Available from all functions in the source file in which it is declared.


The following program is an example of using global variables.

source code
 #include <stdio.h>

int count; /* global variable */

int countfunc(void);

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

int countfunc(void)
{
    count++;
    printf("%d\n", count);
    return count;
}

The result of executing this program will be as follows

Execution Result
1
2
3

Unlike in the previous chapter, this time the number of function calls is successfully counted.
This is closely related to the lifetime of global variables.
In the case of global variables, they survive to the end of the program.
Thus, even if a variable is called many times, it remembers previous values.

By the way, even though the variable count is not initialized in the above program
The clean output of 1, 2, and 3 is no coincidence.
Global variables are automatically initialized to 0 at the start of a program.

Since local variables are created at each function call, the
It is not automatically initialized because it would be wasteful to initialize it each time.
This is because global variables need to be initialized only once at the beginning.
Shared by all functions
Global variables are variables declared outside of a function.
Unlike local variables within a function, they can be used freely from any function.
The following program is an example of changing the variable count from the main function.

source code
 #include <stdio.h>

int count; /* global variable */

int countfunc(void);

int main(void)
{
    countfunc();
    count = 10; /* change here */
    countfunc();
    countfunc();
    return 0;
}

int countfunc(void)
{
    count++;
    printf("%d\n", count);
    return count;
}

The result of executing this program will be as follows

Execution Result
1
11
12

This result shows that the second display suddenly has a value of 11, but
This is because the variable count is changed in the main function.
Thus, global variables are shared by all functions.

This is convenient because it means that you can make assignments and retrieve values from any function, but
We must be very careful how we use them in other functions.
Heavy use of global variables requires many variable names, which can be very cumbersome.

Therefore, global variables should only be used for special data that is shared throughout the program, and not for
Basically, using local variables makes programs easier to understand.
Local variables are independent
In the previous section, we explained that global variables are shared by all functions.
If there is a local variable in the function with the same name as the global variable, then the
I would like to experiment to see which takes precedence.
The following program is an example of declaring the variable count in the main function.

source code
 #include <stdio.h>

int count; /* global variable */

int countfunc(void);

int main(void)
{
    int count; /* declared with the same name */

    countfunc();
    count = 10;
    countfunc();
    countfunc();
    printf("main : count = %d\n", count);
    return 0;
}

int countfunc(void)
{
    count++;
    printf("%d\n", count);
    return count;
}

The result of executing this program will be as follows

Execution Result
1
2
3
main : count = 10

First, given that this program can run
We can see that we can declare a local variable with the same name as a global variable.

Let's look at the results to see which takes precedence
Even though the value of the variable count is changed within the main function
The numbers in the countfunc function are not changed at all, and
At the end of the main function, the value assigned in the function is displayed.
In other words, a local variable with the same name as a global variable takes precedence over a local variable.

This is another mechanism for maintaining function independence, as explained in the previous section.
If the global variable takes precedence, then when the function is copied and used, the
It is troublesome to check whether a variable in the function has the same name as a global variable.

Source Files and Global Variables
We have explained here that global variables are shared by all functions.
More precisely, they are shared within a single source file.

For example, a global variable declared at the beginning of the main.c file is shared by
All functions in the main.c file can be assigned or their values can be referenced, but the
It cannot be referenced by functions in another file, for example, the data.c file.

However, if you tell another file the name and type of the variable
Global variables can be used from all files.

Since we haven't yet explained how to use multiple source files, we're going to use the
At this stage, it is safe to assume that all functions will share this information.



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