May 11, 2021 C
2. The syntax of the reference header file
3. The action that refers to the header file
The header file is a file with the extension .h that contains C function declarations and macro definitions and is referenced and shared by multiple source files. There are two types of header files: the header file written by the programmer and the header file that the compiler brings with it.
To use a header file in a program, you need to use the C preprocessing instruction #include to reference it. We've seen the stdio.h header file before, which is the header file that the compiler brings with it.
Referring to a header file is equivalent to copying the contents of a header file, but we don't copy the contents of a header file directly in the source file, because it's easy to make mistakes, especially if the program is made up of multiple source files.
In A simple practice in C or C++ programs, it is recommended that all constants, macros, system global variables, and function prototypes be written in the header files, which should be referenced whenever needed.
Using preprocessing instructions#include can refer to user and system header files. It takes two forms:
#include <file>
This form is used to refer to the system header file. I t searches the standard list of system directories for files named file. When compiling the source code, you can pre-position the directory in front of the list with the -I option.
#include "file"
This form is used to refer to user header files. I t searches the directory that contains the current file for a file. When compiling the source code, you can pre-position the directory in front of the list with the -I option.
#include instructs the C preprocessor to browse the specified files as input. T he output of the preprocessor contains the output that has already been generated, the output generated by the referenced file, and the text output #include the instruction after the instruction. For example, if you have a header file header.h, here's what:
char *test (void);
and a master program program that uses a header file.c, as follows:
int x;
#include "header.h"
int main (void)
{
puts (test ());
}
The compiler sees the following token flow:
int x;
char *test (void);
int main (void)
{
puts (test ());
}
If a header file is referenced twice, the compiler processes the contents of the header file twice, which results in an error. To prevent this, it is standard practice to place the entire contents of the file in a conditional compilation statement, as follows:
#ifndef HEADER_FILE
#define HEADER_FILE
the entire header file file
#endif
This structure is commonly referred to as the #ifndef. W hen the header file is referenced again, the condition is false because HEADER_FILE is defined. At this point, the preprocessor skips the entire contents of the file and the compiler ignores it.
Sometimes you need to select a reference from several different header files into your program. F or example, you need to specify the configuration parameters that are used on different operating systems. You can do this with a number of conditions, such as:
#if SYSTEM_1 # include "system_1.h" #elif SYSTEM_2 # include "system_2.h" #elif SYSTEM_3 ... #endif
However, if this is not appropriate when there are more header files, the preprocessor uses macros to define the name of the header file. T his is called conditional reference. It is not a direct parameter of #include with the name of the header file, you only need to use the macro name instead:
#define SYSTEM_H "system_1.h" ... #include SYSTEM_H
SYSTEM_H expands, the preprocessor looks for system_1.h, as #include was originally written. SYSTEM_H can be defined by your Makefile with the -D option.