May 11, 2021 C
In any kind of programming, a scope is an area of a variable defined in a program that cannot be accessed beyond that range. There are three places in the C language where variables can be declared:
Let's look at what a local variable, a global variable, and a formal parameter are.
A variable declared inside a function or block is called a local variable. T hey can only be used by the function or by statements inside the block of code. L ocal variables are anaesthetized outside the function. T he following is an example of using a local variable. Here, all variables a, b, and c are local variables of the 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 are defined outside the function, usually at the top of the program. Global variables are valid throughout the life of the program and can be accessed within any function.
Global variables can be accessed by any function. T hat is, global variables are available throughout the program after they are declared. Here are examples of using global 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 and global variables can be the same, but within a function, the value of a local variable overrides the value of the global variable. Here's 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
The parameters of the function, formal parameters, are treated as local variables within the function, and they override the global variables first. Here's an example:
#include <stdio.h>
/* 全局变量声明 */
int a = 20;
int main ()
{
/* 在主函数中的局部变量声明 */
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;
}
/* 添加两个整数的函数 */
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
When a department variable is defined, it is not initialized by the system and you must initialize it yourself. When you define a global variable, it is automatically initialized as follows:
type of data | Initialization default value |
---|---|
int | 0 |
char | '' |
float | 0 |
double | 0 |
pointer | NULL |
It is a good programming practice to initialize variables correctly, otherwise sometimes programs may produce unexpected results because un initialized variables can result in junk values that are already available in memory locations.