The include Directive in Programming Languages

Lecture



The include directive instructs a text-file processor to replace the text of the directive with the contents of the specified file.

The act of inclusion may be logical in nature. The processor may simply process the contents of the included file at the location of the directive, without creating a combined file.

Different processors may use different syntax. The C preprocessor (used with C, C++ and in other contexts) defines the include directive as a line that begins with #include and is followed by a file specification. COBOL defines the include directive, written as copy , for including a copybook.

Typically, in C/C++ the include directive is used to include a header file , but it can include any file. Although it is relatively rare, it is sometimes used to include a body file, such as a .c file.

The include directive can support encapsulation and reuse . Different parts of a system can be separated into logical groups while still relying on each other through file inclusion. C and C++ are designed to use include, and also to optimize build time by allowing a declaration to be separate from the implementation . The included information can be reduced to declarations only.

Because many consider that including the contents of a file has significant drawbacks, newer languages were designed without an include directive. Languages such as Java and C# support modularization through the concept of import , which lets a module use the assets of another module at a conceptual level, rather than by including text.

The include Directive in Programming Languages

Language support

C/C++

Both C and C++ are commonly used with the C preprocessor, which replaces the #includedirective line with the contents of the specified file. The file path is enclosed either in double quotes (for example, "xyz.h") or in angle brackets (for example, ). Some preprocessors locate the included file differently depending on the delimiters enclosing it; a path in double quotes is treated as relative to the including file, while a path in angle brackets is treated as located in one of the directories of a configured system search path.

Examples of such statements include:

// include the standard C header 'stdio.h'; probably a file with that name 
#include  

// include the standard C++ header 'vector'; may or may not be a file
#include  
// include a user-defined header file named 'user_defined.h'
#include "user_defined.h"   

The include directive allows the development of code libraries that:

  • guarantee that everyone uses the same version of a data layout definition or procedural code throughout a program
  • make it easy to cross-reference where components are used in a system
  • make it easy to change programs when needed (only one file needs to be edited)
  • save time by reusing data layouts

Example

Given two C source files. One defines the function add(), and the other uses it. Without using the include directive, the consuming file can declare the function locally as a function prototype :

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

One drawback of this approach is that the function prototype must be present in every file that calls add(). Another drawback is that if the signature changes, every consuming file must be updated. Placing the prototype in a single separate file avoids these problems. If the prototype is moved to the header file add.h, the consuming source file becomes:

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

Header file

In C and C++, a header file is a source code file that allows programmers to separate elements of a codebase — often into reusable, logically related groups.

A header file declares programming elements such as functions , classes , variables , and preprocessor macros . A header file allows a programmer to use programming elements in multiple body files based on a common declaration in the header file. The declarations in a header file allow body files to use implementations without directly including the implementation code. A header separates the interface from the implementation .

Compilation errors can occur if multiple header files include the same file. One solution is to avoid including files in header files — possibly requiring redundant include directives in body files. Another solution is to use an include guard in each header file. [

The C standard library is declared as a set of header files. The C++ standard library is similar, but the declarations may be provided by the compiler without reading an actual file.

Standard C header files are named with the .h file name extension , for example #include . Typically, user-defined C header files have the same extension. User-defined C++ header files tend to have a greater variety of extensions, including .hpp, .h++ and .hh.

Header

The name of a C++ standard library header in angle brackets (for example, ) causes the declarations to be included, but they may not come from a file.

Header unit

Starting with C++20 , C++ supports import semantics via a header unit , that is, separate translation units synthesized from a header.

They are intended to be used together with modules .

The syntax used in this case is as follows:

export [optional] import [header-name];

Example:

import < stdio.h > ; // support for this is optional
import  ; // support for this is mandatory per the standard
export import " user_defined.h " ;  

Header units are provided for all headers of the C++ standard library.

COBOL

COBOL (as well as RPG IV ) allows programmers to copy copybooks into program source code — which is similar to include, but allows text replacement. The COBOL keyword for inclusion is COPY, and replacement is performed with the REPLACING ... BY ...clause. The include directive has been present in COBOL since COBOL 60, but was changed from the original INCLUDE to COPYby 1968.

Fortran

Fortran does not require header files as such . However, Fortran 90 and later have two related features: includestatements and modules. The former can be used to share a common file containing procedure interfaces, much like a C header, although an interface specification is not required for all kinds of Fortran procedures. This approach is not commonly used; instead, procedures are usually grouped into modules, which can then be referenced with the usestatement in other areas of code. For modules, header-like interface information is generated automatically by the compiler and is usually placed in separate module files, although some compilers put this information directly into object files. The language specification itself does not require the creation of any additional files, although module procedure interfaces are almost universally propagated in this way.

Pascal

Most Pascal compilers support the compiler directive $i or $include, in which the $i or $include directive immediately follows the start of a comment block in the form

  • {$i filename.pas}
  • (*$I filename.inc*)
  • {$include filename.inc}
  • (*INCLUDE filename.pas*)

Where the $ior $include directive is case-insensitive , and filename.pas or filename.inc is the name of the file being included. (Pascal include files are usually named with the .inc extension , but this is not required.) To prevent unbounded recursion, some compilers limit the include depth to a certain number, forbid a file from including itself or any currently open file, or limit includes to at most one include file at a time, so that an include file cannot include itself or another file. However, a program that includes other files may include several, only one at a time.

PHP

In PHP, the include directive causes another PHP file to be included and evaluated. Similar commands are require: which, if the inclusion fails, produces a fatal exception and stops the script, and include_once, require_oncewhich prevent a file from being included or required again if it has already been included or required, avoiding the double-inclusion problem of C.

Other languages

Other notable languages with an include directive:

  • include ...( Fortran , MASM )
  • The include Directive in Programming Languages(HTML SSI )
  • var ... = require("...")(JavaScript with CommonJS )
  • The include Directive in Programming Languages ( JSP )
  • {$I ...}( UCSD Pascal , Turbo Pascal )
  • %include ...( PL/I )
  • /COPY QCPYLESRC,QBC(RPG IV – the first argument is the file name, the second argument is the copybook)
  • local ... = require("...")( Lua)
  • import ...;( D )

Modern languages (such as Haskell and Java ) tend to avoid the include directive construct, preferring modules and import/export semantics. Some of these languages (such as Java and C# ) do not use forward declarations, and instead identifiers are recognized automatically from source files and read directly from dynamic library symbols (usually referenced with importor directives).using

See also

  • [[b12844]]
  • [[b3932]]
  • Application programming interface (API) – a connection between computers or programs
  • Class implementation file – a file containing the source code that implements the methods declared in the corresponding header file.
  • Header-only – a C or C++ library defined entirely in header files
  • Interface description language – a computer language used to describe the interface of a software component.
  • File inclusion vulnerability – a type of web vulnerability
  • Modular programming – a software design technique
  • One Definition Rule – a rule of the C++ programming language
  • #pragma once – a preprocessor directive in C and C++
  • Precompiled header – an optimized file type in computer programming
  • Transclusion — the automatic inclusion of one set of data in another.
  • Unity build (Jumbo or Blob build ) – a method of speeding up C/C++ project builds
created: 2025-01-29
updated: 2026-09-29
111



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Algorithmization and programming. Structural programming. C language"

Terms: Algorithmization and programming. Structural programming. C language