Introduction to static, extern and pointer functions in C Language

Source: Internet
Author: User

1. exit (0) exit the program normally

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

2. modify local variables with static variables

When local variables are modified using static, the existence of local variables is prolonged. But pay attention to the following points:

  • Although static modifier variables have a long lifetime, they are always local variables and cannot be used in other functions.
  • What is the difference between static global variables and common global variables? What is the difference between static local variables and common local variables? What is the difference between a static function and a common function?
    The description of global variables (external variables) is preceded by static to form a static global variable. Global variables are static storage, and static global variables are also static storage. The two are not different in storage methods. The difference between the two lies in that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided.
    From the above analysis, we can see that after a local variable is changed to a static variable, its storage mode is changed, that is, its survival time is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use.
    Static functions have different scopes than normal functions. Only in this file. Only functions used in the current source file should be declared as internal functions (static). Internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, it should be described in a header file that the source file to use these functions must contain this header file.
    What is the difference between static global variables and common global variables? static global variables are only made once to prevent being referenced in other file units;
    What is the difference between static local variables and common local variables: static local variables are initialized only once, and the next time is based on the previous result value;
    What is the difference between a static function and a common function: a static function has only one copy in the memory, and a normal function maintains one copy in each call.

3. extern (external variable) modifies global variables

Extern can not only modify variables but also modify functions.

Global variables have a wide range. Why do we need to use extern to modify them? Let's look at the following example.

# Include "stdio. h"

Void main ()

{

Extern;

Extern B;

Printf ("a = % d, B = % d", a, B );

}

Int a = 13, B = 5;

 

In the above example, we use it before defining a and B, that is, extern extends the scope of global variables.

Extern is used not only in the preceding example, but also in global variables in different files.

4. pointer Functions

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

The general form of pointer functions:

Type * function name (parameter list)

The following is an example of how to use a pointer function.

/* 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 declared temporary array must be static, otherwise the value will not be transferred */

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 Article

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.