简体   繁体   English

C 编程全局和局部 scope

[英]C programming global and local scope

#include<stdio.h>

int f1(void);
int f2(void);
int f3(void);

int x = 10;

int main()
{
    int x =1;
    x+=f1() + f2() + f3() + f2();
    printf("%d", x);
    return 0;
}

int f1()
{
    int x = 25;
    x++;
    return x;
}
int f2()
{
    static int x= 50; 
    x++; 
    return x;
}

int f3()
{
    x*=10; 
    return x;
}

Why f3 is taking global x variable?为什么 f3 采用全局 x 变量? I mean main is calling f3 and In main we have x variable why doesn't f3 take the value of main x variable.我的意思是 main 正在调用 f3 并且在 main 我们有 x 变量,为什么 f3 不采用 main x 变量的值。 Please help请帮忙

C uses so-called lexical scope. C 使用所谓的词法scope。 That means that when you use a variable x , you look from the block where you are and outwards until you find the definition of x , and that, then, is the variable you are referring to.这意味着当您使用变量x时,您会从您所在的块向外查看,直到找到x的定义,然后,这就是您所指的变量。

In f3() , you have x , so you look in the block you are inside.f3()中,您有x ,因此您可以查看您所在的块。 There is no definition of x so you look outwards. x没有定义,所以你向外看。 In this case, outwards is the global scope, because we do not have any nested blocks here.在这种情况下,向外是全局 scope,因为我们这里没有任何嵌套块。

Lexical scope is not affected by which functions call which other functions.词法 scope 不受哪些函数调用哪些其他函数的影响。 It is a static property of the code, not a dynamic property of the running code.它是代码的 static 属性,而不是运行代码的动态属性。 If we used that kind of scope rule, it is called dynamic scope , the x in f3() would depend on who called it at any given time.如果我们使用这种 scope 规则,它被称为动态 scopef3()中的x将取决于在任何给定时间调用它的人。 So the x would be different at different times.所以x在不同的时间会有所不同。 There are not many languages that use dynamic scope any longer, because it is damned hard to figure out what variables refer to at any point in the program execution.不再使用动态 scope 的语言已经不多了,因为在程序执行的任何时候都很难弄清楚变量指的是什么。

It is just different scope rules, and C uses lexical scope and not dynamic scope.它只是不同的 scope 规则,而 C 使用词法 scope 而不是动态 Z31A1FD140BE4BEF2D181E158Z1

TL;DR Because scope doesn't propagate through function calling. TL;DR因为scope不会通过 function 调用传播。

Here are some blocks that illustrate scope.以下是一些说明 scope 的块。

scope1

main() {
    scope2
    function();
}

function() {
    scope3
    if () {
        scope4
    }
    scope5
}

scope1 is a "global" scope, that is known and accessible everywhere in your project. scope1是一个“全局”scope,在您的项目中随处可见和可访问。 If you declare any variables here as "static" they are known only within that file.如果您在此处将任何变量声明为“静态”,则它们仅在该文件中是已知的。

scope2 is known in the body of main(). scope2在 main() 的主体中是已知的。 The body of main() also knows and has access to scope1 . main() 的主体也知道并可以访问scope1

scope3 is the same thing as scope5 , and the body of function() also knows the global scope1. scope3scope5是一样的,function() 的主体也知道全局 scope1。 function() does NOT know scope2 , despite being called from there. function() 不知道scope2 ,尽管是从那里调用的。 If you want scope3 or scope5 to know anything about scope2 you must pass that as parameters to function().如果您希望scope3scope5了解有关scope2的任何信息,则必须将其作为参数传递给 function()。

scope4 is the body of an if . scope4if的主体。 The body of the if knows scope3 and the global, scope1 . if 的主体知道scope3和全局scope1 Anything declared in scope4 is lost at the end of the block, so scope5 does not know anything about scope4 , unless scope4 actually changes the value of a variable declared in scope1 or scope3 .在 scope4 中声明的任何内容都会在块的末尾丢失,因此scope5不知道有关scope4的任何信息,除非scope4实际上更改了在scope1scope3中声明的变量的值。

Confusion can easily arise when variables in different levels of nested scope have the same name.当嵌套 scope 的不同级别中的变量具有相同的名称时,很容易出现混淆。 This should be avoided!应该避免这种情况!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM