C language 丨 the correct use of extern keywords in detail

Using the keyword extern, variables or functions defined in another file can be quoted in one file. The following is a classification description based on specific examples.

 

1. Referencing variables in the same file

#include<stdio.h>

int func();

int main ()

{

  func(); //1

  printf("%d",num); //2

  return 0;

}

int num = 3;

int func()

{

  printf("%d\n",num);

}

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

What if I don't want to change the position of the num declaration, but want to use the variable num directly in the main function? You can use the keyword extern. Like the following piece of code, use the extern keyword to declare the num variable first, tell the compiler that the variable num exists, but it was not declared before. You can find it elsewhere. Sure enough, you can pass it smoothly. Compile it. But if you want to deceive the compiler, for example, you declare extern int num; but there is no real declaration of the num variable later, then the compiler looks for it elsewhere, but it still won’t work if you don’t find it.

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

#include<stdio.h>

int func();

int main ()

{

  func(); //1

  extern int num;

  printf("%d",num); //2

  return 0;

}

int num = 3;

int func()

{

  printf("%d\n",num);

}

Two, reference variables in another file

If the keyword extern has this function, then this keyword is redundant, because the program above can be used in the main function by declaring the num variable above the main function. 

The real function of the keyword extern is to reference variables or functions that are not in the same file.

main.c

#include<stdio.h>

int main ()

{

  extern int num;

  printf("%d",num);

  return 0;

}

b.c

#include<stdio.h>

intnum = 5;

voidfunc ()

{

  printf("fun in a.c");

}

For example, here is a variable num defined in bc. If you want to reference this variable in main.c, you can use the keyword extern. Note that the reason for successful reference is that the keyword num is a global variable in bc , That is to say, only when a variable is a global variable, the extern variable will work, and the following will not work.

mian.c

#include<stdio.h>

int main ()

{

  extern int num;

  printf("%d",num);

  return 0;

}

b.c

#include<stdio.h>

void func()

{

  int num = 5;

  printf("fun in a.c");

}

In addition, the extern keyword only needs to specify the type and variable name, and it cannot be re-assigned. The initialization needs to be carried out where the original file is located. If it is not initialized, the global variable will be automatically initialized to 0 by the compiler. Writing like this will not work.

extern int num = 4;

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

#include<stdio.h>

int main ()

{

  extern int num;

  num = 1;

  printf("%d",num);

  return 0;

}

If you don't want this variable to be modified, you can use the const keyword to modify it, as follows:

mian.c

#include<stdio.h>

int main ()

{

  extern const int num;

  printf("%d",num);

  return 0;

}

b.c

#include<stdio.h>

const int num = 5;

void func()

{

  printf("fun in a.c");

}

Using include to include another file can reference variables in another file, but the result of this is that all variables and methods in the included file can be used by this file, which becomes unsafe. If you just want one file to use a variable in another file, it is better to use the extern keyword.

Three, reference a function in another file

In addition to referencing variables in another file, extern can also refer to functions in another file. The method of referencing is similar to referencing variables.

mian.c

#include<stdio.h>

int main ()

{

  external void func ();

  func();

  return 0;

}

b.c

#include<stdio.h>

const int num = 5;

void func()

{

  printf("fun in a.c");

}

Here the main function refers to the function func in bc. Because all functions are global, the extern usage of functions is basically the same as the modification of global variables. It should be noted that the type and parameters of the return value need to be specified.

The above is a detailed explanation of the correct use of the extern keyword in the C language introduced by the editor, and I hope it will be helpful to you.

 

If you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster!
[ C language C++ learning penguin circle ], share (source code, project actual combat video, project notes, basic introductory tutorial)
welcome partners who change careers and learn programming, use more information to learn and grow faster than you think!

Programming learning books:

 

Programming learning video:

 

Guess you like

Origin blog.csdn.net/Hsuesh/article/details/112391266