Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Header Files

C Header Files

Header files are files with the extension .h which contain C function declarations and macro definitions and are shared by reference among multiple source files. There are two types of header files: those written by the programmer and those that come with the compiler.

To use a header file in a program, you need to use the C preprocessor directive #include to include it. Earlier we have seen the stdio.h header file, which is a header file that comes with the compiler.

Including a header file is equivalent to copying the content of the header file, but we do not directly copy the content 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 include these header files whenever needed.

Syntax for including header files

Use the preprocessor directive #include to include user and system header files. It has the following two forms:



#include 

This form is used to include system header files. It searches for a file named file in the standard list of system directories. When compiling the source code, you can prepend directories to this list with the -I option.



#include "file"

This form is used to include user header files. It searches for a file named file in the directory containing the current file. When compiling the source code, you can prepend directories to this list with the -I option.

The operation of including header files

#include The directive instructs the C preprocessor to browse the specified file as input. The output of the preprocessor contains the output already generated, the output generated by the included file, and the #include 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 file program.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 ());

}

Include a header file only once

If a header file is included twice, the compiler processes the content of the header file twice, which will produce errors. To prevent this, the standard practice is to place the entire content of the file within a conditional compilation statement, as follows:



#ifndef HEADER_FILE

#define HEADER_FILE



the entire header file file



#endif

This structure is what is commonly called the wrapper #ifndefWhen the header file is included again, the condition is false because HEADER_FILE is defined, so the preprocessor skips the entire content of the file and the compiler ignores it.

Conditional inclusion

Sometimes you need to choose one from several different header files to include in your program. For example, you may need to specify configuration parameters to use on different operating systems. You can do 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, this approach is not appropriate. The preprocessor uses macros to define the names of header files. This is calledConditional inclusion. Instead of using the header file name as the #include direct argument, you only 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 as #include if it had been written that way originally. SYSTEM_H can be defined by your Makefile with 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 handling common programming tasks such as input/output, string manipulation, mathematical computation, and memory management.

The following are some common C standard library header files and a brief introduction to their functions:

Header File Function description
Standard input/output library, containing printf, scanf and other functions
Standard library functions, including memory allocation, program control, and other functions
String manipulation functions, such as strlen, strcpy etc.
Math function library, such as sin, cos, sqrt etc.
Time and date functions, such as time, strftime etc.
Character handling functions, such as isalpha, isdigit etc.
Defines limit values of various types, such as INT_MAX etc.
Defines limit values for floating-point types, such as FLT_MAX etc.
Assertion macro assert, used for debugging checks
Defines error code variables errno and related macros
Defines general types and macros, such as size_t, NULL etc.
Functions and macros for handling signals, such as signal etc.
Macros and functions that provide non-local jump functionality
Localization-related functions and macros, such as setlocale etc.