Header file

From Seo Wiki - Search Engine Optimization and Programming Languages
Jump to navigationJump to search

In computing, header files are a feature of some programming languages (most famously C and C++) that allows programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers. Programmers who wish to declare standardized identifiers in more than one source file can place such identifiers in a single header file, which other code can then include whenever the header contents are required. The C standard library and C++ standard library traditionally declare their standard functions in header files.

Newer compiled languages (such as Java, C#) do not use forward declarations; identifiers are recognized automatically from source files and read directly from dynamic library symbols. This means header files are not needed.

Motivation

In most modern computer programming languages, programmers can break up programs into smaller components (such as classes and subroutines) and distribute those components among many translation units (typically in the form of physical source files), which the system can compile separately. Once a subroutine needs to be used somewhere other than in the translation unit where it is defined, a way to refer to it must exist. For example, a function defined in this way in one source file:

int add(int a, int b) {
	return a + b;
}

may be declared (with a function prototype) and then referred to in a second source file as follows:

int add(int, int);
 
int triple(int x){
	return add(x, add(x, x));
}

One drawback of this method are that the prototype must be present in all files that use the function. Another drawback is that if the return type or arguments of the function are changed, these prototypes will have to be updated. This process can be automated with the C preprocessor, so that in getting any prototype results in getting the latest one. For example, the following code declares the function in a separate file (the parameter names are not used by the compiler, but they can be useful to programmers):

#ifndef ADD_H_GUARD
#define ADD_H_GUARD
int add(int a, int b);
#endif

This file uses include guards to avoid illegal multiple definitions of the function. The following code demonstrates how header files are used:

#include "add.h"
int triple(int x){
	return add(x,add(x,x));
}

Now, every time the code is compiled, the latest function prototypes in add.h will be included in the files using them, avoiding potentially disastrous errors.

Alternatives

Some newer languages (such as Java) dispense with header files and instead use a naming scheme that allows the compiler to locate the source files associated with interfaces and class implementations (but in doing so, restrict file-naming freedom). These languages (and perhaps others) preserve type information for functions in the object code, allowing the linker to verify that functions are used correctly.

COBOL and RPG IV have a form of include files called copybooks. Programmers "include" these into the source of the program in a similar way to header files, but they also allow replacing certain text in them with other text. The COBOL keyword for inclusion is COPY, and replacement is done with a REPLACING ... BY ... clause.

See also

External links

bg:Заглавен файл de:Header-Datei es:Archivo de cabecera fa:فایل هدر fr:Bibliothèque standard de C#Les en-têtes de la bibliothèque C ISO ko:헤더 파일 it:Header file nl:Headerbestand ja:ヘッダファイル pl:Plik nagłówkowy ru:Заголовочный файл sv:Headerfil uk:Заголовний файл zh:头文件

If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...