A brief analysis of the program header file of C language programming _c language

Source: Internet
Author: User

A header file is a file with an. h extension that contains declarations and macro definitions for C functions, and can also be shared among multiple source files. There are two types of header files: The files that the programmer writes, and the files that are included with the compiler.

A program that requires the use of a header file, including through it, uses the C language preprocessing instructions #include as seen in the containing Stdio.h header file, which is brought along with the compiler.

Include a header file equal to the contents of the copy header file, but we don't do it because it's so easy to go wrong, a good idea is we don't copy the contents of header files, especially the source files that include multiple programs.

The simple practice of C or C + + programs is that we put all the constants, macro-system global variables and function prototypes in the header file, which includes as long as it requires the inclusion of header files.

Include/function grammar
user and System header files are included using preprocessing directives #include. It has the following two kinds of forms:

#include <file>

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

#include "file"

This form is the header file for your own program. It searches for files named in the directory containing the current file. You can add the previous directory to this list-I option to compile the source code.

Include/letter operation
#include指令的工作原理是指导C预处理器使用当前的源文件的其余部分, and then continue scanning the specified file as input. The output from the preprocessor contains the output that followed the text from the #include directive, and then the output from the contained file. For example, if there is a header file header.h as follows:

Char *test (void);

The main program calls using a header file, similar to PROGRAM.C:

int x;
#include "header.h"

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

The compiler will see the same stream of data, as it would if PROGRAM.C read

int x;
Char *test (void);

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

Disposable header File
if the header file is contained exactly two times, the compiler will process its contents two times, resulting in an error. Use standard methods to prevent the entire contents of the enclosing file from being in one condition:

#ifndef header_file
#define Header_file The

entire HEADER file file

#endif

This builder is often referred to as the wrapper #ifndef. When the header is contained again, the condition will be false because of the header_file definition. The preprocessor will skip over the entire contents of the file, and the compiler will not see it two times.

Calculation Package Letter
Sometimes, it is necessary to select a few different header files to be included in the program. They may specify configuration parameters that are used on different types of operating systems, such as. You can do this with a series of 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, replacing the preprocessor with the header file name that uses the macro. That's what the so-called calculations include. Instead of writing a header filename #include a direct argument, simply replace the name of the macro:

 #define SYSTEM_H "System_1.h" ...
 #include system_h

System_h will be expanded while the preprocessor will look for system_1.h as if the #include has been written in the original way. System_h can be defined by the MAKEFILE-D option.


about header file repeat contains
1. If header file A contains header file C, and header file B also contains header file C, the written program contains header file A, header file B, and then the header file C repeat contains errors when compiling.
This error is only written in the header file that you wrote

#ifndef x  //x for your identifier, keep it unique, can be long, such as #ifndef _include_xxxxxx_h_
#define X

//Here is your header file

#endif

The above x can be arbitrarily named (can be used to "load" header file, so x is generally named as uppercase header file name, but the "." Replace with "_", as long as the guarantee #ifndef and #define behind is the same x can be, just a logo. If you have #define X, you will not follow it. So in the same C file, the guarantee can only #include a header file, avoid "in the same C file with a header file include two times" error (first included the header file, it has been #define X, the second time it will encounter "#ifndef X" judgment , now that we have defined it, we have to say yes to this judgment, and the following is no longer enforced,
Header files will not be added again).

2, write code, header file a need to include header file B, header file B need to include header file C, this situation #include A is no problem, do not appear duplicate included error

Note: The #ifndef preprocessing here is to resolve errors that contain two of the same header files in the same C file, and they can contain the same headers in different C files.

If you do not have to #ifndef to the right file to mark, and want to "once and for all" to solve the problem, do not need to worry about duplication of inclusion, it is necessary to form a conscious, do not define a variable or function in the header file, only to declare, so many times include this header file contains So the C language is allowed to declare the same function or variable multiple times.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.