Static keyword parsing in C Language

Source: Internet
Author: User

1. Overview

Static declared variables have two features in the C language:

1) The variables will be placed in the global storage area of the program, so that the original values can be maintained during the next call. This is the difference between stack variables and heap variables.

2) The variable uses static to notify the compiler that it is only visible within the scope of the variable. This is the difference between it and global variables.

2. Problem: static understanding

For static variables, select the correct content for all the following statements:

A. if the global variable is only accessed in a single c file, you can change the variable to a static global variable to reduce the coupling between modules;

B. If the global variable is only accessed by a single function, you can change the variable to the static local variable of the function to reduce the coupling between modules;

C. When designing and using functions that access dynamic global variables, static global variables, and static local variables, you need to consider re-import;

D. The static global variable is too large, which may cause stack overflow.

Answer and analysis:

For A, B: according to the description in the overview section B), we know that a and B are correct.

For C: according to the description in the overview section of this article a), we know that C is correct (the so-called function re-entry problem is described in detail below ).

For D: static variables are placed in the global data zone of the program, rather than allocated in the stack, so it is impossible to cause stack overflow. D is wrong.

Therefore, the answer is A, B, and C.

3. problem: the function cannot be reloaded.

I have designed a function such as the next function. I was reminded of a bug during code check because this function cannot be reentrant. Why?

Unsigned int sum_int (unsigned int Base)
...{
Unsigned int index;
// Note that sum is static.
Static unsigned int sum = 0;
For (Index = 1; index <= base; index ++)
...{
Sum + = index;
}
Return sum;
}

Answer and analysis:

The so-called function is reentrant (or predictable), that is, the same output should be generated as long as the input data is the same.

This function is unpredictable because it uses static variables. Because of the characteristics of static variables, such a function is called a function with the "internal memory" function. Therefore, if we need a reentrant function, we must avoid using static variables in the function. The principle of using static variables in this function is that we do not need to use them as much as possible.

It is very easy to change the above function to a reentrant function. As long as the static keyword in the declared sum variable is removed, the variable sum is changed to an auto type variable, A function is a reentrant function.

Of course, in some cases, static variables must be used in functions. For example, when a function returns a pointer type, the address of a static local variable must be used as the return value, if it is of the auto type, an error pointer is returned.

Description

(1). Cited from <C Programming> tan haoqiang:

"2. sometimes, you want the value of a local variable in a function to be retained without disappearing after the function call, that is, the storage units occupied by the variable are not released. When the function is called in the lower-end function, the variable has an existing value, is the value at the end of the callback function call. this indicates that the local variable is "local static variable", which is described by static."
(2). From: C language test: 0x10 basic issues that embedded programmers should know

6. What is the role of the keyword static?
Few people can answer this simple question completely. In the C language, the keyword static has three obvious functions:
• In the function body, a variable declared as static remains unchanged when the function is called.
• Within a module (but in the external body of the function), a variable declared as static can be accessed by the function used in the module, but not by other functions outside the module. It is a local global variable.
• In a module, a function declared as static can only be called by other functions in the module. That is, this function is restricted to use within the local scope of the module that declares it.
Most of the respondents can answer part 1 correctly. Some of them can answer part 2 correctly. Few people can understand part 3. This is a serious disadvantage of a candidate because he obviously does not understand the benefits and importance of localized data and code scope.

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.