What is the usage of include in C language?
#What is the usage of include in c language?
1. Introduction to #include command
#include command is a kind of preprocessing command. The preprocessing command can convert other source codes into Content is inserted into the specified location; it can identify a certain piece of program code that will only be compiled under specific conditions;
can define macros with similar identifier functions. During compilation, the preprocessor will use Other text replaces the macro.
2. Insert the contents of the header file
##include command tells the preprocessor to insert the contents of the specified header file into the corresponding location of the preprocessor command . There are two ways to specify the header file to be inserted:
#include <文件名> #include "文件名"
If you need to include the standard library header file or the header file provided by the implementation version, the first format should be used. As shown in the following example:
#include <math.h> // 一些数学函数的原型,以及相关的类型和宏
If you need to include source files developed for the program, you should use the second format. Files inserted using the #include command usually have a .h file extension. The files include function prototypes, macro definitions and type definitions.
These definitions can be used by any source file as long as the #include command is used. As shown in the following example:
#include "myproject.h" // Function prototypes, type definitions and macros used in the current project
You can use macros in the #include command. If a macro is used, the macro's substitution must ensure that the correct #include command is generated.
Example 1 shows such an #include command.
[Example 1] Macros in the #include command
#ifdef _DEBUG_ #define MY_HEADER "myProject_dbg.h" #else #define MY_HEADER "myProject.h" #endif #include MY_HEADER
When the above program code enters preprocessing, if the DEBUG macro has been defined, then the preprocessor The contents of myProject_dbg.h will be inserted; if it has not been defined, the contents of myProject.h will be inserted.
3. How the preprocessor finds the header file
The search path for the file specified by the #include command is determined by the given C language implementation version. At the same time, it is also up to the implementation version to determine whether the file name is case-sensitive. For files specified using angle brackets () in the command, the preprocessor usually searches in specific system paths. For example, on Unix systems, the paths /usr/local/include and /usr/include are searched.
For files specified in double quotes ("filename") in the command, the preprocessor usually first looks in the current directory, which is the directory containing other source files for the program. If not found in the current directory, the preprocessor will also search the system's include path. The file name can contain a path. But if the file name contains a path, the preprocessor will only look in that directory.
You can also specify your own search path for the #include command by using compiler command line options or by adding the search path to an environment variable (usually called INCLUDE). For specific methods, please refer to the documentation of the compiler used.
4. Nested #include commands
#include commands can be used nested; that is, the source file itself inserted through the #include command You can also include another #include command. The preprocessor allows up to 15 levels of nested includes.
Because a header file sometimes contains another header file, it is easy for the same file to be included multiple times. For example, suppose the file myProject.h contains the following code:
#include <stdio.h>
If the source file contains the following #include command, stdio.h will be included twice, once directly and once indirectly:
#include <stdio.h> #include "myProject.h"
However, you can use conditional compilation commands to conveniently avoid including the same file multiple times. Example 2 uses this technique.
[Example 2] Avoid multiple inclusions
#ifndef INCFILE_H_ #define INCFILE_H_ /* ...实际的头文件incfile.h的内容写在这里... */ #endif /* INCFILE_H_ */
The first time a command containing incfile.h appears, the INCFILE_H_ macro is not defined. The preprocessor therefore inserts the content between #ifndef and #endif, which contains the definition of the INCFILE_H_ macro. After embedding the incfile.h file, the #ifndef condition will be false and the preprocessor will ignore the content between #ifndef and #endif.
Recommended tutorial: "C Language"
The above is the detailed content of What is the usage of include in C language?. For more information, please follow other related articles on the PHP Chinese website!

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

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.

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

.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.

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).
