Correct use of extern keywords in C language

  • 2020-06-15 10:04:04
  • OfStack

Using the keyword extern, you can refer to variables or functions defined in one file and another file. The following is a classification description combining specific examples.

1. Reference a variable 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 that order variable num behind main function declaration and initialization, then in main function is not directly quoting num this variable, because when the compiler to this one sentence, can't find num this variable declaration, but in func function can be used normally, because func calls to num occurred after num declaration and initialization.

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 extern keyword. For example, in this code, the extern keyword is used to declare the num variable under 1 first, telling the compiler that the variable exists, but was not declared before that, you should look for it elsewhere, and sure enough, it will compile. But you can't fool the compiler either if you say extern int num; But there is no actual declaration of the num variable, so the compiler looks elsewhere, but it won't work.

The following program USES the extern keyword with the variables defined below.


#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);
}

2. Reference a variable in another file

If that's what the extern keyword does, then the keyword is redundant because the above program can make the main function usable by declaring the num variable above the main function.
The real purpose of the extern keyword is to refer to 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>

int num = 5;

void func()
{
  printf("fun in a.c");
}

b. Here, for example, defines a variable c num, if main c want to reference the variable, you can use the keyword extern, note that there can be a successful reference because num. The key word in b c is a global variable, that is to say, only when a variable is a global variable, extern variables to be effective, is not to like this.

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 cannot be re-assigned. Initialization needs to be done in the original file, and global variables will be automatically initialized to 0 by the compiler if they are not initialized. I can't write it this way.


extern int num=4;

After the declaration, however, you can modify it using the variable name, like this:


#include<stdio.h>

int main()
{
  extern int num;
  num=1;
  printf("%d",num);
  return 0;
}

If you do not want this variable to be modified, you can modify it using the const keyword, 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 will another one file included can refer to all the other variables in a file, but the result is that contained in the file all the variables and methods can be used by the file, so that it becomes unsafe, if just want 1 files use another 1 file of a variable or use extern keyword is better.

3. Reference a function in another file

extern can refer to a function in another file in addition to a variable in another file, in a similar way to a reference variable.

mian.c


#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);
}
0

b.c


#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);
}
1

Here the main function refers to func in ES112en.c. Since all functions are global, the use of extern for functions is much the same as the modification of global variables. Note that you need to specify the type and parameters of the return value.


Related articles: