[My C language interview series] 001 static What is the purpose?

Source: Internet
Author: User

[My C language interview series] 001 static What is the purpose?


[Question] What is the purpose of static?

 

The answer is:

1. restrict the scope of Variables

2. Set the storage domain of the Variable

I think it is inappropriate to answer questions in this way.

 

The following is my answer:

The static type declaration has three main purposes in the C language:

1, Declare static local variables.

2, Declare static external global variables.

3, Declare static external functions.

 

The following are my explanations of the above three usages. In addition, the static usage Summary of xiaocai0001 provides more detailed explanations. For more information, see.

Http://blog.csdn.net/xiaocai0001/archive/2006/04/14/662921.aspx

Static local variables(Compared with auto)

1, Storage space allocation, scope, and Storage Period

Static is allocated in the static storage area, and the scope is limited to the internal function that declares the variable. In the program

The entire running period is not released, and the lifetime runs throughout the entire process of running the program.

The auto type is assigned to the stack and belongs to the dynamic storage class, which occupies the dynamic storage space. The scope is limited to the function that declares the variable. The function is automatically released after the function call ends. The lifetime is only within the function that declares the variable.

2Processing Method for Initial Value assignment

Static static local variables assign initial values at compilation, that is, only the initial values are assigned once;

Auto auto variable assigns an initial value when the function is called. Every time the assign function is called, the initial value is re-assigned, which is equivalent to executing a value assignment statement.

3Processing Method when no initial value is assigned

If the initial value is not assigned when defining a local variable:

Static local variables. The initial value 0 (For numeric variables) or null characters (for character variables) are automatically assigned during compilation ).

Auto automatic variable. If no initial value is assigned, its value is an uncertain value.

 

Static external global variables

In C language, static is also used to declare static external global variables, so the scope of this global variable is limited within this file.

External variables (global variables) are defined outside the function. Their scope is from the definition of variables to the end of the program file. If the external variable is not defined at the beginning of the file, its effective scope is limited to the end of the file at the definition. If the function before the definition point wants to reference this external variable, you should use the keyword extern to declare this variable before the reference ". This variable is an external variable that has been defined. With this declaration, you can legally use this external variable from the "Declaration.

What if the declared global variables do not want to be accessed and used by other files?

That is to add the keyword static before the declaration.

 

Static external functions

 

In C language, our functions are global by default, that is, you can call functions in other files. In use, we can add the extern in the header file as before. But sometimes the function we write does not want other files to access and call, so we can add static before declaring the function.

The advantages of using internal functions are as follows:

1. Preventing some internal functions from being used by others, but allowing callers to use what they can, helps to protect code.

2. When writing different functions for different users, you don't have to worry about whether the defined functions will have the same name as the functions in other files.

 

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.