Global variables

This is an explanation of global variables in C language.

C language programming without global variables is recommended

First of all, it is highly recommended to do C programming without using global variables.

If you create your own live concert or application, you can program it without using global variables.

If a library you haven't created requires you to use global variables as the library's interface, you'll need to use global variables, but if you're programming your own C language, use global variables. You can program without it. Even in such a case, we will devise ways to avoid access to global variables as much as possible.

Dangers of global variables

The danger of global variables is that, in a nutshell, global variables can be changed from anywhere.

Now, assuming there were 1 million lines of programming, something went wrong and the global variable changed somewhere.

Now, do you want to struggle to find this part, or do you want to eliminate the risk from the beginning?

The C99-enabled introduction to C strongly recommends ways to eliminate risk from the beginning.

But why are global variables overflowing?

But why, after all, is programming out there so full of global variables?

With 500 lines, it was easy to check even with global variables. The next person in charge copied and imitated what was written before. It's very easy as a makeshift. The program is getting bigger and bigger.

If you notice, it has reached 10,000 lines.

Even if I try to rewrite it, I will not be given time for refactoring. There is no authority or atmosphere to negotiate such.

A realistic way to achieve the next requirement is to copy and paste the previous one and use it with a slight modification. And because I neglected to refactor it so much, the time I spent writing programming was steadily increasing and I was reaching the exam period. At the last minute of the exam, I worked overtime until 10 pm and finally finished writing the program. Now, is such a system a reliable system?

How to program without using global variables

I will introduce a method of programming without using global variables.

Integer constant is enum

If you want to use integer constants, use enum enum.

Floating point constants are constant macros

If you want to use floating point constants, #define macro description.

String constants are static local variables

The technique for using string constants is described in the static local variables section of the explanation of local variables.

An array of string constants is a static local variable

The technique for using an array of string constants is described in the static local variables section of the explanation of local variables.

I want to keep the same value from the beginning to the end of the program

You can use static local variables in this case as well.

I want to access a set of data from multiple functions

The motivation for using global variables is that it is troublesome to pass only the necessary data to the function as an argument, and you may want to access a set of data from multiple functions.

Passing only the data you need to a function is the safest way to reentrant, but it's tedious and impractical when building a slightly larger application.

In such cases, let's incorporate object-oriented thinking into C language. Save data to the object for access.

Explanation of the grammar of global variables

So far, I will explain the grammar by letting you feel the global variable "Hiyari, Hatto".

Declaration of global variables

Variables declared outside the function are global variables. Global variables are initialized with 0 (all bits are 0). This is different from local variables, which have indefinite values.

Global variables are valid from the beginning to the end of the program. Global variables are visible everywhere in the program (extern if outside the file).

#include <stdio.h>
#include <stdint.h>

// Declaration of global variables
int32_t MYAPP_GLOBAL_VAR;

// Output the contents
int main (void) {
  printf("%d\n", MYAPP_GLOBAL_VAR);
}

This is the output result.

0

The C language has a specification that you cannot write an expression outside a function, but you can write a term on the right-hand side to initialize a global variable.

#include <stdio.h>
#include <stdint.h>
#include <string.h>

// Declaration and initialization of global variables
int32_t MYAPP_GLOBAL_VAR_CONST1 = 5;
int32_t MYAPP_GLOBAL_VAR_CONST2 = sizeof (6);
int32_t MYAPP_GLOBAL_VAR_NO_CONST = strlen ("AAABBB");

// Syntax error when writing an expression
// strlen ("AAABBB");

// Output the contents
int main (void) {
  printf("%d\n", MYAPP_GLOBAL_VAR_CONST1);
  printf("%d\n", MYAPP_GLOBAL_VAR_CONST2);
  printf("%d\n", MYAPP_GLOBAL_VAR_NO_CONST);
}

This is the output result of gcc. If the initialization is not a constant, you will be warned.

a.c: 8: warning: initializer element is not constant
a.c: 8: warning: (near initialization for ‘MYAPP_GLOBAL_VAR_NO_CONST’)
Five
Four
6

Keep this in mind as a specification, and if you want to use constants, we recommend not using the global variables introduced above.

Access global variables defined in other object files

The C language has the concept of file scope, and global variables described in other object files cannot be seen as they are. If you want to access global variables in other object files, use the extern declaration.

# Allow access to global variables described in other files
extern OUTER_GLOBAL_VAR;

Make global variables inaccessible from other object files

Use the static modifier to make global variables inaccessible from other object files. With the static modifier, you can only use it in this file (imagine it's an object file).

#include <stdio.h>
#include <stdint.h>

// Declaration of global variables that are valid only in this file (imagine it as an object file)
static int32_t MYAPP_GLOBAL_VAR;

// Output the contents
int main (void) {
  printf("%d\n", MYAPP_GLOBAL_VAR);
}

Please let me know if you have actually used global variables.

Yes. I confess honestly.

Let's talk about the case where we have used statically qualified global variables.

In the processing system of programming languages, there is logic to extract the ID of a function from the function name. The process of extracting the ID of a function from the function name is a linear search, and if the ID is extracted every time, the cost of performance is very high.

Furthermore, the function ID is expected to be used tens to hundreds of times in the same object file.

The ID of a function does not change from the beginning to the end of programming. If you get it only once and set it, you can use it forever.

To meet this goal, I wanted to use statically qualified global variables, and in fact I ended up using statically qualified global variables.

Associated Information