關(guān)鍵字static弄企、inline超燃、extern 、const

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市污朽,隨后出現(xiàn)的幾起案子散吵,更是在濱河造成了極大的恐慌,老刑警劉巖蟆肆,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件矾睦,死亡現(xiàn)場離奇詭異,居然都是意外死亡炎功,警方通過查閱死者的電腦和手機枚冗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蛇损,“玉大人官紫,你說我怎么就攤上這事肛宋≈菖海” “怎么了束世?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長床玻。 經(jīng)常有香客問我毁涉,道長,這世上最難降的妖魔是什么锈死? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任贫堰,我火速辦了婚禮,結(jié)果婚禮上待牵,老公的妹妹穿的比我還像新娘其屏。我一直安慰自己,他們只是感情好缨该,可當(dāng)我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布偎行。 她就那樣靜靜地躺著,像睡著了一般贰拿。 火紅的嫁衣襯著肌膚如雪蛤袒。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天膨更,我揣著相機與錄音妙真,去河邊找鬼。 笑死荚守,一個胖子當(dāng)著我的面吹牛珍德,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播矗漾,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼锈候,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了缩功?” 一聲冷哼從身側(cè)響起晴及,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎嫡锌,沒想到半個月后虑稼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡势木,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年蛛倦,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片啦桌。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡溯壶,死狀恐怖及皂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情且改,我是刑警寧澤验烧,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站又跛,受9級特大地震影響碍拆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜慨蓝,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一感混、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧礼烈,春花似錦弧满、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至摹迷,卻和暖如春疟赊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背峡碉。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工近哟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人鲫寄。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓吉执,卻偏偏與公主長得像,于是被迫代替她去往敵國和親地来。 傳聞我的和親對象是個殘疾皇子戳玫,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容