A brief analysis of the program header files for the introduction to C programming language

  • 2020-05-07 20:05:23
  • OfStack

The header file is a file with the.h extension that contains the declaration and macro definition of the C function and can also be Shared between multiple source files. There are two types of header files: files written by programmers, and files that come with compilers.

Programs that require the use of a header file, including through it, use the C language preprocessor instruction #include which as seen contains the stdio.h header file, which comes with the compiler.

Including 1 header file is equal to copying the contents of the header file, but we don't do this because it's error prone. A good idea is that we don't copy the contents of the header file, especially the source files that include multiple programs.

The simple approach in C or C++ programs is that we put all the constants, macros and system-wide global variables and function prototypes in the header file, which includes as long as it is required to contain the header file.

includes the/function syntax
user and system header files are included using the preprocessing instruction #include. It comes in two forms:


#include <file>

This form is used for system header files. It searches the files for the files specified in the standard list of the system directory. You can add directories to this list before -I option in compiling source code.


#include "file"

This form is the header file for your own program. It searches for named files in the directory that contains the current file. You can add directories to this list before -I option in compiling source code.

includes/function operations
The
#include directive works by instructing the C preprocessor to use the rest of the current source file and then continue to scan the specified file as input. The output from the preprocessor contains the output generated from the text followed by the #include instruction, followed by the output from the contained file. For example, if there is a header file header.h as follows:


char *test (void);

The main program call USES a header file, like program.c:


int x;
#include "header.h"

int main (void)
{
  puts (test ());
}

The compiler will see the same data stream because it will read if program.c


int x;
char *test (void);

int main (void)
{
  puts (test ());
}

1 primary header file
if the header file is included exactly twice, the compiler will process its contents twice, resulting in 1 error. Use standard methods to prevent enclosing the entire contents of the file in 1 condition, as follows:


#ifndef HEADER_FILE
#define HEADER_FILE

the entire header file file

#endif

This build is commonly referred to as wrapper #ifndef. When the header is included again, the condition will be false because HEADER_FILE is defined. The preprocessor skips the entire contents of the file, and the compiler doesn't see it twice.

calculates package letters
Sometimes, it is necessary to select several different 1 header files to be included in the program. They may specify configuration parameters that are used for different types of operating systems, for example. This can be done with a series of 1 conditional sentences, as follows:


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

However, as its content grows it becomes tedious, instead of the preprocessor providing a header file name that USES macros. This is what's called a calculation involving. Instead of writing 1 header name #include as a direct argument, simply replace the name of the macro:


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

SYSTEM_H will be extended, and the preprocessor will look for system_1.h as if #include had been written the original way. SYSTEM_H can be defined with the Makefile-D option.


on header file contains duplicate
1. If the header A contains the header C, and the header B also contains the header C, and the program is written with the header A and the header B, then the header C contains the same error as


This error is simply written in your own header file


#ifndef X  //X For your identifier , Keep only 1 It could be longer, for example  #ifndef _INCLUDE_XXXXXX_H_
#define X

// Here is your header file 

#endif

The above X can be arbitrarily named (it can be used to "load" the header file, so X1 will be the name of the uppercase header file, but "_" instead of "."), just make sure that #ifndef and #define are followed by the same X, just one identifier. If you already have #define X, the rest will not be executed. That in with a c file, you can only # include1 header files, to avoid "in the same 1 c file for 1 header files include twice" error (after the first contains the header files, have already # define X 1 time, 2 times contain when "# ifndef X" judgment, now that you have defined, had to said to the judge yes, behind of no longer perform,
The header file will not be added).

2. In the code, the header file A needs to contain the header file B, and the header file B needs to contain the header file C

Note: the #ifndef preprocessing here is to resolve the error of containing the same header file twice in the same c file, while it is possible to include the same header file in different c files.

If not do tag # ifndef to header files, and want to LaoYongYi "1" to solve the problem, you don't need to worry about repeating contains, then must consciously formation, do not define a variable or function in the header file, statement, only so many times include this header file contains the declaration, there will be no problem, so is to allow multiple times c language statement with a function or variable.