" and "#include "filename""."/> " and "#include "filename"".">
search
HomeBackend DevelopmentC#.Net TutorialWhat is the usage of include in C language?

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!

Statement
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
C# and the .NET Runtime: How They Work TogetherC# and the .NET Runtime: How They Work TogetherApr 19, 2025 am 12:04 AM

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.

C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

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.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

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

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

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.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

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

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment