extern is a keyword in C that is used to declare a variable or function that is defined in another source file or module. It is used to indicate to the compiler that the variable or function is defined somewhere else in the program and to prevent multiple definitions of the same variable or function.

To use extern, we first declare the variable or function in a header file or at the top of a source file using the extern keyword followed by the data type and the name of the variable or function. For example:

extern int my_variable;
extern void my_function();

Then, in another source file where we want to use the variable or function, we simply include the header file or declare the variable or function again using the extern keyword. For example:

#include "my_header.h"

int main() {
    extern int my_variable;
    my_variable = 10;

    extern void my_function();
    my_function();
    return 0;
}

We use extern when we have variables or functions that are defined in one source file and used in another. This allows us to separate our code into smaller modules and to avoid duplicating code. It is also useful for sharing variables or functions across multiple source files and for creating libraries

what is extern in C how to use it and when to use why use it

原文地址: https://www.cveoy.top/t/topic/ecUN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录