Scope rules for C language functions

Source: Internet
Author: User
Tags define define local

The scope rule for language is a set of rules that determine whether part of the code is "visible" or access to another part of the code and data.

Every function in the C language is a separate block of code. The code block of a function is hidden inside a function and cannot be accessed by any statement in any other function (other than the statement that invokes it) (for example, it is not possible to jump to another function with the G o t o statement). The code that forms the body of a function is hidden from the rest of the program, and it does not affect other parts of the program, nor is it affected by other parts. In other words, because two functions have different scopes, code data defined within a function cannot interact with code and data that is defined within another function.

All functions in the C language are at the same scope level. This means that it is impossible to define a function inside another function.

4.2.1 Local Variables

The variables defined within the function become local variables. In some C language textbooks, local variables are called automatic variables, and this is consistent with the practice of defining local variables with the optional keyword a u t o. A local variable is accessed only by statements within its defined module. In other words, local variables are not known outside of their own code modules. Remember: The module starts with a left curly brace and ends with a right curly brace.

For local variables, the most important thing to understand is that they exist only in the currently executing code block that is defined, that is, the local variable is generated when it enters the module and dies when it exits the module.

The most common block of code that defines a local variable is a function. For example, consider the following two functions.

Integer variable x is defined two times, once in func1 (), once in Func2 (). X in Func1 () and Func2 () are unrelated. The reason is that each x as a local variable is only known within the defined block.

The language includes the keyword auto, which can be used to define local variables. But since the default for all Non-global variables is assumed to be auto, auto is rarely used, so this keyword is not seen in all of the examples in this book.

It is the most common practice to define all required variables at the beginning of each function module. Doing so makes it easy for anyone to read this function and understand the variables used. This does not have to be done, however, because local variables can be defined in any module. To understand how this works, see the following function.

Here The local variable s is established at the entrance of the If Block and dies at its exit. So S is only known in the If block and is inaccessible elsewhere, even in other parts of the function that contains it.

The main advantage of defining local variables within a condition block is that memory is allocated only when needed. This is because local variables enter the lifetime only when control is transferred to their defined blocks. While this is not very important in most cases, this becomes important when the code is used for a dedicated controller (such as a garage door controller that identifies digital security code), since random memory (RAM) is extremely scarce.

Because local variables are created or released as the import and export of their defined modules, the information they store is lost at the end of the block work. Remember, this is especially important for access to functions. When a function is accessed, its local variables are established, and when the function returns, the local variable is destroyed. This means that the value of a local variable cannot be persisted between invocations of two times.

4.2.2 Global Variables

Unlike local variables, global variables run through the entire program and can be used by any one module. They remain valid throughout the execution of the program. Global variables are defined outside all functions and can be accessed by any expression within the function. As you can see in the following program, the variable count is defined outside all functions, before the function main (). But in fact it can be placed in any place before it is first used, as long as it is not within the function. Practice shows that the best place to define global variables is at the top of the program.

After careful study of this program, the visible variable count is neither main () nor func1 () defined, but both can be used. The function Func2 () also defines a local variable count. When Func2 accesses count, it accesses only its own defined local variable count, not the global variable count. It is convenient to remember that when a global variable has the same name as a local variable of a function, all accesses to that name are for local variables only and have no effect on global variables. However, if you forget this, even if the program appears to be correct, it can lead to strange behavior at run time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.