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

AI Hentai Generator
Generate AI Hentai for free.

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



In C language, special characters are processed through escape sequences, such as: \n represents line breaks. \t means tab character. Use escape sequences or character constants to represent special characters, such as char c = '\n'. Note that the backslash needs to be escaped twice. Different platforms and compilers may have different escape sequences, please consult the documentation.

In C, the char type is used in strings: 1. Store a single character; 2. Use an array to represent a string and end with a null terminator; 3. Operate through a string operation function; 4. Read or output a string from the keyboard.

In C language, the main difference between char and wchar_t is character encoding: char uses ASCII or extends ASCII, wchar_t uses Unicode; char takes up 1-2 bytes, wchar_t takes up 2-4 bytes; char is suitable for English text, wchar_t is suitable for multilingual text; char is widely supported, wchar_t depends on whether the compiler and operating system support Unicode; char is limited in character range, wchar_t has a larger character range, and special functions are used for arithmetic operations.

The usage methods of symbols in C language cover arithmetic, assignment, conditions, logic, bit operators, etc. Arithmetic operators are used for basic mathematical operations, assignment operators are used for assignment and addition, subtraction, multiplication and division assignment, condition operators are used for different operations according to conditions, logical operators are used for logical operations, bit operators are used for bit-level operations, and special constants are used to represent null pointers, end-of-file markers, and non-numeric values.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

In C language, char type conversion can be directly converted to another type by: casting: using casting characters. Automatic type conversion: When one type of data can accommodate another type of value, the compiler automatically converts it.

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

The char array stores character sequences in C language and is declared as char array_name[size]. The access element is passed through the subscript operator, and the element ends with the null terminator '\0', which represents the end point of the string. The C language provides a variety of string manipulation functions, such as strlen(), strcpy(), strcat() and strcmp().
