Share URLs
Http://wenku.baidu.com/link?url=e-xWNn7f84rrEf_vhHz5CQh2LCVmaGBTA6iB0BC8zPv_8eXz5SKmRofsSuenh8wn_ Jjeqzbd103xca6wqdkyo9szlivkgcpywwrbynvbxvs
In the general
C
In textbooks, you can see
6
Type modifiers, respectively, are
: auto, const, register,
Static, volatile, extern.
Local variables unless explicitly indicated as
Static
Otherwise the default is
Auto
, so the type is not typically used in code
Modifier
Auto.
In the post-compiler era, the optimizer can allocate registers reasonably, so it is generally not possible to use type repair in code
Decorative characters
Register.
extern
Used only to declare global variables, use single.
This section will mainly introduce
const, static
And
Volatile.
1. Const
The first thing to note is that
Const
The modifier is the type in front of it, if it is not preceded by a type, then it repairs
It is the type that follows it.
For example:
(a) const int i = 0;
And
(b) int const i = 0;
is exactly the same.
In
(a)
In
Const
There is no type ahead, it modifies the one behind it.
Int
Type. In
(b)
In
Const
Before decorating it
Face of
Int
Type, there is no difference between the two.
Looking at another slightly more complicated example, the following two statements are not the same:
(c) const int *PI = 0;
/*
Equivalent
int const *PI = 0; Pi
is a point to
const INT
Pointer to this operator to obtain a
A
const INT
Type, which cannot be used as an lvalue, after which the statement uses a value similar to
*PI = 1
Operation will result in
Compilation error. But the variable itself does not have
Const
property, you can use the
PI = &i
The operation. Can be used to access read-only
Memory.
*/
(d) int* const PI = 0;
/* PI
is a point to
Int
Type of
Const
Pointer, a complex reference to this operator to obtain a
Int
Type, which can be
As an lvalue, the statement can use a value similar to the
*PI = 1
operation, but the variable itself has
Const
Properties, using
PI = &i
Operation will result in a compilation error. Can be used to access storage at a fixed location.
*/
Let's look at a more complicated example:
(e) const int* const PI = 0;
/* PI
And
*pi
are not allowed as lvalue values. It is only suitable for reading a read-only memory in a fixed position
*/
Const
The following are typical uses
:
*
Used for a parameter list, usually decorated with a pointer type, indicating that the function does not attempt to write to an incoming address
Operation. For example:
void *memcpy (void *, const void *, size_t);
*
Used to return a value, typically a pointer to a read-only region. For example:
Const datatype_t *get_fixed_item (int index);
*
To fix the unchanging data
(
such as the Code table
)
With a read-only attribute, in some cases you can reduce
Ram
The overhead.
2.static
Static
The use of global variable declarations and local variable declarations has completely different semantics, and it has to be said that this is
C
Language
An unreasonable point in the design of a speech. When
Static
Used to decorate global variable declarations
(
or a function declaration, you can think of a function
A declaration is the declaration of a pointer to a snippet, the value of which is finally determined by the link, in this sense,
A function declaration is also a global variable declaration
)
That indicates that the variable has a file scope and can only be used by the source file's code
Referenced and cannot be accessed by code in other source files. The actual changes that are caused at compile time are
Static
Modified variables
Will not be written to the output section of the destination file and will not be referenced when parsing undefined symbols in other modules at link time.
The opposite of it is
extern
。
For example:
------MAIN.C---
extern int A (void);
int main () {return a ();}
------A.C------
/* Link would fail unless remove
“
Static
”
Modifier * *
static int A (void) {return 0;}
When
Static
Used to decorate a local variable declaration, which indicates that the variable is not allocated in the activity record of the function, and
Is the data segment that is assigned to the global
(
Or
Bss
Paragraph
)
In Simply put, it is
Static
The modified local variables are not actually
is a local variable, but a global variable with a function scope, except that it can only be accessed within the function that defines it
(
This is by
C
Grammar-determined
)
, its run-time characteristics are exactly the same as global variables, and function returns do not affect its state, its
Initialization occurs only once, at the time of the program's loading, rather than at each time the function call is initialized. It's anti-righteousness
Word is
Auto
。
For example
,
The following function returns how many times it has been called:
int callee (void) {
static int times_called = 0;
Return (+ + times_called);
}
3.volatile
Volatile
Modifier is the function of telling the optimizer not to optimize the read and write operation of this variable, so be sure to change the
Read-write operations to generate code.
For example:
/*
Delay operation
*/
int foo (void) {
/* 100
Return after the second subtraction
*/
volatile int i = 100; /* (a) */
while (i > 0) i--; /* (b) */
return 0;
}
In no
Volatile
The modified case, because the variable
I
Changes have no effect on the context, so the optimizer is likely
Will omit the right
I
Operation of the code, and only the generated
return 0
The code, plus
Volatile
can guarantee that the compiler must be
Statement
(a)
And
(b)
Generate code to achieve the purpose of delay.
/*
Device status Determination
*/
int Uart_write_char (int c) {
/*
Sending registers to the serial port to write the characters to be sent
*/
* (volatile unsigned int *) Uart_tx_reg = c;
/*
Determine if it has been sent
*/
while ((* (volatile unsigned int *) Uart_status_reg & tx_bit)! = 0); /* (c) */
return 0;
}
In the statement
(c)
, if you do not use the
Volatile
, the optimizer may be able to read two times
Uart_status_re
G
There is no right between
Uart_status_reg
Writes, and the read operation refers to the outside of the loop and causes a dead loop.
About type modifiers for the C language