Latest web development tutorials

C scope rules

Any kind of programming, the scope is defined in the program variable region that exist beyond the variable region can not be accessed. C language, there are three places you can declare variables:

  1. In thelocal variables within a block or
  2. Allglobal variables outside the function
  3. Function parameters define the parameterform

Let's see what arelocal variables, globalvariables andformalparameters.

Local variables

Variable declared inside a function or block called local variables. They can only be used by the function or block of code inside the statement. Local variables outside the function is unknown. The following are examples of the use of local variables. Here, all of the variables a, b and c are local variables 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 in the external function, usually at the top of the program. Global variables throughout the application life cycle are valid in any internal functions can access global variables.

Global variables can be accessed by any function. In other words, after the global variable declaration it is available throughout the program. The following is the use of global and local variables examples:

#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 the program, local variables and global variables can be the same, but inside a function, local variables will override the value of a global variable. Here 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 produces the following results:

value of g = 10

Formal parameters

Function parameters, the formal parameters are treated as local variables within the function, they will give priority to override the global variable. Here 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 produces the following results:

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

Initialize local and global variables

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

数据类型初始化默认值
int 0
char '\0'
float 0
double 0
pointer NULL

Properly initialized variable is a good programming practice, otherwise the program may sometimes produce unexpected results because an uninitialized variable causes some garbage value is already available in the memory location.