const 參照:
http://www.reibang.com/p/2dfbac8bff0d
http://www.reibang.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
下面就是用我要介紹的內(nèi)聯(lián)函數(shù)來解決這些問題拘领,我們可以使用內(nèi)聯(lián)函數(shù)
來取代宏的定義意乓。而且事實上我們可以用內(nèi)聯(lián)函數(shù)完全取代預(yù)處理宏。
內(nèi)聯(lián)函數(shù)和宏的區(qū)別在于约素,宏是由預(yù)處理器對宏進行替代届良,而內(nèi)聯(lián)函數(shù)是
通過編譯器控制來實現(xiàn)的。而且內(nèi)聯(lián)函數(shù)是真正的函數(shù)业汰,只是在需要用到的時
候伙窃,內(nèi)聯(lián)函數(shù)像宏一樣的展開,所以取消了函數(shù)的參數(shù)壓棧样漆,減少了調(diào)用的開
銷为障。你可以象調(diào)用函數(shù)一樣來調(diào)用內(nèi)聯(lián)函數(shù),而不必擔(dān)心會產(chǎn)生于處理宏的一
些問題放祟。
我們可以用Inline來定義內(nèi)聯(lián)函數(shù)鳍怨,不過,任何在類的說明部分定義的函
數(shù)都會被自動的認為是內(nèi)聯(lián)函數(shù)跪妥。
下面我們來介紹一下內(nèi)聯(lián)函數(shù)的用法鞋喇。
內(nèi)聯(lián)函數(shù)必須是和函數(shù)體申明在一起,才有效眉撵。像這樣的申明
Inline Tablefunction(int I)是沒有效果的侦香,編譯器只是把函數(shù)作為普通的函
數(shù)申明,我們必須定義函數(shù)體纽疟。
Inline tablefunction(int I) {return I*I};
這樣我們才算定義了一個內(nèi)聯(lián)函數(shù)罐韩。
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