What is Nested File Inclusion in C Programming?

Question: What is Nested File Inclusion in C?

Answer: In simple terms #include containing other #include directives included in the program. For ex.,

/* sll.h */ 
#include <stdio.h>
#include <stdlib.h>
#define BUFFSIZE 512
/* function declarations to be used in a program */
void sum2ints(const int, const int);
void sum2floats(const float, const float);
/* other declarations */

Notice that #include “sll.h” contained, together with other declarations, two more #includes: stdio.h and stdlib.h. Actually, C programs generally contain several library functions which are prototyped in stdio.h and stdlib.h.

Standard requires that nested #include files be supported to a depth of at least eight, but it doesn’t impose a maximum limit on nesting depth. A disadvantage of nested #include files is that they make it difficult to determine the true dependencies of source files on each other. A program on Linux, “make utility” must know these dependencies in order to determine which files need to be compiled after some files are modified. Another disadvantage of #include files is the possibility that one header file being included multiple times. Let’s illustrate this error, for ex.,

#include "a.h"
#include "a.h"

Although, no one would ever make such a mistake. But what happens if one does like as

#include "a.h"
#include "b.h"

and each #include a.h and b.h contains a nested #include of x.h. Once again #include x.h is included twice and only this time it’s not as obvious.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Multiple inclusion occurs most often in larger programs with multitude of header files, thus it’s not easy to find. How can one prevent it from occurring in programs? The solution is easy with conditional compilation. Let’s understand this right away,

If you define all your’s header declarations as,

#include _HEADERNAME_H
#define _HEADERNAME_H 1
 * Write here all your stuff you want in this header file

then the risks of multiple inclusions are eliminated. Therefore, the first time when the header file is included it’s processed normally and the symbol _HEADERNAME_H is defined to be 1. When it’s included again, this time preprocessor ignores it because of conditional compilation. Symbol _HEADERNAME_H is named after the filename to avoid any conflicts with other similar symbols in the other header files.


Keep in mind, though, that the preprocessor reads the entire contents of header file, even if the whole file is ignored. Because of this, processing slows down compilation and therefore multiple inclusions whether by nested #include’s or not should be avoided when possible.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.