c语言中怎么查看变量的范围_C中变量的范围

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.

原因是默认情况下局部变量是在堆栈上声明的,除非您使用指针在堆上显式分配它们,但随后必须自己管理内存。

翻译自: https://flaviocopes.com/c-variables-scope/

c语言中怎么查看变量的范围

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值