The role of extern in c language

下次还敢
Release: 2024-05-09 10:09:17
Original
851 people have browsed it

The extern keyword is used in C language to declare externally defined variables, functions or other symbols to avoid repeated definition errors and enhance code scalability. It allows an external symbol to be declared, allocated memory and referenced in the current source file even if it is not exactly defined.

The role of extern in c language

The role of extern keyword in C language

extern keyword is C A key keyword in the language used to declare that a variable, function, or other symbol is defined outside the current source file.

Function:

  • Avoid duplicate definitions: extern tells the compiler that the symbol exists in other source files and does not need to be in the current source file to prevent duplicate definition errors.
  • Declaring external symbols: extern allows us to declare an external symbol in the current source file even if we don't know its exact definition, allocate memory for it and reference it.
  • Increased extensibility: By separating symbol definition from use, extern enhances the scalability of the code, allowing the code to be modified and maintained in different source files.

Syntax:

<code class="c">extern [类型] [符号名];</code>
Copy after login
Among them:

  • Type: The type of symbol, such as int, Char et al.
  • Symbol name: The symbol name that needs to be declared.

Usage scenarios:

extern keyword is often used in the following scenarios:

    Declare functions or variables in header files, Then use them in different source files.
  • Declare functions or variables defined in a dynamic link library (DLL).
  • Declare functions or variables defined in external libraries.

Example:

<code class="c">// 定义一个名为"x"的变量,并将其存储在外部源文件中
extern int x; // 在当前源文件中声明该变量

// 使用定义在其他源文件中的"f"函数
extern void f();</code>
Copy after login

The above is the detailed content of The role of extern in c language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!