[Study Notes] [C Language] functions of static and extern, staticextern

Source: Internet
Author: User

[Study Notes] [C Language] functions of static and extern, staticextern

If a program contains multiple source files (. c), multiple target files (. obj), these target files cannot be run independently, because these target files may be associated, such as. obj may call c. A function defined in obj. Only when these Associated target files are linked together can an executable file be generated.

External functions:If the function defined in the current file allows other files to access and call, it is called an external function. According to the C language, external functions with the same name are not allowed.
Internal functions:If the function defined in the current file cannot be accessed or called by other files, it can only be used internally. The C language specifies that different source files can have internal functions with the same name and do not interfere with each other.

1. static
* When defining a function, you can declare the function as an internal function (also called a static function) by adding static on the far left of the function ), in this way, the function can only be used in the file where its definition is located. If there are internal functions with the same name in different files, they do not interfere with each other.
* Static can also be used to declare an internal function.
 
2. extern
* When defining a function, if the keyword extern is added to the leftmost of the function, this function is an external function and can be called by other files. The C language specifies that if extern is omitted when defining a function, it is implicitly an external function.
* To call external functions in other files in a file, you need to declare the external function with extern in the current file, and then you can use it. The extern here can also be omitted.

3. Code

Main. c file

1/* 2 external functions: the defined function can be accessed by this file and other files. 3 1> by default, all functions are external functions. 4 2> external functions with the same name are not allowed. 5 6 internal functions: the defined function can only be accessed by this file, and other files cannot access 7 1> allow different files to have internal functions with the same name 8 9 static to function: 10 1> define an internal function 11 2> declare the role of an internal function 12 13 extern on the function: 14 1> completely define an external function 15 2> completely declare an external function 16 (extern can be omitted. By default, both the declared and defined functions are external functions) 17 */18 19 20 // declare a test function 21 // completely declare an external function 22 // extern can be omitted 23 // extern void test (); 24 void test (); 25 26 // void test2 (); 27 28 int main () 29 {30 test (); 31 32 // test2 (); 33 return 0; 34} 35 36 // void test () 37 // {38 // 39 //} 40 static void test2 () 41 {42 43}

One. c file

1 # include <stdio. h> 2 3 // declare an internal function 4 static void test2 (); 5 6 // completely define an external function 7/* 8 extern void test () 9 {10 printf ("test function \ n called"); 11} */12 // by default, all functions are external functions, therefore, extern13 void test () 14 {15 printf ("the test function \ n" is called); 16 17 test2 (); 18} 19 20 21 // define an internal function 22 static void test2 () 23 {24 printf ("test2 function \ n called"); 25}

 

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.