C register, extern, and static usage

Source: Internet
Author: User

1. register variable

The keyword regiter requests the compiler to store the variable in the CPU register as much as possible. Note the following points.

1. The register variable must be a type that can be accepted by the CPU register. This usually means that the register variable must be a single value and its length should be less than or equal to the length of an integer.

However, some machine registers can also store floating point numbers.

2. The register variable may not be stored in the memory. Therefore, the "&" operator cannot be used.

3. Only local variables and form parameters can be used as register variables, but not global variables.

4. Static variables cannot be defined as register.
The register modifier implies that the corresponding variables of the compiler will be frequently used. If possible, they should be stored in the CPU register to speed up their storage. For example, the following memory block copy code,

/* Procedure for the assignment of ures ,*/

/* If the C compiler doesn't support this feature */

# Ifdef NOSTRUCTASSIGN

Memcpy (d, s, l)

{

Register char * d;

Register char * s;

Register int I;

While (I --)

* D ++ = * s ++;

}

# Endif

 

Ii. static

1. Modify variables (local and global variables)

① Static global variables:

Allocate memory in the global data area. Static global variables without initialization will be automatically initialized to 0 by the program (the value of the automatic variable is random unless it is explicitly initialized );

The scope is limited to the files where variables are defined.

 


Differences between global variables and Global static variables:

1) global variables are global variables not explicitly modified with static, but global variables are static by default, and the scope is the global variables defined in a file throughout the project, in another file,

You can use global variables by using the extern global variable name declaration.

2) Global static variables are global variables explicitly modified with static, and the scope is the file. Other files cannot be declared using extern.

② Static local variables

 

# Include <cstdio> Add this statement
Char a = 'a'; // global variable
Void msg ()
{
Printf ("Hellon ");
}

You may ask: why can the global variables a and msg defined in a. c be used in main. c? As mentioned above, all global variables and functions without the static prefix have global visibility, and other source files can also be accessed. In this example, a is a global variable, msg is a function, and there is no static prefix. Therefore, it is visible to other source files main. c.
If static is added, other source files are hidden. For example, if static is added before the definitions of a and msg, main. c will not be able to see them. This feature allows you to define functions with the same name and variables in different files without worrying about name conflicts. Static can be used as the prefix of functions and variables. For a function, the role of static is limited to hiding. For a variable, static has the following two functions.
(2) The second role of static is to keep the variable content persistent. Variables stored in the static data area will be initialized at the beginning of the program, which is also the only initialization. There are two types of variables stored in the static storage area: global variables and static variables, but compared with global variables, static can control the visible range of variables. static is used to hide the variables.
(3) The third role of static is that the default Initialization is 0. In fact, global variables also have this attribute, because global variables are also stored in the static data zone. In the static data area, the default values of all bytes in the memory are 0x00. In some cases, this feature can reduce the workload of programmers.
Finally, we will summarize the three functions of static in one sentence. First, the main function of static is to hide. Secondly, because static variables are stored in the static storage area, they have persistence and default value 0.
The content of main. c is as follows:
In addition to header files, you must declare the function: void msg ();

Int main (void)
{
Extern char a; // extern variable must be declared before use
Printf ("% c", );
(Void) msg ();
Return 0;
}

2. Static Functions

Add the static keyword before the return type of the function. The function is defined as a static function. A static function is different from a common function. It can only be seen in the file where it is declared,

Cannot be used by other files. Functions with the same name can be defined in other files without conflict.

Static function .....
Internal and external functions
When a source program is composed of multiple source files, the C language divides the function into internal functions and external functions based on whether the function can be called by functions in other source files.
1 internal function (also known as static function)
A function defined in a source file can only be called by functions in this file, but cannot be called by functions in other files of the same program. This function is called an internal function.
To define an internal function, you only need to add a "static" keyword before the function type, as shown below:
Static function name (function parameter table)
{......}
The keyword "static" is "static" in Chinese. Therefore, internal functions are also called static functions. However, the meaning of "static" here is not the storage method, but the scope of the function is limited to this file.
The advantage of using internal functions is that when different people write different functions, you don't have to worry about whether your own defined functions will have the same name as the functions in other files, because the same name does not matter.
2. External Functions
External Function Definition: when defining a function, if the keyword "static" is not added or the keyword "extern" is appended, this function is an external function:
[Extern] function name (function parameter table)
{......}
When calling an external function, you need to describe it:
[Extern] function name of the function type (parameter type table) [, function name 2 (parameter type table 2)…];
Case] external function application.
(1) file mainf. c

Main ()
{Extern void input (...), Process (...), Output (...);
Input (...); Process (...); Output (...);
}
(2) file subf1.c
......
Extern void input (......) /* Define external functions */
{......}
(3) file subf2.c
......
Extern void process (......) /* Define external functions */
{......}
(4) file subf3.c
......
Extern void output (......) /* Define external functions */
{......}

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.