关键字static、inline、extern 、const

const 参照:
http://www.jianshu.com/p/2dfbac8bff0d
http://www.jianshu.com/p/f83335e036b5
static

**Preserves variable value to survive after its scope ends.**
Keyword static
 may be applied to both data and function definitions:
static *data-definition*;static *function-definition*;
For example,
static int i = 10;static void PrintCR (void) { putc ('\n'); }
static
 tells that a function or data element is only known within the scope of the current compile. In addition, if you use the static
 keyword with a variable that is local to a function, it allows the last value of the variable to be preserved between successive calls to that function. Note that the initialization of automatic and static variables is quite different. Automatic variables (local variables are automatic by default, except you explicitely use static
 keyword) are initialized during the run-time, so the initialization will be executed whenever it is encountered in the program. Static (and global) variables are initialized during the compile-time, so the initial values will simply be embeded in the executable file itself. If you change them, they will retain changed in the file. By default, the C language proposes that all uninitialized static variables are initialized to zero, but due to some limitations in TIGCC linker, you need to initialize explicitely all static and global variables if you compile the program in "nostub" mode. The fact that global and static variables are initialized in compile-time and kept in the executable file itself has one serious consequence, which is not present on "standard" computers like PC, Mac, etc. Namely, these computers always reload the executable on each start from an external memory device (disk), but this is not the case on TI. So, if you have the following global (or static) variable
int a = 10;
and if you change its value somewhere in the program to 20 (for example), its initial value will be 20 (not 10) on the next program start! Note that this is true only for global and static variables. To force reinitializing, you must put explicitely something like
a = 10;
at the begining of the main program! Note, however, that if the program is archived, the initial values will be restored each time you run the program, because archived programs are reloaded from the archive memory to the RAM on each start, similarly like the programs are reloaded from disks on "standard" computers each time when you start them.

extern

**Indicates that an identifier is defined elsewhere.**
Keyword extern
 indicates that the actual storage and initial value of a variable, or body of a function, is defined elsewhere, usually in a separate source code module. So, it may be applied to data definitions and function prototypes:
extern *data-definition*;extern *function-prototype*;
For example,
extern int _fmode;extern void Factorial (int n);
The keyword extern
 is optional (i.e. default) for a function prototype.

Inline Functions
http://www.cnblogs.com/pengyingh/articles/2405718.html
下面就是用我要介绍的内联函数来解决这些问题,我们可以使用内联函数

来取代宏的定义。而且事实上我们可以用内联函数完全取代预处理宏。

内联函数和宏的区别在于,宏是由预处理器对宏进行替代,而内联函数是

通过编译器控制来实现的。而且内联函数是真正的函数,只是在需要用到的时

候,内联函数像宏一样的展开,所以取消了函数的参数压栈,减少了调用的开

销。你可以象调用函数一样来调用内联函数,而不必担心会产生于处理宏的一

些问题。

我们可以用Inline来定义内联函数,不过,任何在类的说明部分定义的函

数都会被自动的认为是内联函数。

下面我们来介绍一下内联函数的用法。

内联函数必须是和函数体申明在一起,才有效。像这样的申明

Inline Tablefunction(int I)是没有效果的,编译器只是把函数作为普通的函

数申明,我们必须定义函数体。

Inline tablefunction(int I) {return I*I};

这样我们才算定义了一个内联函数。

By declaring a function inline
, you can direct GCC to integrate that function's code into the code for its callers. This makes execution faster by eliminating the function-call overhead; in addition, if any of the actual argument values are constant, their known values may permit simplifications at compile time so that not all of the inline function's code needs to be included. The effect on code size is less predictable; object code may be larger or smaller with function inlining, depending on the particular case. Inlining of functions is an optimization and it really "works" only in optimizing compilation. If you don't use **'-O'**, no function is really inline. Inline functions are included in the ISO C99 standard, but there are currently substantial differences between what GCC implements and what the ISO C99 standard requires. To declare a function inline, use the inline
 keyword in its declaration, like this:
inline intinc (int *a){ (*a)++;}
(If you are writing a header file to be included in ISO C programs, write __inline__
 instead of inline
. See Alternate Keywords.) You can also make all "simple enough" functions inline with the option **'-finline-functions'**. Note that certain usages in a function definition can make it unsuitable for inline substitution. Among these usages are: use of a variable number of arguments, use of alloca, use of variable sized data types (see Variable Length Arrays), use of computed goto (see Labels as Values), use of nonlocal goto, and nested functions (see Nested Functions). Using **'-Winline'** will warn when a function marked inline
 could not be substituted, and will give the reason for the failure. Note that in C (unlike C++), the inline
 keyword does not affect the linkage of the function. When a function is both inline and static
, if all calls to the function are integrated into the caller, and the function's address is never used, then the function's own assembler code is never referenced. In this case, GCC does not actually output assembler code for the function, unless you specify the option **'-fkeep-inline-functions'**. Some calls cannot be integrated for various reasons (in particular, calls that precede the function's definition cannot be integrated, and neither can recursive calls within the definition). If there is a nonintegrated call, then the function is compiled to assembler code as usual. The function must also be compiled as usual if the program refers to its address, because that can't be inlined. When an inline function is not static
, then the compiler must assume that there may be calls from other source files; since a global symbol can be defined only once in any program, the function must not be defined in the other source files, so the calls therein cannot be integrated. Therefore, a non-static
 inline function is always compiled on its own in the usual fashion. If you specify both inline
 and extern
 in the function definition, then the definition is used only for inlining. In no case is the function compiled on its own, not even if you refer to its address explicitly. Such an address becomes an external reference, as if you had only declared the function, and had not defined it. This combination of inline
 and extern
 has almost the effect of a macro. The way to use it is to put a function definition in a header file with these keywords, and put another copy of the definition (lacking inline
 and extern
) in a library file. The definition in the header file will cause most calls to the function to be inlined. If any uses of the function remain, they will refer to the single copy in the library. For future compatibility with when GCC implements ISO C99 semantics for inline functions, it is best to use static inline
 only. (The existing semantics will remain available when **'-std=gnu89'** is specified, but eventually the default will be **'-std=gnu99'** and that will implement the C99 semantics, though it does not do so yet.) GCC does not inline any functions when not optimizing unless you specify the always_inline
 attribute for the function, like this:
/* Prototype. */inline void foo (const char*) __attribute__((always_inline));

参考:http://tigcc.ticalc.org/doc/gnuexts.html#SEC93
http://tigcc.ticalc.org/doc/gnuexts.html#SEC93

你可能感兴趣的:(关键字static、inline、extern 、const)