Terminology definition - Scope in C application

前端 未结 3 723
一生所求
一生所求 2020-12-21 14:17

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

3条回答
  •  猫巷女王i
    2020-12-21 14:50

    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 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.

提交回复
热议问题