Skip to main content
Engineering LibreTexts

1.2.1: Understanding Preprocessing- File Inclusion

  • Page ID
    29004
  • his type of preprocessor directive tells the compiler to include a file in the source code program. There are two types of files which can be included by the user in the program:

    1. Header File or Standard files: These files contains definition of pre-defined functions like cout & cin, getline() etc. These files must be included for working with these functions. Different function are declared in different header files. For example: standard Input/Output functions are in ‘iostream’ file, whereas functions which perform string operations are in 'string’ file.
      Syntax:
      #include <iostream>
      #include <string>
      

      where iostream and string are both names of files that the system will include. The ‘<‘ and ‘>’ brackets tells the compiler to look for the file in standard set of directories. The contents of these files are literally imported into the source code as part of the pre-processor step so that when the compiler goes to locate the function definitions they are found. 

      A full list of C++ header files shows all of the currently supported header files and a brief description of what they are used for.

    2. User Defined Header Files: Users have the ability define their own functions that are used across several files or projects. These files can be included into the source code as:
      #include "filename"

    where filename is the name of the actual file that contains the code. Notice that user defined files are enclosed in double quotes as opposed to the < > brackets. Normally these files exist in the same folder as the .cpp file. Certain tools that are used to build large software projects have the capability to define other locations for header files...but that is beyond the scope of this module.

    One more note: many header files in the past, especially in the C programming world, ended in a .h - some of these files continue to exist in code. So if you see a file name such as <stdio.h> - know that is a valid name, just an older format.

    Adapted from: "C/C++ Preprocessors" by Harsh AgarwalGeeks for Geeks is licensed under CC BY-SA 4.0