HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
The header file is the file extension.h, including a C function declarations and macro definitions, cited shared by multiple source files.There are two types of headers: programmers to write header files and compiler comes with header files.
In the program you want to use the header files, use the C preprocessor directive#include to refer to it.We've already seen thestdio.h header file, which is the header file that comes with the compiler.
Quote header file is equivalent to copy the contents of the header file, but we will not copy the contents of the header file directly in the source file, because doing so is prone to error, especially when the program is composed of a plurality of source files.
A simple practice in C or C ++ program, recommended that all constants, macros, global variables and function prototypes system written in the header file when needed at any time refer to these headers.
Using preprocessing directives#include 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 directory. When compiling the source code, you can -I options before the pre directory the list.
#include "file"
This form is used to reference the user header files. It searches for a file named file in the directory containing the current file. When compiling the source code, you can -I options before the pre directory the list.
#include directive instructs the C preprocessor browse the specified file as input.Output of the preprocessor contains the output that has been generated, the referenced text output files generated and output#include instruction.For example, if you have a header file header.h, as follows:
char *test (void);
And a header file using the main programprogram.c,as follows:
int x; #include "header.h" int main (void) { puts (test ()); }
The compiler will see the following token stream:
int x; char *test (void); int main (void) { puts (test ()); }
If a header file is referenced twice, the compiler will process the contents of the two header files, which will generate an error. To prevent this, the standard practice is to put the entire contents of the file on the conditional compilation statements, as follows:
#ifndef HEADER_FILE #define HEADER_FILE the entire header file file #endif
This structure is commonly referred wrapper#ifndef.When referencing the header file again, the condition is false, because HEADER_FILE defined. In this case, the preprocessor will skip the entire contents of the file, the compiler will ignore it.
Sometimes you need to select a reference to the program from several different header files. For example, you need to specify the configuration parameters on different operating systems. You can use a series of conditions to achieve this, as follows:
#if SYSTEM_1 # include "system_1.h" #elif SYSTEM_2 # include "system_2.h" #elif SYSTEM_3 /en. #endif
However, if the header file more time to do so is very inappropriate, use the name preprocessor macros to define headers. This is calleda conditional reference.It is not used as the name of the header file#include direct parameter, you only need to use a macro to replace the name:
#define SYSTEM_H "system_1.h" /en. #include SYSTEM_H
SYSTEM_H expands, the preprocessor looks system_1.h, as originally written as#include.SYSTEM_H Makefile may be defined by the -D option for you.