Deep understanding of C language Static, extern, and pointer functions

Source: Internet
Author: User
Tags exit printf valid variable scope

  This article mainly introduces the C language static, extern and pointer functions, a friend in need can refer to the

1.exit (0) Normal exit program   exit (1) Program exception exit program   2.static (static variable) modified local variable   The local variable using static modification, will prolong the existence of local variables. But we need to pay attention to a few points:   Although the lifetime of a static modifier variable is very 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? The description of the       global variable (external variable) before it is static constitutes a 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, that is, it is 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, only functions within that source file can be public,   avoids causing errors in other source files.       From the above analysis, we can see that changing the local variable to static variable is changing its storage mode, which changes its lifetime. Changing the global variable to a static variable changes its scope and limits its use. The      static function differs from the normal function scope. 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 of the current source file, it should be indicated in a header file that the source file to use these functions should contain the header file      static Global variables are different from ordinary global variables: Static global variables are only first made once, Prevent being referenced in other file units;      static What is the difference between a local variable and a normal local variable: the static local variable is only initialized once, and the next time it is based on the previous result value;       What is the difference between a static function and a normal function: The static function has only one copy in memory, and the normal function maintains aCopy 3.extern (external variable)   Cosmetic global variable   extern not only can you modify variables but also modify functions   global variable scope is already very wide, why do you want to decorate with extern? Look at the following example   code as follows: #include "stdio.h" void Main () {      extern        a;   &NB Sp   extern        b;       printf ("a=%d,b=%d", a,b);  } int a=13,b=5;     In the above example, use it before defining a,b, which means that extern extends the scope of the global variable.   extern Not only the example above, global variables in different files can also be used by extern.   4. Pointer function   Definition: A pointer function is a function that is a function type and a return value is a pointer.   Pointer functions in general form:     type * Function name (parameter list)   below use an example to realize the function of the pointer functions the   code is as follows: * Note:your choice is C IDE * * * #inc Lude "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];/* this placeThe declared temporary array must be static, otherwise the value will not pass to/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.