"Go" C language correctly uses the extern keyword

Source: Internet
Author: User

  By using the keyword extern, you can refer to a variable or function defined in another file in one file, with a specific example, as described below.

One, referencing variables in the same file
1#include <stdio.h>2 3 intfunc ();4 5 intMain ()6 {7Func ();//18printf"%d", num);//29     return 0;Ten } One  A intnum =3; -  - intfunc () the { -printf"%d\n", num); -}

  If, in this order, the variable num is declared and initialized behind the main function, then the variable of NUM cannot be directly referenced in the main function, because when the compiler compiles to this sentence, the declaration of the variable of NUM cannot be found, but it can be used normally in the Func function. Because the func call to NUM occurs after the declaration and initialization of Num.

What if I don't want to change the position of NUM's declaration, but want to use the NUM variable directly in the main function? You can use the extern keyword. Like this piece of code, using the extern keyword to first declare the NUM variable, tell the compiler num This variable is there, but not before the declaration, you go to find it somewhere else, sure enough, so that can be successfully compiled. But if you want to cheat the compiler is not good, such as you declare the extern int num, but in the back there is no real give the NUM variable declaration, then the compiler went to other places to find, but did not find it or not.

The following program uses the extern keyword to use the variables defined behind.

1#include <stdio.h>2 3 intfunc ();4 5 intMain ()6 {7Func ();//18     extern intnum;9printf"%d", num);//2Ten     return 0; One } A  - intnum =3; -  the intfunc () - { -printf"%d\n", num); -}
Second, referencing a variable in another file

  If the extern keyword is this feature, then this keyword is superfluous, because the top program can be used in the main function by declaring the NUM variable on top of the main function.
extern the true function of this keyword is to refer to a variable or function that is not in the same file.

Main.c

1 #include <stdio.h>23int  main ()4{5     externint  num; 6     printf ("%d", num); 7     return 0 ; 8 }

B.c

1 #include <stdio.h>23int5; 4 5 void func () 6 {7     printf ("fun ina.c"); 8 }

 For example Here B.C defines a variable num, if you want to refer to this variable in MAIN.C, then you can use the extern keyword, note that the reason for the success of this reference is that the NUM keyword is a global variable in B.C, meaning that the extern variable will only work if a variable is a global variable. , it's not going to work like this.

Mian.c

1 #include <stdio.h>23int  main ()4{5     externint  num; 6     printf ("%d", num); 7     return 0 ; 8 }

B.c

1 #include <stdio.h>23void  func ()4{5      int5; 6     printf ("fun ina.c"); 7 }

  In addition, the extern keyword only needs to indicate the type and variable name on the line, can not be re-assigned, initialization needs to be in place of the original file, if not initialized, the global variable will be automatically initialized to 0 by the compiler. It doesn't work like this.  

1 extern int num=4;

  But after the declaration, you can use the variable name to modify it, like this:

1#include <stdio.h>2 3 intMain ()4 {5     extern intnum;6num=1;7printf"%d", num);8     return 0;9}

  If you do not want this variable to be modified, you can use the Const keyword to decorate it as follows:

Mian.c

 1  #include <stdio.h>2  3   main ()  4  { 5  extern  const  int   num;  6  printf ( " %d   " ,num);  7  return  0   8 } 

B.c

1 #include <stdio.h>23constint num=5; 4 void func () 5 {6     printf ("fun ina.c"); 7 }

Using include to include the other file in its entirety can also refer to a variable in another file, but the result is that all the variables and methods in the contained file can be used by this file, which becomes unsafe, It's better if you just want one file to use one of the variables in another file or use the extern keyword.

Iii. referencing a function in another file

  extern can refer to a function in another file in addition to a variable in another file, and the reference method and reference variable are similar.

Mian.c

1 #include <stdio.h>23int  main ()4{5     externvoid  func (); 6     func (); 7     return 0 ; 8 }

B.c

1 #include <stdio.h>23constint num=5; 4 void func () 5 {6     printf ("fun ina.c"); 7 }

  The function func in B.C is referenced here in the main function. Because all functions are global, the extern usage of the function and the modification of the global variable are basically the same, and it is important to note that the type and parameters of the return value need to be indicated.

Transferred from "http://blog.csdn.net/xingjiarong/article/details/47656339"

"Go" C language correctly uses the extern keyword

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.