C Header file
A header file is a file whose extension is.hThe file contains C function declarations and macro definitions, and is referenced and shared by multiple source files. There are two types of header files: header files written by programmers and header files that come with the compiler.
To use a header file in a program, you need to use the C preprocessor directive.#includeto reference it. We have already seen above.stdio.hHeader file, it is a header file that comes with the compiler.
Referencing a header file is equivalent to copying the contents of the header file, but we do not directly copy the contents of the header file into the source file, because doing so is error-prone, especially when the program consists of multiple source files.
A simple practice in C or C++ programs is to write all constants, macros, system global variables, and function prototypes in header files, and reference these header files whenever needed.
Syntax for referencing header files
Use preprocessor directives#includeIt can reference user and system header files. It has the following two forms:
#include <file>
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 prepend directories to this list using the -I option.
#include "file"
This form is used to reference user header files. It searches for a file named file in the directory containing the current file. When compiling source code, you can prepend directories to this list using the -I option.
The operation of referencing header files
#includeThe directive instructs the C preprocessor to scan the specified file as input. The preprocessor's output contains the already generated output, the output generated by the referenced file, and#includethe text output after the directive. For example, if you have a header file header.h, as follows:
char *test (void);
and a main program that uses the header fileprogram.c, as follows:
int x;
#include "header.h"
int main (void)
{
puts (test ());
}
The compiler will see the following code:
int x;
char *test (void);
int main (void)
{
puts (test ());
}
Reference a header file only once
If a header file is referenced twice, the compiler will process the contents of the header file twice, which will produce errors. To prevent this, the standard practice is to put the entire contents of the file in conditional compilation statements, as follows:
#ifndef HEADER_FILE #define HEADER_FILE the entire header file file #endif
This structure is commonly known as a wrapper#ifndefWhen the header file is referenced again, the condition is false because HEADER_FILE is already defined. At this point, the preprocessor skips the entire contents of the file, and the compiler ignores it.
Conditional inclusion
Sometimes it is necessary to choose one header file from multiple different header files to reference in the program. For example, you need to specify configuration parameters used on different operating systems. You can achieve this with a series of conditions, as follows:
#if SYSTEM_1 # include "system_1.h" #elif SYSTEM_2 # include "system_2.h" #elif SYSTEM_3 ... #endif
But when there are many header files, doing so is very inappropriate. The preprocessor uses macros to define the name of the header file. This is the so-calledConditional inclusionIt does not use the name of the header file as#includedirect parameter. You only need to substitute the macro name:
#define SYSTEM_H "system_1.h" ... #include SYSTEM_H
SYSTEM_H will expand, and the preprocessor will look for system_1.h, just as#includeoriginally written. SYSTEM_H can be defined by your Makefile via the -D option.
Standard library header files
C standard library header files are a set of header files defined by the ANSI C (also known as C89/C90) and ISO C (C99 and C11) standards. These header files provide a large number of functions, macros, and type definitions for common programming tasks such as input/output, string operations, mathematical calculations, memory management, and so on.
The following are some common C standard library header files and a brief introduction to their functions:
| Header file | Function Introduction |
|---|---|
| <stdio.h> | standard input/output library, containingprintf、scanfand other functions |
| <stdlib.h> | Standard library functions, including functions such as memory allocation and program control. |
| <string.h> | string manipulation functions, such asstrlen、strcpywait |
| <math.h> | Math function libraries, such assin、cos、sqrtwait |
| <time.h> | time and date functions, such astime、strftimewait |
| <ctype.h> | character handling functions, such asisalpha、isdigitwait |
| <limits.h> | defines limit values for various types, such asINT_MAXwait |
| <float.h> | defines limit values for floating-point types, such asFLT_MAXwait |
| <assert.h> | Assertion macroassert, for debugging checks |
| <errno.h> | Define error code variableserrnoand related macros |
| <stddef.h> | defines common types and macros, such assize_t、NULLwait |
| <signal.h> | functions and macros for handling signals, such assignalwait |
| <setjmp.h> | macros and functions that provide non-local jump functionality |
| <locale.h> | Locale-related functions and macros, such assetlocalewait |