【C】external/internal/static/register variable and function

 external(global)

 external variable is defined outside of functions. They are available to all the below functions and all of reference points to the same instance. 

 Unlike variable, functions are all external because C don't allow define function inside another. 

 If they are referred before it's defined or it's defined in different file, extern keyword should be used. Like:

In file1:
    extern int sp;
    extern char var [];

    void pushvar(double f) {...};
    void pop(void) {...};

In file2:
    int sp =0;
    char var[MAXNUM];

  

internal(local)

internal variable are also called automatic variable. They're defined in functions,  come to existence when the function is entered and disappear when left. 

static

static keyword could be applied to:

1, external var. In this case, the scope of var is limited to the rest of the current complied file.Without external keyword, variable wlll be seen from the whole program.

2, internal var. In this case, var is not destroyed after function exit. It could be used to save value between different times of function invoke.

3, function. In this case , function scope is limited to the rest of the current compiled file, just like external var. 

Register

Register keyword imply that this variable is heavily used. Register keyword can only be applied to automatic variable.  

PS: external and static var has to be intialized by constant expression while automatic and register var  has no such restiction. If absence of explicit initialization, external and static var is initialized to 0, automatic and register var have undefined values. 

原文地址:https://www.cnblogs.com/dracohan/p/2937996.html