This article introduces the usage of extern keyword in C/C. It mainly talks about the usage when modifying variables. You can refer to it. , hope it helps you.
1. Basic explanation
extern can be placed before a variable or function to indicate that the definition of the variable or function is in another file, prompting the compiler to encounter this variable or function When looking for its definition in other modules. In addition, extern can also be used to link specifications.
extern has two functions: (1) When it is used together with "C", such as: extern "C" void func(int a); it tells the compiler to follow the rules of C when compiling the func function name. To translate the corresponding function name instead of C. Regarding this point, you may be able to find the answer in the book "In-depth Exploration of the C Object Model"; in addition, there is a backtrace function under Linux that can print stack information and view the C translated function name (this function is used in the muduo library) .
(2) When extern is not used together with "C" to modify a variable or function, such as in the header file: extern int g_Int; its function is to declare the scope of the function or global variable keyword, and the declared Functions and variables can be used in this module or other modules, remember that it is a declaration not a definition!
2. When extern modifies variables
The correct way to use it is: define the variable in the .c file and declare it in the corresponding .h file.
We determine whether it is a declaration or a definition by whether memory space will be allocated for the variable (strictly speaking, it is simply allocating memory and does not include the initialization part). So does the sentence int i; declare or define it? It is both a statement and a definition. If we use this sentence in the test.h file, once i is defined in other files (e.g.1), or the file is included repeatedly (e.g.2), a redefinition error will occur.
/* e.g.1 以下为3个文件 */ //test.h int i; //test2.h int i; //main.cpp #include "test.h" #include "test2.h" int main() { return 0; }
/* e.g.2 以下为3个文件 */ //test.h int i; //test2.h #include "test.h" //main.cpp #include "test.h" #include "test2.h" int main() { return 0; }
Related tutorials: C video tutorial
The above is the detailed content of How to use extern keyword in C/C++. For more information, please follow other related articles on the PHP Chinese website!

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software