The last variable standing
Global variable lifetime
In the previous section, we explained that variables declared within a function have a lifetime that ends within that function.
Naturally, that leads one to consider what would happen outside the function.
I used to declare all variables inside functions, but it turns out you can also declare them outside of functions.
Variables declared outside of a function are sometimes called global variables.
Variables declared outside functions. They persist until the entire program terminates and can be used from all functions within the source file where they are declared.
The following program is an example using global variables.
The output of this program is as follows:
Unlike the previous chapter, this time the number of function calls is being counted.
This is deeply related to the lifetime of global variables.
Global variables persist for the duration of the program.
Therefore, it remembers previous values even if the variable is called multiple times.
By the way, in the program mentioned above, the variable 'count' was not initialized.
It was neatly output as 1, 2, 3, and that's no coincidence.
Global variables are automatically initialized to zero at the start of the program.
Local variables are created each time a function is called.
Since it's wasteful to initialize every time, it's not initialized automatically.
Because global variables only need to be initialized once.
Naturally, that leads one to consider what would happen outside the function.
I used to declare all variables inside functions, but it turns out you can also declare them outside of functions.
Variables declared outside of a function are sometimes called global variables.
Keyword
ćGlobal variableć
Variables declared outside functions. They persist until the entire program terminates and can be used from all functions within the source file where they are declared.
The following program is an example 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 output of this program is as follows:
Results
1
2
3
2
3
Unlike the previous chapter, this time the number of function calls is being counted.
This is deeply related to the lifetime of global variables.
Global variables persist for the duration of the program.
Therefore, it remembers previous values even if the variable is called multiple times.
By the way, in the program mentioned above, the variable 'count' was not initialized.
It was neatly output as 1, 2, 3, and that's no coincidence.
Global variables are automatically initialized to zero at the start of the program.
Local variables are created each time a function is called.
Since it's wasteful to initialize every time, it's not initialized automatically.
Because global variables only need to be initialized once.
shared by all functions
Global variables are variables declared outside of any function.
Unlike local variables within a function, they can be freely used from any function.
The following program demonstrates an example of modifying the variable `count` from the `main` function.
The output of this program is as follows:
Looking at these results, the value suddenly became 11 on the second display.
This is because the variable 'count' is being modified within the main function.
In this way, global variables are shared by all functions.
This is convenient because it allows for assignment and value retrieval from any function.
You must use it with great care, paying close attention to how it's used in other functions.
Using global variables heavily can lead to a large number of variable names, which can be very cumbersome.
Therefore, global variables should be used only for data that is meant to be shared throughout the entire program.
Essentially, using local variables can make a program more understandable.
Unlike local variables within a function, they can be freely used from any function.
The following program demonstrates an example of modifying 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 output of this program is as follows:
Results
1
11
12
11
12
Looking at these results, the value suddenly became 11 on the second display.
This is because the variable 'count' is being modified within the main function.
In this way, global variables are shared by all functions.
This is convenient because it allows for assignment and value retrieval from any function.
You must use it with great care, paying close attention to how it's used in other functions.
Using global variables heavily can lead to a large number of variable names, which can be very cumbersome.
Therefore, global variables should be used only for data that is meant to be shared throughout the entire program.
Essentially, using local variables can make a program more understandable.
Local variables are independent.
In the previous section, we explained that global variables are shared by all functions.
If a local variable has the same name as a global variable within a function,
I'd like to experiment to see which takes priority.
The following program demonstrates declaring a variable named 'count' within the main function.
The output of this program is as follows:
First, starting with what this program can do,
I understand that you can declare a local variable with the same name as a global variable.
To see which takes priority, let's look at the results.
despite modifying the value of the variable 'count' within the main function,
The numbers within the countfunc function have not been altered at all.
At the end of the main function, the value assigned within the function is displayed correctly.
In essence, a local variable takes precedence over a global variable if they have the same name.
This is also a mechanism for maintaining the independence of functions, as explained in the previous section.
If global variables are prioritized, when using a copy of a function,
It's a hassle to check if the variables within that function have the same names as global variables.
If a local variable has the same name as a global variable within a function,
I'd like to experiment to see which takes priority.
The following program demonstrates declaring a variable named 'count' within 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 output of this program is as follows:
Results
1
2
3
main : count = 10
2
3
main : count = 10
First, starting with what this program can do,
I understand that you can declare a local variable with the same name as a global variable.
To see which takes priority, let's look at the results.
despite modifying the value of the variable 'count' within the main function,
The numbers within the countfunc function have not been altered at all.
At the end of the main function, the value assigned within the function is displayed correctly.
In essence, a local variable takes precedence over a global variable if they have the same name.
This is also a mechanism for maintaining the independence of functions, as explained in the previous section.
If global variables are prioritized, when using a copy of a function,
It's a hassle to check if the variables within that function have the same names as global variables.
Source Files and Global Variables
Here we explained that global variables are shared by all functions, but
to be precise, they are shared within a single source file.
For example, a global variable declared at the top of the main.c file
can be assigned to or referenced by all functions within the main.c file,
but it cannot be referenced by functions in another file, such as data.c.
However, if you tell another file the variable's name and type,
the global variable becomes usable from all files.
Since we haven't yet covered using multiple source files,
it's perfectly fine to think of them as shared by all functions at this stage.
to be precise, they are shared within a single source file.
For example, a global variable declared at the top of the main.c file
can be assigned to or referenced by all functions within the main.c file,
but it cannot be referenced by functions in another file, such as data.c.
However, if you tell another file the variable's name and type,
the global variable becomes usable from all files.
Since we haven't yet covered using multiple source files,
it's perfectly fine to think of them as shared by all functions at this stage.
About This Site
Learning C language through suffering (Kushi C) isThis 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.




