Some questions about scope and link properties and storage types from the extern and static keywords

Source: Internet
Author: User

Before we get to the point we have to understand some concepts:

Identifiers: Identifiers not only represent the names of variables, but the main () function is also an identifier, which is important.

Storage type: That is, where the variable is stored and its life cycle:
Static zone: Divided into two pieces
. date has been explicitly initialized for a global variable with a static variable
. BSS holds uninitialized global or static variables

Attention:
The initial value of a static variable is initialized at compile time;
The meaning is that the static modified variable is assigned to the value of the word is saved as his initial value, if not initialized, the assignment is zero, and the whole program is initialized only once;
That is, regardless of the static int i = 1, or where this static int A is located in the program, where the compiler initial values are loaded 1 and 0 are saved in the static area
Note that initialization is performed only once, see the following example:
#include <stdio.h>

void Add ()
{
static A;
static B = 1;
printf ("%d\t", a++);
printf ("%d\n", b++);
}

int main ()
{
int i = 0;
for (; i<5; i++)
{
Add ();
}
return 0;
}
The result is:
0 1
1 2
2 3
3 4
4 5


Stack area:

Stack: The system automatically allocates and releases: for example, a local variable function or a code block at the end of the release because the local variable is stored in the stack area.
Heap: Developers apply and release themselves (for example, call void *malloc ()). Its life cycle is the developer's decision.
When you use malloc, it is created and destroyed after free.

Constant zone: stores constant data, which is present at the end of the program and can be accessed but cannot be modified.


Link Properties:
Suppose we have a lot of. c files together to generate an executable file such as:
TEST0.C test1.c test2.c
|       | |
|               | |
|               | |
TEST0.O TEST1.O TEST2.O
|      | |
—————————————— Links
|
Test

The role of a link is to bring the various binary code file chains to an executable so no matter how many C files we write, we end up with one,
It's time for us to think about a problem. Whether the identifiers of individual files are common, this leads to link properties.

c There are three types of link properties:
External (Global): Each file accesses the same identifier.
Internal (partial): identifiers are accessible only where they are defined in the file.
None (no link property): No Link property.


Scope: The size of the code area that the identifier can be accessed.
Code block scope: identifiers defined in a code block can be accessed only at the beginning of the definition and the block of code, even if declared as static does not enlarge the scope, as mentioned below.
File scope: Generally refers to global variables and functions, that is, in the entire file from the beginning of the definition of the location can be accessed.
Prototype scope: The scope of the formal parameter, which is generally valid in the entire function code block, and its existence is to prevent us from defining the identifier of a name conflict with the parameter identifier multiple times within the function
Because of this we will lose the value of the formal parameter.
Function scope: This is used to say the code of the role of the area, so he and Goto about, hehe, I looked at a glance to forget.

Here are a few key words ()

Auto: When you enter the program, the stack is created and the program ends automatically. The general local variable comes with the Auto property. So very little is used.

Static
1: When modifying a local variable, the storage type is modified to make it a static storage. As for the variables after the static storage of the local variables, refer to the explanations and examples above. But the scope does not change.
2: When modifying the global identifier, it means to modify the external property of the global variable to internal. But the scope does not change.
3: Modify the function to modify its Link property external property to internal. But the scope does not change;

extern
1. The variable is declared when the variable is defined in another file or elsewhere in this file, and is generally used to external the global identifier (variable function);
Attention:
If we declare in an identifier, once it is declared outside the function block, the compiler defaults to the previous external property instead of the extern modifier. This is important and easy to confuse.
The local variables declared in the code block are then taken with a intenral link property. But this does not mean that he can only be given the property of the code block that is intenral by the link property.
The real reason is that the local variable is treated as a variable of type auto.

Register: Variables stored in registers, what are the characteristics of registers we all know that the efficiency of the register is often used for repetitive variable operations. Attention
You cannot use global variables to apply, typically auto, which is related to the life cycle of global variables.

Summary: Never confuse extern and external with static and internal. Their specific explanations look at the text.
Dog Day's company written tests and test questions often take this to test people. Just like this:
#include "stdio.h"
int a=1;

int f (int c)
{
static int a=2;
c=c+1;
Return (a++) +c;
}
Main ()
{
int i,k=0;
for (i=0;i<2;i++)
{
int a=3;
K+=f (a);
}
K+=a;
printf ("%d\n", K);
}
We just need to follow the above-mentioned knowledge: To change the variable representation, you can:
#include "stdio.h"
int a=1;

int f (int c)
{
static int b=2;
c=c+1;
Return (b++) +c;
}
Main ()
{
int i,k=0;
for (i=0;i<2;i++)
{
int d=3;
K+=f (d);
}
K+=a;
printf ("%d\n", K);
}

Problem and solve, mother no longer have to worry about my study.

Some questions about scope and link properties and storage types from the extern and static keywords

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.