C scoping rules


In any kind of programming, the scope is the area where the variables defined in the program exist. Variables cannot be accessed beyond this area. There are three places in C language where variables can be declared:

  1. Inside a function or block Local Variables

  2. At all Global variables outside the function

  3. In the function parameter definition of the form parameter

let Let's take a look at what local variables, global variables and form parameters are.

Local variables

Variables declared inside a function or block are called local variables. They can only be used by statements inside the function or block of code. Local variables are not known outside the function. The following is an example of using local variables. Here, all variables a, b and c are local variables of main() function.

#include <stdio.h> int main (){  /* 局部变量声明 */  int a, b;  int c; 
  /* 实际初始化 */
  a = 10;
  b = 20;
  c = a + b;
 
  printf ("value of a = %d, b = %d and c = %d\n", a, b, c); 
  return 0;}

Global variables

Global variables are defined outside the function, usually at the top of the program. Global variables are valid throughout the program life cycle and can be accessed within any function.

Global variables can be accessed by any function. In other words, global variables are available throughout the program after they are declared. The following are examples of using global variables and local variables:

#include <stdio.h> /* 全局变量声明 */int g; int main (){  /* 局部变量声明 */  int a, b; 
  /* 实际初始化 */
  a = 10;
  b = 20;
  g = a + b;
 
  printf ("value of a = %d, b = %d and g = %d\n", a, b, g); 
  return 0;}

In a program, the names of local variables and global variables can be the same, but within a function, the value of the local variable will override the value of the global variable. The following is an example:

#include <stdio.h> /* 全局变量声明 */int g = 20; int main (){  /* 局部变量声明 */  int g = 10;
 
  printf ("value of g = %d\n",  g); 
  return 0;}

When the above code is compiled and executed, it will produce the following results:

value of g = 10

Formal parameters

Parameters of the function, formal parameters, Treated as local variables within the function, they will override global variables first. The following is an example:

#include <stdio.h> /* 全局变量声明 */int a = 20; int main (){  /* 在主函数中的局部变量声明 */  int a = 10;  int b = 20;  int c = 0;  int sum(int, int);

  printf ("value of a in main() = %d\n",  a);
  c = sum( a, b);
  printf ("value of c in main() = %d\n",  c);  return 0;}/* 添加两个整数的函数 */int sum(int a, int b){
    printf ("value of a in sum() = %d\n",  a);
    printf ("value of b in sum() = %d\n",  b);    return a + b;}

When the above code is compiled and executed, it will produce the following results:

value of a in main() = 10value of a in sum() = 10value of b in sum() = 20value of c in main() = 30

Initialize local variables and global variables

When local variables When defined, the system does not initialize it, you must initialize it yourself. When defining a global variable, the system will automatically initialize it as follows:

##pointerNULL
Data typeInitialization default value
int0
char'\0'
float0
double0
It is a good programming practice to initialize variables correctly, otherwise sometimes the program may produce unexpected results because uninitialized variables lead to some garbage values ​​that are already available in the memory location .