In depth understanding of C static extern and pointer functions

  • 2020-04-02 02:06:25
  • OfStack

1. Exit (0) normal exit procedure

Exit (1) exit the program when the program is abnormal

2. A static variable modifies a local variable

Using static modification on local variables will prolong the existence of local variables. But we need to note a few things:

The & # 8226; Although the static modifier has a long lifetime, it is always a local variable and cannot be used in other functions
The & # 8226; What's the difference between a static global variable and a normal global variable? What's the difference between a static local variable and a normal local variable? What's the difference between a static function and a normal function?
        The description of a global variable (an external variable) is prefixed with static to constitute a static global variable. Global variables themselves are statically stored,   Static global variables are of course statically stored. The two are no different in the way they are stored. The difference between the two is that the scope of the non-static global variable is the entire source program,   When a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable limits its scope, meaning it is only valid in the source file where the variable is defined,   It cannot be used in other source files of the same source program. Because static global variables are scoped to a source file and can only be Shared by functions in that source file,   Therefore, you can avoid causing errors in other source files.
        From the above analysis, it can be seen that changing a local variable to a static variable changes its storage mode, that is, its lifetime. Changing a global variable to a static variable changes its scope and limits its use.
        The static function has a different scope than a normal function. Only in this document. Functions that are used only in the current source file should be declared as internal (static), and internal functions should be declared and defined in the current source file. For functions that can be used outside the current source file, it should be stated in a header file that the source file to use the functions contains
        Static global variables and ordinary global variables what is the difference: the static global variables only make the original, to prevent the static global variables in other file units to be referred to;
        Static local variables and ordinary local variables what is the difference: static local variables are only initialized once, the next time according to the last result value;
        What's the difference between a static function and a normal function: a static function has only one copy in memory, while a normal function maintains one copy per call
3. Extern (external variables)   Modify global variable

Extern can modify functions as well as variables

Global variables have a wide range, so why use extern? Here's an example


#include "stdio.h"
void main()
{
      extern        a;
      extern        b;
      printf("a=%d,b=%d",a,b);  
}
int a=13,b=5;


In the above example, extern is used before a and b are defined, meaning that extern extends the scope of global variables.

Not only is extern used in the above example, but global variables in different files can also be used with extern.

4. Pointer functions

Definition: a pointer function is a function whose type and return value are Pointers.

General form of pointer function:

  Type * function name (parameter list)

Here's an example of how pointer functions work



#include "stdio.h"
char * SubString(char s[],int i,int j);
char *SubString1(char s[] ,char temp[], int i,int j);
void main()
{ 
char string[]="I Love C Language"; 
char *ps=NULL;
char temp[100];
ps=SubString(string,2,9); 
printf("%sn",ps);  
SubString1(string,temp,2,9);
printf("%sn",temp);
}
char * SubString(char s[],int i,int j)
{
static char temp[100];
int m,n;
for(m=0,n=i;n<=j;m++,n++)
{
temp[m]=s[n];
}
temp[m]='0';
return temp;
}
char *SubString1(char s[] ,char temp[], int i,int j)
{
int m,n;
for(m=0,n=i;n<=j;m++,n++)
{
temp[m]=s[n];
}
temp[m]='0';
}


Related articles: