Cprogramming 简明教程
C - Scope Rules
任何编程中的作用域都是程序的一个区域,其中已定义的变量可以存在,并且变量不能超出此区域进行访问。在 C 编程语言中,变量可以在三个地方声明 −
A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed. There are three places where variables can be declared in C programming language −
-
Inside a function or a block which is called local variables.
-
Outside of all functions which is called global variables.
-
In the definition of function parameters which are called formal parameters.
让我们了解什么是 local 和 global 变量,以及 formal 参数。
Let us understand what are local and global variables, and formal parameters.
Local Variables
在函数或块内声明的 Variables 称为 local variables 。它们只能由该函数或代码块内的语句使用。局部变量对于其自身外部的函数来说是未知的。
Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own.
Example
以下示例显示了如何使用局部变量。此处,所有变量 a、b 和 c 都是 main() function 的局部变量。
The following example shows how local variables are used. Here all the variables a, b, and c are local to main() function.
#include <stdio.h>
int main () {
/* local variable declaration */
int a, b;
int c;
/* actual initialization */
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 a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.
任何函数都可以访问全局变量。也就是说,全局变量在声明后可以在整个程序中使用。
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration.
Example
以下程序显示了如何在程序中使用全局变量。
The following program show how global variables are used in a program.
#include <stdio.h>
/* global variable declaration */
int g;
int main () {
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10;
b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
程序可以为局部变量和全局变量使用相同名称,但函数内部的局部变量值将优先。下面是示例 −
A program can have same name for local and global variables but the value of local variable inside a function will take preference. Here is an example −
Formal Parameters
形式参数在函数内视为局部变量,并且优先于全局变量。下面是示例 −
Formal parameters, are treated as local variables with-in a function and they take precedence over global variables. Following is an example −
Example
#include <stdio.h>
/* global variable declaration */
int a = 20;
int main () {
/* local variable declaration in main function */
int a = 10;
int b = 20;
int c = 0;
printf ("value of a in main() = %d\n", a);
c = sum( a, b);
printf ("value of c in main() = %d\n", c);
return 0;
}
/* function to add two integers */
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 result −
value of a in main() = 10
value of a in sum() = 10
value of b in sum() = 20
value of c in main() = 30
Initializing Local and Global Variables
当定义局部变量时,它不会由系统初始化,您必须自己初始化它。当您按照以下方式定义全局变量时,它们会自动由系统初始化 −
When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Global variables are initialized automatically by the system when you define them as follows −
Data Type |
Initial Default Value |
int |
0 |
char |
'\0' |
float |
0 |
double |
0 |
pointer |
NULL |
对变量进行正确初始化是良好的编程做法,否则程序可能会产生意外结果,因为未初始化的变量将采用其内存位置中已有的垃圾值。
It is a good programming practice to initialize variables properly, otherwise your program may produce unexpected results, because uninitialized variables will take some garbage value already available at their memory location.