Difference between local scope and function scope

前端 未结 5 1811
独厮守ぢ
独厮守ぢ 2020-12-20 19:05

Once I assumed that these two have the same meaning but after reading more about it i\'m still not clear about the difference. Doesn\'t the local scope sometimes refer to sc

5条回答
  •  情话喂你
    2020-12-20 19:45

    Local scope is the area between an { and it's closing }. Function scope is the area between the opening { of a function and its closing }, which may contain more "local" scopes. A label is visible in the entirety of the function within which it is defined, e.g.

    int f( int a ) 
    {
        int b = 8;
        if ( a > 14 )
        {
           int c = 50;
           label:
           return c - a - b;
        }
        if ( a > 7 ) goto label;
        return -99;
    }
    

    int c is not visible outside its enclosing block. label is visible outside its enclosing block, but only to function scope.

提交回复
热议问题