Deep understanding of C language static, extern and pointer function _c language

Source: Internet
Author: User

1.exit (0) Normal exit procedure

Exit (1) program exception exits program

2.static (static variable) modifies local variables

Using static adornments in local variables prolongs the lifetime of local variables. But we need to pay attention to a few points:

• Although the lifetime of a static decorated variable is long, it is always a local variable and cannot be used in other functions
static What is the difference between a global variable and a normal global variable? What is the difference between a static local variable and a normal local variable? What is the difference between a static function and a normal function?
     Global variables (external variables) before the description of the static to form the global variable. The global variable itself is the static storage mode,  static global variable is of course also static storage mode. The two are not different in the way they are stored. The difference between the two is that the scope of the Non-static global variable is the whole 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 restricts its scope, meaning that only valid,  within the source file that defines the variable cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can be used only for functions that are within the source file, thereby avoiding causing errors in other source files, .
     from the above analysis, we can see that the change of the local variable to the static variable is the change of its storage mode that changes its lifetime. Changing the global variable to a static variable changes its scope and limits its use. The
     static functions are different from normal function scopes. Only in this document. Functions that are used only in the current source file should be described as internal functions (static), and internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, you should indicate in a header file that the source file to use these functions contains this header file
     What is the difference between a static global variable and a normal global variable: Static global variables are only first made once, preventing them from being referenced in other file cells; What is the difference between a
     static local variable and a normal local variable: the static local variable is initialized only once, the next time based on the previous result value;
     What is the difference between a static function and a normal function: There is only one copy of the static function in memory, and the normal function maintains a copy in each invocation
3.extern (external variable)   Cosmetic global variable

extern can not only modify variables but also modify functions

The scope of global variables is already very wide, why should you use extern to decorate it? Look at one of the following examples

Copy Code code as follows:

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


In the example above, it is used before the a,b is defined, which means that extern extends the scope of the global variable.

extern is not only the use of the above example, global variables in different files can also be used by extern.

4. Pointer function

Definition: A pointer function is a function of a function type and a return value is a pointer.

The general form of the pointer function:

Type * Function name (argument list)

Let's use an example to realize the magical function of the pointer functions.

Copy Code code as follows:

/* Note:your choice is C IDE/*
#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 ("%s\n", PS);
SubString1 (string,temp,2,9);
printf ("%s\n", temp);
}

char * SubString (char s[],int i,int j)
{
Static char temp[100];/* the temporary array declared by this place must be static, otherwise the value will not be passed.
int m,n;
for (m=0,n=i;n<=j;m++,n++)
{
Temp[m]=s[n];
}
Temp[m]= ' ";
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]= ' ";
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.