Terminology definition - Scope in C application

2019-01-28 14:12发布

问题:

Is there a specific term for the following type of C code? In my example, I want to increase the depth of scope for a small chunk of code within a function without having to use needless if/else/for statements so that I can re-declare a variable with the same name multiple times. eg:

void myfunc(void) {
  int a = 0, b = 1;
  {
    int c;
    c = 3;
    printf("c is: %d", c);
  }

  a = 2;
  {
    int c = 5;
    printf("c is %d", c);
  }

}

What is the term used to describe how I just wrapped some code in curly braces and increased the scope depth so the rest of the function doesn't see the 'c' int?

Thanks!

回答1:

Scope is defined as the area in which the object is active. There are five scopes in C. They are as follows.

Program Scope

These are the declarations at the top most layers. They are available up to the life of a program. All the functions have this scope. This is otherwise known as global scope.

File Scope

It has scope such that it may be accessed from that point to the end of the file.

void dummy(void) { }
// absence of static automatically gives program scope to `dummy()`

static void dummy(void) { } 
// static keyword here gives function `dummy()` a file scope 

Function Scope

Only labels have this scope. In this scope, they are active up to end of the function.

void printFun()
{
print:
    printf(“i is less than j”);
}

int main()
{
    int i=1,j=2;
    if(i < j)
        goto print;
}

This code will be flagged error by the compiler saying that the label print is not known because labels have only function scope. If you have to jump unconditionally between the functions, you have to use setjmp/longjmp functions.

Block Scope

Declarations that are active up to the end of the block (where a block is defined as statements within { }). All the declarations inside the function have only block scope.

int fun(int a, int b)
{
    int c; 
    {   
        int d;
    }
    // a, b, c, d all have block scope
}

As I have said, function scope applies only to labels. So should not be confused with block scope. The function arguments are treated as if they were declared at the beginning of the block with other variables (remember that the function body is also considered as a block within { }). So the function arguments have block scope (not function scope).

Local scope is general usage to refer the scope that is either function or block scope.

Prototype Scope

They are having the scope only inside the prototype declaration. This scope is interesting because the variable names are valid only in the declaration of prototype and does not conflict with other variable names. It exists for very little time and of less use and so goes unnoticed.

int add(int a, float b);

Here the variables a and b are said to have prototype scope.

Selecting Minimal Scope

When a name has to be resolved, that name is searched in the minimal scope, and if that is not available, it is searched at higher levels of scope. So, if a variable has to be declared, you have to select a minimal scope possible. If you can limit your scope, that increases the efficiency, readability and maintainability of your program. If you want a variable which is not useful outside a block, declare it inside the block and not in the outer ones. Similarly, if you want a variable whose value will be accessed only within the function but has to retain the value between the function calls, opt for static variable to a global one.



回答2:

I'd say you are introducing a new local scope, or a nested scope, or a block.

This becomes seriously important in C++ when you take active advantage of this:

{
    std::lock_guard<std::mutex> lk(my_mutex);

    do_critical_stuff();  // might throw exception?
}
// the lock is released automagically!

But even in C it's good practice to only use variables locally where they're needed and not bleed them into unnecessarily wide scopes.



回答3:

The term is the scope.

K&R2 defines the word scope as

A name also has a scope, which is the region of the program in which it is known

Scope is the word that refers to the visibility of an identifier.



标签: c scope