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 scope of function? and what does it mean that only labels have a function scope?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Yes. In most C-derived languages, variables are valid in the scope in which they're declared. If you declare a variable inside a function, but not within any other code block, then that variable is usually called a "local" or "automatic" variable. You can refer to it anywhere in the function. On the other hand, if you declare your variable inside another code block -- say, in the body of a conditional statement, then the variable is valid only inside that block. Several other answers here give good examples.
Context would be helpful, but it means that you can't jump from one function to a label in a different function.
Not to stir up a hornet's nest, but the scope of labels probably won't come up too often. Labels are mainly useful with the
goto
statement, which is needed only very rarely if ever, and even if you did choose to usegoto
you probably wouldn't even think of trying to jump into a different function.Function Scope is between outer
{
}
.Local scope is between inner
{
}
Note that, any scope created by
{``}
can be called as the local scope while the{``}
at the beginning of the function body create the Function scope.So, Sometimes a Local Scope can be same as Function Scope.
Labels
are nothing but identifiers followed by a colon. Labeled statements are used as targets forgoto
statements. Labels can be used anywhere in the function in which they appear, but cannot be referenced outside the function body. Hence they are said to have Function Scope.Code Example:
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 c
is not visible outside its enclosing block. label is visible outside its enclosing block, but only to function scope.The scope of the function is slightly larger than the scope of the function body: The function arguments are in the outer scope, while local variables are only in the inner one. This is most visibly manifest in a function-try-block:
Inside the catch block, only the variables in function scope are still in scope, but not the local variables.
Of course you can and do also introduce further, deeper nested scopes all the time, e.g. in
for
loop bodies or conditional bodies.