Explanation of the scope and storage type of variables in C Language

Source: Internet
Author: User

I. Scope and survival period
C program identifiers have three scopes: local, global, and file. The scope of the identifier determines which statements in the program can use it. In other words, it is the visibility of the identifier in other parts of the program. Generally, the scope of an identifier is implicitly indicated by its position in the program.
1. Local Scope
Variables in the previous examples are partial scopes. They are declared within the function and cannot be accessed by the code of other functions. The parameter scopes in the form of functions are also local, and their scope of action is limited to the statement blocks used within the function.
Void add (int );
Main ()
{
Int num = 5;
Add (num );
Printf ("% d", num);/* output 5 */
}
Void add (int num)
{
Num;
Printf ("% d", num);/* output 6 */
}
The two num variables in the above example are both local variables and are only visible in their own functions. As we mentioned above, variables with the same name in two functions do not interfere with each other. Therefore, the above two outputs are still 5 in the main function and 6 in the add () function.

2. Global Scope
For variables with a global scope, we can access them anywhere in the program. When a variable is declared externally in all functions, that is, at the beginning of the program, the variable is a global variable.
Void add (int );
Int num;
Main ()
{
Int n = 5;
Add (n );
Printf ("% d", num);/* output 6 */
}
Void add (num)/* The format parameter does not specify the type */
{
Num;
Printf ("% d", num);/* output 6 */
}
In the above main () and add (), num is not declared, but num is required to be output at the end of the output. This is because num is a global variable at the beginning of the program, that is, this variable can be used in all functions. At this time, the value of a variable is changed in a function, and the value in other functions will also be affected. The output in the above example is 6, because the num value is changed in the add () function, because num is a global variable, it is as if they both share a variable, so in main () the num in the function also changes.

3. File Scope
In many C language books, the file scope is not described, or just slightly mentioned. In fact, the file scope is very useful in large programs (in multiple file systems ). The file scope means that the external identifier is only visible to the function summary within the same conversion unit that declares it. The so-called conversion unit refers to the source code file that defines these variables and functions (including any source code files contained through the # include command ). The static storage type modifier specifies that the variable has a file scope.
Static int num;
Static void add (int );
Main ()
{
Scanf ("% d", & num );
Add (num)
Printf ("% d", num );
}
Void add (num)
{
Num;
}
In the above program, the variables num and add () use static storage type modifiers in the declaration, which makes them have a file scope and are visible only in files that define them.
Most of the programs we mentioned contain only one compilation file, so this writing method has no practical significance. However, there are many files in the actual project. They are not written by one person and completed by many people. These files are compiled by each other, this will inevitably make some people use the same global variable name, so in order to avoid mutual interference between the variables and functions in the future program, you can use the static modifier, in this way, other code files connected to the same program are invisible.

Ii. Variable storage type
As we mentioned above, variables are declared in the following similar form:
Int num;
Float total;
None of them have storage type modifiers. We can also use the storage type modifier to tell the compiler what type of variables will be processed during declaration. There are four Storage types: auto, static, external, and regiser ).

1. Automatic Storage Type
The Automatic Storage Type modifier specifies a local variable as automatic, which means that every time you execute a statement block that defines the variable, will generate a new copy of the variable in the memory and initialize it. In fact, if not specified, the storage type of local variables is automatically by default. Therefore, you can add or remove auto.
Main ()
{
Auto int num = 5;
Printf ("% d", num );
}
In this example, the code execution effect is the same no matter whether the declaration of the variable num contains the keyword auto. The Function Format Parameter Storage type is also automatic by default.

2. static storage variables
The static keyword has been used before, but for local variables, the static storage type has different meanings. In this case, it is relative to the automatic storage type. The scope of a static local variable is still limited to the statement block that declares it, but the variable will always keep its value during the execution of the statement block. In addition, the initialization value only takes effect when the statement block is executed for the first time. During subsequent running, the variable retains the value of the last execution of the statement block. Let's look at the two corresponding programs below:
/* 1. C * // * 2. C */
Int add ();
Main ()
& N

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.