C Language | Preprocessing | Include - #include

The #include preprocessor directive, which is processed before compilation, can include header files containing declarations and other shared definitions in every source file. This operation is called an include.

Creating a Header File

In medium-sized and larger development projects consisting of multiple files, file organization is as important as programming techniques. A small test program like the ones used here can fit in a single file, but real projects cannot. Development teams often work concurrently on separate areas, such as graphics and audio, which cannot reasonably be managed in one file.

When logically separated features are developed in separate files, those files must eventually be brought together for compilation. By convention, the source file containing the main() function and used to create an executable has a *.c extension. Other files that provide library declarations and feature sets are header files with a *.h extension. Header files are inserted before the main() function.

To compile multiple files together, use the #include preprocessor directive to tell the compiler to insert a header file at a specified location. When the compiler encounters #include, it inserts the specified file at that point. Until now, examples such as #include <stdio.h> have included library files provided with C. This operation is called including a file.

For files other than standard headers, write the filename inside double quotation marks, as in #include "filename". This was explained in Your First C Program. Consult your compiler documentation for details about how files are searched. A filename enclosed in double quotation marks is usually searched for in the same directory as the *.c file.

An include can be understood as a simple file insertion. Commands beginning with # are instructions for the preprocessor, not the compiler. Preprocessing runs before source code is compiled. Because #include inserts a file, the contents of the specified file are inserted as-is before the compiler processes the source code. Developers can place declarations for common functions in a header file and include it in the project’s C source files. This avoids writing the same declarations repeatedly.

Code 1

/* sample.h */
int strlen(const char *str);

Code 2

int strlen(const char *str) {
  int count;
  for(count = 0 ; *(str + count) ; count++);
  return count;
}

Code 3

#include <stdio.h>
#include "sample.h"

int main() {
 char *str = "Kitty on your lap";
  printf("%s length=%d\n" , str , strlen(str));
  return 0;
}

Create the header file shown in Code 1, include it in Code 3, and compile the program. The contents of the header file are copied to the location of #include "sample.h" before compilation. Code 1 declares the strlen() function, which returns the number of characters in a string excluding the null character. The implementation of strlen() is written in Code 2. Once created, a header file like this can be reused across development projects.