c语言中怎么查看变量的范围
When you define a variable in a C program, depending on where you declare it, it will have a different scope.
在C程序中定义变量时,根据变量的声明位置,其作用域将有所不同。
This means that it will be available in some places, but not in others.
这意味着它将在某些地方可用,但在其他地方则不可用。
The position determines 2 types of variables:
该位置确定2种类型的变量:
global variables
全局变量
local variables
局部变量
This is the difference: a variable declared inside a function is a local variable, like this:
区别在于:在函数内部声明的变量是局部变量,如下所示:
int main(void) {
int age = 37;
}
Local variables are only accessible from within the function, and when the function ends they stop their existence. They are cleared from the memory (with some exceptions).
局部变量只能从函数内部访问,当函数结束时,它们将停止存在。 它们从内存中清除(有些例外)。
A variable defined outside of a function is a global variable, like in this example:
在函数外部定义的变量是全局变量,例如以下示例:
int age = 37;
int main(void) {
/* ... */
}
Global variables are accessible from any function of the program, and they are available for the whole execution of the program, until it ends.
全局变量可从程序的任何功能访问,并且在整个程序执行之前可用,直到结束。
I mentioned that local variables are not available any more after the function ends.
我提到函数结束后,局部变量不再可用。
The reason is that local variables are declared on the stack, by default, unless you explicitly allocate them on the heap using pointers, but then you have to manage the memory yourself.
原因是默认情况下局部变量是在堆栈上声明的,除非您使用指针在堆上显式分配它们,但随后必须自己管理内存。
c语言中怎么查看变量的范围