范围规则 [英] Scoping rules

查看:52
本文介绍了范围规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题源于

维基百科中的静态与动态范围界定文章。

http://en.wikipedia.org/wiki/Scope_(..ynamic_scoping


使用这个网站示例,如果我这样做:


#include< stdio.h>


int x = 0;

int f(){return x;}

int g(){int x = 1; return f();}


int main(void){


printf("%d \ nn",g());

返回0;

}


我得到

$ gcc -Wall bi.c -o bi

bi.c:在函数g中:

bi.c:5:警告:未使用的变量`x''

$。/ bi

0


我假设这意味着(在这种情况下)x是静态范围的,对吧?


Chad

The following question stems from Static vs Dynamic scoping article in
wikipedia.

http://en.wikipedia.org/wiki/Scope_(...ynamic_scoping

Using this sites example, if I go like this:

#include <stdio.h>

int x = 0;
int f () { return x; }
int g () { int x = 1; return f(); }

int main(void) {

printf("%d\n",g());
return 0;
}

I get
$gcc -Wall bi.c -o bi
bi.c: In function `g'':
bi.c:5: warning: unused variable `x''
$./bi
0

I''m assuming this means (in this case) that x is static scoped, right?

Chad

推荐答案

gcc -Wall bi.c -o bi

bi.c:在函数g中:

bi.c:5:警告:未使用的变量`x''
gcc -Wall bi.c -o bi
bi.c: In function `g'':
bi.c:5: warning: unused variable `x''


./ bi

0


我假设这意味着(在这种情况下)x是静态范围的,对吧?


Chad

./bi
0

I''m assuming this means (in this case) that x is static scoped, right?

Chad




Chad写道:

Chad wrote:

以下问题源于

维基百科中的静态与动态范围界定文章。

http://en.wikipedia.org/wiki/Scope_(..ynamic_scoping


使用这个网站示例,如果我这样:


#include< stdio.h>


int x = 0;

int f(){return x; }

int g(){int x = 1; return f(); }


int main(无效){


printf("%d \ n",g());

返回0;

}


我得到
The following question stems from Static vs Dynamic scoping article in
wikipedia.

http://en.wikipedia.org/wiki/Scope_(...ynamic_scoping

Using this sites example, if I go like this:

#include <stdio.h>

int x = 0;
int f () { return x; }
int g () { int x = 1; return f(); }

int main(void) {

printf("%d\n",g());
return 0;
}

I get


这篇关于范围规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆