English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Other

C Language Reference Manual

C Language Header Files

Header files are files with the extension .h The file contains C function declarations and macro definitions, which are shared and referenced by multiple source files. There are two types of header files: header files written by programmers and header files built-in by the compiler.

To use header files in a program, you need to use C preprocessor directives #include to reference it. We have already seen stdio.h Header files, which are the header files built-in by the compiler.

Referencing a header file is equivalent to copying the content of the header file, but we will not directly copy the content of the header file in the source file, because doing so is easy to make mistakes, especially when the program is composed of multiple source files.

A simple practice in C or C++++ 程序中,建议把所有的常量、宏、系统全局变量和函数原型写在头文件中,在需要的时候随时引用这些头文件。

In the program, it is recommended to write all constants, macros, system global variables, and function prototypes in header files and refer to these header files as needed.

The syntax for including header files #include Using preprocessor directives

User and system header files can be referenced. Its form has the following two types:

#include <file> -This form is used to reference user header files. It searches for a file named file in the directory of the current file. When compiling source code, you can use

This form is used to reference system header files. It searches for a file named file in the standard list of system directories. When compiling source code, you can use

#include "file" -This form is used to reference user header files. It searches for a file named file in the directory of the current file. When compiling source code, you can use

The I option places the directory at the front of the list.

#include the operation of including the header file #include Instructions will instruct the C preprocessor to browse the specified file as input. The preprocessor's output includes the generated output, the output generated by the referenced file, and

char *test(void);

and a main program that uses the header file program.c, as follows:

int x;
#include "header.h"
int main(void)
{
   puts(test());
}

The compiler will see the following code information:

int x;
char *test(void);
int main(void)
{
   puts(test());
}

Only include the header file once

If a header file is referenced twice, the compiler will process the content of the header file twice, which will produce an error. To prevent this, the standard practice is to place the entire content of the file within conditional compilation statements, as follows:

#ifndef HEADER_FILE
#define HEADER_FILE
the entire header file file
#endif

This structure is usually called a wrapper #ifndef. When the header file is referenced again, the condition is false because HEADER_FILE is already defined. In this case, the preprocessor will skip the entire content of the file, and the compiler will ignore it.

Conditional inclusion

Sometimes it is necessary to select one reference from multiple different header files to be included in the program. For example, it is necessary to specify configuration parameters for different operating systems. You can achieve this by a series of conditions, as follows:

#if SYSTEM_1
   # include "system_1.h"
#elif SYSTEM_2
   # include "system_2.h"
#elif SYSTEM_3
   ...
#endif

However, when there are too many header files, it is not appropriate to do so, the preprocessor uses macros to define the names of header files. This is what is calledConditional inclusion. It is not defined by the name of the header file as #include The direct parameter, you just need to use the macro name instead:

 #define SYSTEM_H "system_1.h"
 ...
 #include SYSTEM_H

SYSTEM_H will be expanded, and the preprocessor will look for system_1.h, just like #include Written originally like this. SYSTEM_H can be accessed through -Option D is defined in your Makefile.