关键字static、inline、extern 、const

const 参照:
//www.greatytc.com/p/2dfbac8bff0d
//www.greatytc.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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,837评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,551评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,417评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,448评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,524评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,554评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,569评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,316评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,766评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,077评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,240评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,912评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,560评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,176评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,425评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,114评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,114评论 2 352

推荐阅读更多精彩内容