Header File in C

A file header is a file with the .h format containing function declarations and macro definitions and can be shared across multiple source files. There are two types of file headers: the file that the programmer writes and the file that comes with your compiler.

A file header is a file with the .h format containing function declarations and macro definitions and can be shared across multiple source files. There are two types of file headers: the file that the programmer writes and the file that comes with your compiler.

You require using the file Header in the program by adding it to the program, with the #include preprocessor character as you add stdio.h to the Header file, it will accompany your compiler.

Including the Header file is equivalent to copying the Header file's content, but you don't need to do it, just #include, your code will be more compact and still use the content of the Header file.

In fact C and C ++ programs we store most constants and macros and global variables and function prototypes in the Header files and include these files when you need to use them.

Syntax include in C

Both the user file and the system header are included using the #include preprocessor instruction. Here are 2 types:

 chứa 

This format is used for system files . It will find the file with the file name in the list of system directories.

 include "file" 

This format is used for files in your program. It will search for files with the file name in the same directory as the current file .

Include activities

The #include directive works by directing the preprocessor in the C language to scan certain files to add to the current file before starting with the existing source code. For example, if you have a Header file as header.h as follows:

 char * test (void); 

and the main program calls program.c to use the File header, like:

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

and the compiler will see the same token stream, when the program.c program reads as follows:

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

Once-Only Header

If the file header is included twice, the compiler will report an error and print an error. The standard way to avoid this case is to use conditional expressions as follows:

 ifndef HEADER_FILE 
#define HEADER_FILEthe entire header file file # endif

In case it is already included, the above program will not include it again.

Include with the conditions

Sometimes you need to choose one of the headers to include in the program, you must have the operating system configuration parameter to use. You can do this with a range of conditions as follows:

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

But when this number of conditions is many, it becomes tedious, instead the preprocessor provides the ability to use a macro for the Header name. This is called Conditional Include . Instead of writing a Header name as the direct parameter of #include , you simply put a macro name there instead of it:

 define SYSTEM_H "system_1.h" 
.
#include SYSTEM_H

SYSTEM_H will be expanded, and the preprocessor will search for system_1.h if #include has been written that way initially. SYSTEM_H can be defined by the file you create with -D option.

According to Tutorialspoint

Previous article: The preprocessor in C

Next lesson: Press type in C

4 ★ | 1 Vote