[C language-6] static & extern

Source: Internet
Author: User

A. extern function


A c file generates an OBJ file external function: A function that allows other files to be accessed, called (the default function is an external function), and does not allow the presence of an external function with the same name my.c
1//define a extern function perfectly2 void extern testEx () 3 {4     printf ("my.c ==> call external function\n"); 5}6  
Main.c
1//declare the function first to apply the C99 compiler standard2 void extern testEx (); 3 4 int main (int argc, const char * argv[]) {5     testEx (); 6     return 0;7}
B. Static function Intrinsics: Functions that can only be used in the current file, different source files may have intrinsic functions with the same name can define intrinsic functions using static modifiers one.c
1 static void One () 2 {3     printf ("One.c ==> one () \ n"); 4}5  
C.extern with global variables in Java can be called after defining variable C cannot use global variables before definition PS: definition and declaration are two different steps
1//first solution, define the variable before calling 2//int A; 3  4//declare a variable 5 extern int A; 6  7 int main (int argc, const char * argv[]) {8     9     a = 10;10    1 1     return 0;12}13//define the variable15 int A;

If you want to use before a global variable declaration, you need to use the extern modifier variable, which indicates that the variable is about to be defined below.
1 void Test () 2 {3     extern int b;4     printf ("b =%d\n", b); 5}6 7 int b = 3;
D.static and global variables define and declare an internal variable that can only be accessed by this file, cannot be accessed by other files e.static and local variables are static modified local variables, life cycle can be extended to the end of the program but the scope has not changed
1 void Test () 2 {3     static int e = 10;4     e++;5     printf ("E =%d\n", e); 6}
Main calls Test ();
Test ();
Test (); Out:e = 11
E = 12
E = F. Global/Local variables: variables can be declared inside or outside the function A. Global variables: Global variables (outside functions) can be declared repeatedly, cannot be repeatedly defined int a;//marked as weak, allow to define int a = 4;//succuss, mark as strong,  This variable is the standard of a global variable with the same name, and does not allow for multiple definitions of int a = 5;//error If there is a global variable with the same name in a different source file, it refers to the same variable B. Local variables: local variables (within functions) cannot be repeated the effect of internal variables on external variables can be distinguished by two variables Static & extern summarize extern to StatementA global variable, but cannot definition?-"Can declare, cannot duplicate define static can declare and define an internal variable?"

[C language-6] static & extern

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.