What is the method of declaring global variables in C language?

In C language, the keyword extern is used to indicate the declaration of global variables. Global variable declarations are typically placed outside functions, above all functions. Here is how global variables are declared:

  1. Declare variables globally.
int globalVariable; // 声明一个全局int类型变量
  1. Declare and initialize variables on a global scale.
int globalVariable = 10; // 声明并初始化一个全局int类型变量
  1. Declare a variable on a global scale and indicate that it is defined in another file by using the “extern” keyword.
extern int globalVariable; // 声明一个在其他文件中定义的全局int类型变量

It is important to note that global variables should not be initialized when declared. Initialization should be done at the point of definition. Also, when using global variables in other files, the extern keyword should be used at the point of use to indicate that the variable is defined in another file.

bannerAds